Spring Cloud Learning (six): Config configuration management

1. Config Introduction

In a distributed, we have a lot of services, each service has a configuration file, if you modify a used across multiple services configuration items, you need to change the individual services, very troublesome. But also in the development, testing, production configuration is used in three different scenarios, in order to facilitate the management of these configurations, you use the configuration center.
Configuration Center is divided into the server and the client, the server is mainly reads the configuration item git / svn repository, and then the client will go to get the configuration from the server.

2. Preparation

A registration center, a Config Server, a Config Client, different environmental profiles

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

Content are

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

3. code implementation

3.1 Registration Center

Here is not how the new registration center

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);
    }
}
  • Start Registry and configuration center
    access http://127.0.0.1:5006/config/dev
    return

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

Rule is requested address / {application} / {profile} [/ {label}], according to the named file to define

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

My file name is config-dev.properties, in line with the third {application} is the config, {profile} is 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 Luliangshan 
 * @date 2019/7/24 
 * / 
@RestController 
@RefreshScope 
public  class TestController { 
 
    @Value ( "Hello $ {}" )
     Private String Hello; 
 
    @RequestMapping ( "/ Test" )
     public String Test () {
         return "hello configuration values:" + hello; 
    } 
}

Note: The configuration of cloud.config need to write to a successful start in bootstarp.yml because these things need to application.yml, and is the first to application.yml of bootstarp.yml

hello configuration value: this is dev
  • Change the contents of the configuration file in git

this is dev 改为 this is dev change
hello configuration value: this is dev

With git found no change in the content of the document and change, which is just what we need to manually refresh

  • pom.xml introducing monitoring module

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • Actuator configuration file to add a default refresh endpoint because actuator only health, info, so use to refresh need to add

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

note:

  1. I see a lot of tutorials are online at http: // localhost: 5007 / refresh, but I always reported 404 errors.

  2. http: // localhost: 5007 / actuator / refresh  sure to post the request, the browser is accessible on the get request, the request will be less than

  3. @RefreshScope This annotation is also necessary to add, if not refresh even if successful, will not return again to request the configuration information after change

  4. I do not see a lot of tutorials actuator added refresh endpoints, leading to the beginning of time it has been 404

hello configuration value: this is dev change

So far, Config Client also succeeded welcome message: http: //pikaqiu.vip/article/2371.html sample code: https: //github.com/Liangshan1994/SpringCloud 


Guess you like

Origin www.cnblogs.com/liangshandada/p/11243375.html