如何在SpringCloud2023中快速集成配置中心

题图来自APOD

你好,这里是codetrend专栏"SpringCloud2023实战"。

前言

配置中心在前文提到有很多选型,在这里以 Spring Cloud Config 为例说明配置中心的集成和使用。

选择 Spring Cloud Config 作为配置中心原因如下:

  • 无依赖,直接以Springboot服务部署方式启动。
  • 可以使用本地配置,也可以使用git的版本配置。
  • 集成方便,注解通用,集成starter即可。

服务端启动

引入pom.xml

  • Spring Cloud Config 服务端的核心依赖是 <artifactId>spring-cloud-config-server</artifactId>
  • 同时集成了注册中心,使得配置中心可以分布式部署、调用时的负载均衡。
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>io.rainforest</groupId>
        <artifactId>banana</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>banana-config</artifactId>
    <description>spring cloud config</description>
    <packaging>jar</packaging>


    <dependencies>
        <!--配置中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!--注册中心客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!-- 工具包依赖 -->
        <dependency>
            <groupId>io.rainforest</groupId>
            <artifactId>banana-common-core</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-crypto</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-http</artifactId>
        </dependency>
        <!--接口文档-->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

修改配置

yaml 复制代码
spring:
  application:
    name: config
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
    config:
      server:
        native:
          search-locations: classpath:/appconfig # 使用本地配置文件
  profiles:
    active: native
server:
  port: 10110
  servlet:
    context-path: /

修改启动类

java 复制代码
package io.rainforest.banana.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

注:此处是子模块工程配置,更详细的代码参看:gitee.com/r0ad/spring...

验证配置访问

通过 http://localhost:10110/client1/dev 就能访问到配置中心的应用。

  • http://localhost:10110 是应用地址。
  • client1 是应用名称。
  • dev 是环境名称。

配置信息如下:

json 复制代码
{
    "name": "client1",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "classpath:/appconfig/client1-dev.yml",
            "source": {
                "server.servlet.context-path": "/client1",
                "server.tomcat.accept-count": 500,
                "server.tomcat.connection-timeout": -1,
                "server.tomcat.max-connections": 10000,
                "server.tomcat.max-http-post-size": -1,
                "server.tomcat.max-threads": 1000,
                "server.tomcat.min-spare-threads": 25,
                "cc": "cccccc2223333333344444455555555",
                "feignId": "client3",
                "feignPath": "/client3"
            }
        }
    ]
}

通过该链接实际访问的就是该配置classpath:/appconfig/client1-dev.yml

实际配置中心客户端也是基于http请求访问对应的配置的。

客户端引入

客户端引入pom.xml

  • 引入配置中心主要是引入 <artifactId>spring-cloud-starter-config</artifactId>
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>io.rainforest</groupId>
        <artifactId>banana</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>banana-client3</artifactId>
    <description>spring cloud banana-client3</description>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!--注册中心客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--配置中心客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注:此处是子模块工程配置,更详细的代码参看:gitee.com/r0ad/spring...

客户端修改配置

  • 新增配置文件 application.yml,配置中心配置主要是 spring.cloud.config 下面的配置。
yaml 复制代码
spring:
  application:
    name: client3
  cloud:
    config:
      profile: dev
      name: ${spring.application.name}
#      uri: http://localhost:10110
      discovery:
        enabled: true
        service-id: config
    zookeeper:
      connect-string: 127.0.0.1:2181

客户端修改启动类

  • 启动类不需要特殊修改。
java 复制代码
package io.rainforest.banana.client3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

可能出现的问题

可能出现无法通过service-id找到配置中心。在SpringCloudConfig的文档提到一个解决方法。

这个时候需要新建 application.properties ,写入如下配置。

yaml 复制代码
spring.config.import=optional:configserver:

这个是springcloud2022的问题,之前版本并未出现过该情况。之前启动是通过 bootstrap.yml 启动,现在是 application.yml,导致出现的这个问题。

关于作者

来自一线全栈程序员nine的探索与实践,持续迭代中。

欢迎关注公众号"雨林寻北"或添加个人卫星codetrend(备注技术)。

相关推荐
吃饱了得干活2 天前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
蝎子莱莱爱打怪6 天前
XZLL-IM干货系列 04|Netty 长连接实战:Pipeline 怎么排、心跳怎么跳、连接怎么管
后端·微服务·面试
SamDeepThinking7 天前
Java微服务练习方式
java·后端·微服务
米丘10 天前
微前端之 Web Components 完全指南
微服务·html
霸道流氓气质13 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
慧一居士13 天前
Feign的GET请求如何传递对象参数?
java·spring cloud
我登哥MVP13 天前
SpringCloud Alibaba 核心组件解析:服务链路追踪
java·spring boot·后端·spring·spring cloud·java-ee·maven
慧一居士13 天前
SpringCloud 微服务Feigin 用的完整调用端和被调用的示例
java·spring cloud
霸道流氓气质13 天前
Spring Boot 微服务性能优化完全指南
spring boot·微服务·性能优化
地瓜伯伯13 天前
从MESI缓存一致性协议讲透synchronized的底层
java·spring boot·spring·spring cloud·微服务·springcloud