[8] Spring Cloud Config component

Spring Cloud Config is a distributed configuration management solution, including the server side and the client side.

Introduction to Config

  • Server side : Provides storage of configuration files, provides the contents of configuration files in the form of interfaces, and
    @EnableConfigServerembeds them in Spring boot applications by using annotations.
  1. The naming rules are as follows: {application}-{profile}.ymlor {application}-{profile}.propertieswhere
    application is the application name and profile refers to the environment (used to distinguish development environment, test environment, production environment, etc.)
  • Client : Obtain configuration data through the interface and initialize its own application

Insert image description here

Build config-server

Insert image description here

  1. Create a new public warehouse and create a new configuration file on the gitee productm-service-resume-dev.yml
server:
  port: 8080

spring:
  application:
    name: m-service-resume
#注册发现
eureka:
  client:
    service-url:
      defaultZone: http://CloudEurekaServerA:8761/eureka,http://CloudEurekaServerB:8762/eureka
        # 每隔多久拉取⼀次服务列表
    registry-fetch-interval-seconds: 30
  instance:
    #显示ip
    prefer-ip-address: true
    # 192.168.28.22:m-service-autodeliver:8090:1.0-SNAPSHOT
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
    #元数据
    metadata-map:
      A: A1
      B: A1
# 租约续约间隔时间,默认30秒
    lease-renewal-interval-in-seconds: 30
# 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,EurekaServer会将服务从列表移除
    lease-expiration-duration-in-seconds: 90
my:
  test:
    num: 100
  1. Build modulem-cloud-config-server
  2. Import dependencies
 <!--eureka client 客户端依赖引⼊-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--config配置中⼼服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
  1. Startup class
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer //开启Config配置
public class McloudConfigServer {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(McloudConfigServer.class,args);
    }
}

  1. Configuration file application.yml
  • Add config configuration
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/vipMygz/m-cloud-config-server.git
          username: XXXXXX
          password: XXXXXX
          search-paths:
            - m-cloud-config-server
      label: master
server:
  port: 9006

spring:
  application:
    name: m-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/vipMygz/m-cloud-config-server.git
          username: XXXXXX
          password: XXXXXX
          search-paths:
            - m-cloud-config-server
      label: master

#注册发现
eureka:
  client:
    service-url:
      defaultZone: http://CloudEurekaServerA:8761/eureka,http://CloudEurekaServerB:8762/eureka
    # 每隔多久拉取⼀次服务列表
    registry-fetch-interval-seconds: 30
  instance:
    prefer-ip-address: true
    instance-id: ${
    
    spring.cloud.client.ip-address}:${
    
    spring.application.name}:${
    
    server.port}:@project.version@
    # 租约续约间隔时间,默认30秒
    lease-renewal-interval-in-seconds: 30
    # 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,EurekaServer会将服务从列表移除
    lease-expiration-duration-in-seconds: 90
  1. Start the config service
    Insert image description here

  2. Verify access configuration http://localhost:9006/master/m-service-resume-dev.yml

    Insert image description here

Client transformation

? ? ? Since it is the configuration center, we register the client service to the Eureka center, and put that part of the configuration in the configuration center. Take a look at the effect.

  1. Transformationm-service-resume-8080
  2. Introduce dependencies
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-config-client</artifactId>
</dependency>
  • Config client configuration
spring:
  cloud:
     config:
      uri: http://localhost:9006/
      name: m-service-resume
      profile: dev
      label: master
  1. Modify application.yml to bootstrap.yml configuration file
server:
  port: 8080

spring:
  application:
    name: m-service-resume

  cloud:
     config:
      uri: http://localhost:9006/
      name: m-service-resume
      profile: dev
      label: master

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl  #避免将驼峰命名转换为下划线命名

  1. Start m-service-resume-8080and observe the registration center
    Insert image description here
  2. Observe the registration center.
    Insert image description here
    The registration was successful, but there was a problem. Some of our configurations did not take effect. No IP is displayed. We found the configuration
    Insert image description here
    and the results obtained by the config configuration service are sorted alphabetically by ABCD.
    Insert image description here
    This is not the focus of the problem. The problem should be that some configurations do not take effect.
    Insert image description here

Config configuration manual refresh

question

  1. The client service adds a controller to obtain my.test.num
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    
    
    @Value("${my.test.num}")
    private String myTest;


    @GetMapping("/viewconfig")
    public String viewconfig() {
    
    
        return "msg==>" + myTest  + " num=>" + myTest;
    }
}
  • Modify the configuration num of gitee to 200 and find that the client is not没有获取到最新的配置
    Insert image description here

Configure manual refresh

  1. Add configuration
management:
 endpoints:
 web:
 exposure:
 include: refresh

##### 或者
management:
 endpoints:
 web:
 exposure:
 include: "*"
  1. Client Add to the class used by the client to configure the information@RefreshScope

  2. Manually initiate a POST request to the Client, http://localhost:8080/actuator/refresh , to refresh the configuration informationInsert image description here

  3. Restart the service and access the interface
    Picture description here
    . Note: The manual refresh method avoids service restart (process: Git configuration change -> for loop script manually refreshes each microservice)

Config configuration automatic update

In the microservice architecture, we can combine the message bus (Bus) to realize automatic update of distributed configuration (Spring CloudConfig+Spring Cloud Bus) to achieve a notification that takes effect everywhere.

Spring Cloud Bus (based on MQ, supports RabbitMq/Kafka) is the message bus solution in Spring Cloud. The
combination of Spring Cloud Config + Spring Cloud Bus can realize automatic updating of configuration information.

  1. Config Server server adds message bus support
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>
  1. ConfigServerAdd configuration
spring:
 rabbitmq:
  host: 127.0.0.1
  port: 5672
  username: guest
  password: guest
  1. Microservice exposed port
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

# 暴露端口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. Restart each service, change the configuration, and send a post request to the configuration center server.
http://localhost:9003/actuator/bus-refresh //各个客户端配置即可⾃动刷新
http://localhost:9006/actuator/bus-refresh/m-service-resume:8081  //定向刷新

Guess you like

Origin blog.csdn.net/u014535922/article/details/129998666