如何在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(备注技术)。

相关推荐
百锦再4 小时前
Java的TCP和UDP实现详解
java·spring boot·tcp/ip·struts·spring cloud·udp·kafka
canjun_wen6 小时前
Nacos框架整合 04,K8s环境下部署Nacos集群:容器化微服务架构实践
spring cloud·微服务
小小工匠6 小时前
极客防御美学:在微服务架构中落地 PoW 工作量证明与防重放机制
微服务·云原生·架构·pow
indexsunny9 小时前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景的应用
java·spring boot·微服务·面试·kafka·prometheus·电商
百锦再9 小时前
Spring Boot Web 后端开发注解核心
开发语言·spring boot·python·struts·spring cloud·kafka·maven
切糕师学AI9 小时前
RabbitMQ 是什么?
微服务·消息队列·rabbitmq
Coder_Boy_9 小时前
技术交流总结:分布式、数据库、Spring及SpringBoot核心知识点梳理
数据库·spring boot·分布式·spring·微服务
七夜zippoe10 小时前
微服务链路追踪实战:SkyWalking vs Zipkin 架构深度解析与性能优化指南
java·开发语言·微服务·springcloud·sleuth·zipkin
百锦再1 天前
Java中的反射机制详解:从原理到实践的全面剖析
java·开发语言·jvm·spring boot·struts·spring cloud·kafka
渣瓦攻城狮1 天前
互联网大厂Java面试实战:核心技术与场景分析
java·大数据·redis·spring·微服务·面试·技术分享