如何在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 天前
微服务架构下基于k8s的流水线构建实战
微服务·k8s·流水线
逃逸线LOF2 天前
SpringCloud Stream
后端·spring·spring cloud
小南家的青蛙2 天前
Spring Cloud 微服务熔断限流实战
微服务
猫猫不是喵喵.4 天前
从0到1创建一个微服务项目
java·数据库·微服务·1024程序员节
htzyl2064 天前
别急着上微前端:从 6 个场景倒推 Module Federation 2.0 与 qiankun 2.x 的选型
前端·微服务
知守观5 天前
一个方法两个触发口:反编译 XXL-Job 后,我看清了 inner/job 子包的裸奔现状
java·spring boot·微服务
wz97545 天前
Docker 从 systemd 托管容器、overlay2 存储驱动到 Compose 多容器实战
spring cloud·docker·eureka
草莓熊Lotso6 天前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
小郎君65810 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud
jktantan10 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构