Spring Cloud学习:Config配置管理

Posted liangshandada

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud学习:Config配置管理相关的知识,希望对你有一定的参考价值。

1. Config介绍

在分布式中我们会有很多服务,每个服务都有配置文件,如果修改了某个在多个服务中用到的配置项,那么就需要去各个服务中更改,非常麻烦。 而且在开发,测试,生产三个场景用到的配置也是不同的,为了方便这些配置的管理,就用到了配置中心。
配置中心分为了服务端和客户端,服务端主要是读取git/svn仓库中的配置项,然后客户端会去从服务端获取配置。

2. 准备工作

一个注册中心,一个Config Server,一个Config Client,不同环境的配置文件

config-dev.properties
config-pro.properties
config-test.properties

内容分别是

hello=this is dev
hello=this is pro
hello=this is test

3. 代码实现

3.1 注册中心

这里就不介绍怎么新建注册中心了

3.2 Config Server

  • pom.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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pikaqiu.springcloud</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringCloud-Config</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • application.yml

server:
  port: 5006
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Liangshan1994/SpringCloud
          search-paths: SpringCloud-Config-Repo
          username:
          password:
eureka:
  client:
    service-url:
        defaultZone: http://eureka-server1:3001/eureka/
  instance:
    instance-id: 配置中心-5006
  • SpringCloudConfigApplication.java

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringCloudConfigApplication 
    public static void main(String[] args) 
        SpringApplication.run(SpringCloudConfigApplication.class, args);
    
  • 启动注册中心和配置中心
    访问http://127.0.0.1:5006/config/dev
    返回

    "name":"config",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"99f25c23da606d7588299151d64737ccbf4c544c",
    "state":null,
    "propertySources":[
        
            "name":"https://github.com/Liangshan1994/SpringCloud/SpringCloud-Config-Repo/config-dev.properties",
            "source":
                "hello":"this is dev"
            
        
    ]

请求地址规则是/application/profile[/label],根据文件的命名去定义

/application-profile.yml
/label/application-profile.yml
/application-profile.properties
/label/application-profile.properties

我的文件名是config-dev.properties,符合第三种application是config,profile是dev

3.3. Config Client

  • pom.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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pikaqiu.springcloud</groupId>
    <artifactId>configclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringCloud-Config Client</name>
    <description>Demo project for Spring Boot</description>
 
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>
  • application.yml

server:
  port: 5007
spring:
  application:
    name: config-client
  • bootstrap.yml

spring:
  cloud:
    config:
      name: config
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: config
eureka:
  client:
    service-url:
        defaultZone: http://eureka-server1:3001/eureka/
  instance:
    instance-id: 配置中心客户端-5007
  • SpringCloudConfigClientApplication.java

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudConfigClientApplication 
 
    public static void main(String[] args) 
        SpringApplication.run(SpringCloudConfigClientApplication.class, args);
    
 
  • TestController.java

/**
 * @author 吕梁山
 * @date 2019/7/24
 */
@RestController
@RefreshScope
public class TestController 
 
    @Value("$hello")
    private String hello;
 
    @RequestMapping("/test")
    public String test()
        return "配置中的hello值为:" + hello;
    

注意:cloud.config下的配置需要写在bootstarp.yml中才能启动成功,因为这些东西是需要先于application.yml,而bootstarp.yml就是先于application.yml的

配置中的hello值为:this is dev
  • 更改git中配置文件的内容

this is dev 改为 this is dev change
配置中的hello值为:this is dev

发现并没有随着git中文件内容的更改而更改,这是就需要我们去手动刷新一下

  • pom.xml引入监控模块

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 配置文件中给actuator添加refresh端点 因为actuator默认只有health, info,所以要用到refresh需要自己添加

server:
  port: 5007
spring:
  application:
    name: config-client
management:
  endpoints:
      web:
        exposure:
          include: health, info, refresh
[
    "config.client.version",
    "hello"
]

注意:

  1. 我在网上看到好多的教程都是http://localhost:5007/refresh,但是我都是报404错误。

  2. http://localhost:5007/actuator/refresh 一定要post请求,在浏览器上访问是get请求,也会请求不到的

  3. @RefreshScope这个注解也是必须要加上,如果不加就算refresh成功了,再次请求也不会返回更改后的配置信息

  4. 我看到好多教程都没有actuator添加refresh端点,导致刚开始的时候一直是404

配置中的hello值为:this is dev change

至此,Config Client也成功了 

欢迎留言:http://pikaqiu.vip/article/2371.html
示例代码:https://github.com/Liangshan1994/SpringCloud

以上是关于Spring Cloud学习:Config配置管理的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud学习--配置中心(Config)

springCloud学习-分布式配置中心(Spring Cloud Config)

Spring Cloud学习笔记 篇一:分布式配置中心 Spring Colud Config

SpringCloud学习分布式配置中心(Spring Cloud Config)(Finchley版本)

spring cloud config 配置管理中心

spring cloud config统一配置管理的使用及配置文件