如何在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 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>

修改配置

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: /

修改启动类

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);
	}
}

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

验证配置访问

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

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

配置信息如下:

{
    "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 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>

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

客户端修改配置

  • 新增配置文件 application.yml,配置中心配置主要是 spring.cloud.config 下面的配置。
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

客户端修改启动类

  • 启动类不需要特殊修改。
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 ,写入如下配置。

spring.config.import=optional:configserver:

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

关于作者

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

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

  • 30
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值