config: distributed configuration center & bus: message bus


Each application needs a corresponding yml configuration when running. Multiple servers and application services in a distributed architecture are faced with multiple configuration files. It is difficult to modify and publish. A management center is needed for unified management, which is elegant It solves the problems of dynamic configuration changes, persistence, operation and maintenance costs, etc.

Process:
The distributed configuration center goes to the remote warehouse to read the created yml file, and the application client goes to the distributed configuration center to obtain the configuration

Spring Cloud Config: spring cloud config server and spring cloud config client
based on Http protocol

hello world

1. Create a warehouse on gitee, create application.yml, and configure it as follows:

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: fsfs

2. Create a project config_server
1. Import dependencies:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2.yml configuration:

server:
  port: 8888

# 增加分布式配置中心服务端配置。连接GIT仓库
spring:
  cloud: # spring cloud常用配置前置
    config: # 分布式配置中心配置前置
      server: # 服务端配置
        git: # git文件仓库配置
          uri: https://gitee.com/xxxx/spring_cloud_config.git # git仓库具体地址
          #username: xxxxxx # 私有仓库必须配置用户名和密码。
          #password: xxxxxx # 公开仓库可以省略用户名和密码配置。

3. Add @EnableConfigServer annotation to the startup class

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

3. Access test:
Enter http://localhost:8888/name/profile/label in the address bar of the browser to obtain the required configuration file based on the Config Server.
name-required restful parameter, which represents the name of the main body of the configuration file, and generally application profile-required restful parameter, which represents the name of the extended environment of the configuration file, and reads the default environment extended configuration by default, that is, xx label
in application-xx.yml- optional
Parameter, representing the name of the GIT branch where the configuration file is located, the default is null, which is equivalent to the master branch

Visit http://localhost:8888/application/default (no need to write)/master (branch name)

4. Create project config_client to obtain configuration information of config_server
1. Import 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>

2. Create bootstrap.yml instead of application.yml to prevent conflicts.
New configuration file bootstrap.yml | properties. It is a new configuration file supported by spring cloud config technology.
The configuration file is read by the config distributed configuration center client and requests the distributed configuration center server. After querying and obtaining the configuration file, Spring Boot initializes the environment according to the configuration file.

spring:
  cloud:
    config: # spring cloud config 客户端配置
      uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888
      name: application # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
      # profile: default # 要读取的配置文件环境是什么,默认default
      label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

4. Change the port server.port of the remote warehouse,
start the cofig_client project repeatedly, and observe the port number change

hot refresh

1. Add import dependencies

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

2. Config Client new configuration content

management:
  endpoints:
    web:
      exposure:
        include: refresh,info,health

3. The Config Client modifies the service implementation.
Add a new annotation on the class that uses the remote configuration content (the Service in the case is invalid on the controller):

@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClientService {
    
    
    @Value("${my.content}")
    private String content;
    @Override
    public String test() {
    
    
        System.out.println("content = " + content);
        return content;
    }
}

4. Test hot refresh
Use the PostMan tool to send a POST
to visit http://localhost:8080/actuator/refresh and change the address to refresh the injection. It has actually been refreshed and
visit http://localhost:8080/test again

Spring Cloud Bus: Message Bus

Spring Cloud Bus integrates common message brokers such as RabbitMQ and Kafka on the market. It will connect all the nodes with the Bus mechanism in the microservice system. When there is a data change, it will notify all microservice nodes to update the data synchronously through the message middleware by means of message broadcast. (e.g. microservice configuration update, etc.)

To implement the hot refresh function based on the Bus message bus, it is necessary to add the spring-cloud-starter-bus-amqp dependency in all Eureka Client applications. This dependency is the RabbitMQ message synchronization component integrated with the message bus. The hot refresh based on the message bus is also implemented through the actuator, so the spring-boot-starter-actuator starter dependency is required.
1. Add dependencies in Config Client:

<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.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 总线技术中的amqp相关依赖。 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2. Edit the configuration file: Edit the configuration file in Config Client.

spring:
  rabbitmq:
    host: localhost
    username: guest
    password: giest
management:
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: bus-refresh,info,health

3. Test
The message bus Bus provides a hot refresh service based on the Actuator, and the service address is: http://localhost:8080/actuator/bus-refresh. This service can only be requested using POST, and can be tested using PostMan.

Guess you like

Origin blog.csdn.net/m0_56182317/article/details/130245004