Build the configuration Center

Configure the central server

Create a configuration center module and register it in Eureka. Add the following configuration POM file on the basis of other services to add configuration server Settings

<! - center server config configuration - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-config-server</artifactId> </dependency>Copy the code

The following configuration needs to be added

spring:
  application:
    name: zhao-service-config
  cloud:
    config:
      server:
        git:
          username: [email protected]
          password: xxx
          search-paths:
             - zhao-config-repo
          uri: https://gitee.com/kylezhen/zhao-config-repo.git
      label: main
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
Copy the code

It should be noted that we still try to use Gitee as the pull address of the remote configuration center, otherwise there will be various problems due to the poor github network. After configuration we add @enableconFigServer to the startup class

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

After configuring the server in the configuration center, you can access the configuration file through the server

Configure the center client configuration and manual refresh

Pom file added

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
Copy the code

Increased use of the configuration center after renaming the configuration file to bootstrap.yml. Yml is system-level and has a higher priority than application.yml. This configuration file is checked during application startup and the service address of the configuration center is specified in this configuration file. All application configurations are automatically pulled and enabled. Configure endpoint interfaces such as exposure health checks to update the configuration

spring
  cloud:
    config:
      name: zhao-service-resume
      profile: dev
      label: main
      uri: http://localhost:9007
management:
  endpoints:
    web:
      exposure:
        include: "*"
Copy the code

Add configuration access after configuration

@restController@requestMapping ("/config") public class ConfigController {@value ("${zhao.name}") private String name; // @Value("${mysql.url}") // private String mysqlUrl; @getMapping ("/ viewConfig ") public String viewConfig () {return "zhaoname==>" + name; @getMapping ("/ viewConfig ") public String viewConfig () {return "zhaoname==>" + name; }}Copy the code

Access the interface that gets the configurationHowever, we can’t get the latest configuration this way, so we need to add the @refreshScope annotation to the configuration class that gets the configuration. After the changes are made, manually send a POST request to the service health check interface that uses the configuration file to make the updateIf the value is null, no data has been changed. The preceding information indicates that configuration file changes have been obtained

Dynamically refresh the configuration with Spring Cloud Bus

Rabbitmq and Kafka are the official versions of Rabbitmq and Kafka, but I’ll use Alibaba’s Rocketmq as an example

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-bus-rocketmq</artifactId> < version > 2.1.0. RELEASE < / version > < / dependency >Copy the code

Perform corresponding configurations on the server and client in the configuration center

spring: application: name: zhao-service-config cloud: config: server: git: username: @qq.com password: xxx search-paths: - zhao-config-repo uri: https://gitee.com/kylezhen/zhao-config-repo.git label: main bus: enabled: True rocketmq: name - server: 127.0.0.1:9876Copy the code

By accessing http://localhost:9007/actuator/bus-refresh the configuration change can be pushed to the configuration

Welcome to search attention to my public number [micro view technology], and summary of classified interview questions github.com/zhendiao/Ja…