SpringCloud Config配置中心实战

介绍

本文以理论结合实践编写,篇幅较长,各位看官保持耐心:),部分内容引用自网络。

什么是配置中心?

当微服务过多的时候,每个微服务的配置很难集中管理。SpringCloud Config通过git代码托管来实现配置的集中管理。实现配置中心客户端获取远程的配置文件,并可以动态刷新,即时生效。

如何使用?

SpringCloud Config分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

构建服务端

1.新建模块

添加模块:cloud-config-server3344

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4bHwxGws-1626772734897)(E:\java\微服务\assets\1626763821090.png)]

2.修改pom.xml

添加如下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.在码云上添加配置仓库

登录码云,新建仓库:config-server,新增如下2个yml文件:

在这里插入图片描述

内容自定义即可。

4.修改application.yml

注意此处把配置中心微服务注册到了eureka,后续文章会讲到。

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/indexman/config-server.git #上面创建的git仓库地址

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

5.新建启动类

创建启动类:ConfigCenterMain3344

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

6.启动测试

依次访问:localhost:3344/order-dev.ymllocalhost:3344/order-prod.yml,浏览器分别返回:

msg: this is order-dev config.
msg: this is order-prod config.

说明配置成功!

7.补充知识点

  • 如果仓库有多个文件夹,可配置搜索路径:
search-paths: /**  # 指定搜索根路径下的所有目录,若有多个路径使用逗号隔开
  • 如果配置中心为私有仓库,需要配置git用户名和密码:
username: username  # 以及相应的账户名
password: password  # 和密码
  • 如果需要本地存储配置仓库,需要配置basedir:
basedir: E:\Java_IDEA\config\basedir  # 可以使用这个配置项来指定本地git仓库的路径
  • 具体的微服务配置可通过如下规则获得:
/{
    
    application}/{
    
    profile}[/{
    
    label}]
/{
    
    application}-{
    
    profile}.yml
/{
    
    label}/{
    
    application}-{
    
    profile}.yml
/{
    
    application}-{
    
    profile}.properties
/{
    
    label}/{
    
    application}-{
    
    profile}.properties

构建客户端

1.新建模块

新建模块:cloud-config-client-3355

在这里插入图片描述

2.修改pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.修改application.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: order #配置文件名称
      profile: dev #读取后缀名称
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4.新建启动类

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

5.新建测试类

@RestController
public class ConfigClientController {
    
    
    @Value("${msg}")
    private String msg;

    @GetMapping("/configInfo")
    public String say(){
    
    
        return msg;
    }
}

6.启动测试

启动后访问:localhost:3355/configInfo

this is order-dev config.

存在问题

以上所示,服务端和客户端都配置和测试通过。此时我去修改码云仓库中的配置会出现什么情况?

服务端和客户端还能实时更新配置吗?
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

答案是:

  • 服务端实时刷新没问题
  • 客户端在不重启的情况下无法实时更新

那么如何解决?请看下一篇文章:《SpringCloud Config配置中心动态刷新实战》

猜你喜欢

转载自blog.csdn.net/IndexMan/article/details/118940300