Springcloud configuration center settings

What is the configuration center of
spring cloud spring cloud config provides centralized external configuration support for microservices in the microservice architecture, and the configuration server provides a centralized external configuration for all environments of different microservice applications. What
Insert picture description here
can spring cloud config do?

  1. Centralized management profile
  2. Different configurations in different environments, dynamic configuration updates,
  3. Dynamically adjust the configuration during operation, no longer need to write configuration files on each machine deployed by the service, and the service will uniformly pull and configure its own information from the configuration center
  4. When the configuration changes, the service does not need to restart to perceive the configuration and apply the latest configuration
  5. Expose configuration information in REST interface style

Configure configuration center via github

  1. Create a new repository of spring-cloud-config on github
  2. Modify after local clone and add configuration files for different environments
  3. Create a new module module to set the configuration center

application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center  # 注册进eureka服务器的微服务名字
  cloud:
    config:
      server:
        git:
          uri: https://github.com/sofencyXiao/spring-cloud-config.git
          search-paths:
            - spring-cloud-config  #搜索目录
      label: master  #读取分支


eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

Start class

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer  // 启动配置服务器
public class ConfigCenterApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigCenterApplication.class,args);
    }
}

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">
    <parent>
        <artifactId>Spring Cloud学习</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ConfigCenter</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

Configure the client to access the configuration information of the server
Create a new module to test
the settings of the client pom.xml of the configuration center

<?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>
        <artifactId>Spring Cloud学习</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ConfigClient</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>

bootsrtap.yml is the system-level configuration file loading order prior to application.yml
configuration is as follows

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #客户端配置
    config:
      label: master  #分支名称  github配置中心的分支
      name: config  #配置文件的名字  对应github上面的文件就是{name}-{profile}.yml
      profile: dev  #读取的后缀名称
      uri: http://localhost:3344  #配置中心的地址
#服务注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

Client startup class

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

Test whether the controller can access the configuration file on github

@RestController
public class ConfigClientController {
    
    


    @Value("${config.info}")  //github配置中心上面config-dev.yml下的内容config.info的信息
    private String configInfo;

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

The problem with the above configuration is that after we modified the configuration on github, we http://localhost:3344/master/config-dev.ymlcan access the latest information on github by accessing under the application of 3344 , but accessing the configuration file through the interface on 3355 shows the original information, which is not carried out in time. Update operation.
Unless restarted.

But with the increasing number of services, it is very time-consuming to restart the business module every time you modify it, so you need to introduce an annotation and add actuator monitoring

   <dependency>
    	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

@RefreshScope, and need to use post request again curl -X POST "http://localhost:3355/actuator/refresh"

@RestController
@RefreshScope
public class ConfigClientController {
    
    
    @Value("${config.info}")  //github配置中心上面config-dev.yml下的内容config.info的信息
    private String configInfo;

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

But with more microservices, do you need to execute the post request to activate the client every time? So the concept of bus bus is introduced

Guess you like

Origin blog.csdn.net/qq_43079376/article/details/108314485