Registration center/configuration management——SpringCloud Consul

Consul overview

Consul is a distributed service framework that can provide service discovery, health check, multiple data centers, key/value storage, and is used to realize the discovery and configuration of distributed systems. Cousul is implemented using the Go language, so it is naturally portable. The installation package only contains one executable file, which can be run directly upon startup, making it easy to deploy.


Consul installation and startup

Taking windows as an example, download Consul from the official website: https://www.consul.io/

Insert image description here

After downloading, unzip it, enter the directory and run consul.exe:.\consul.exe agent -dev

After Consul is started, visit http://ocalhost:8500/ in the browser to see the Consul home page.


Consul service registration and discovery

Create cousul-service project and introduce dependencies. Spring Boot Actuator is a package that health check depends on. This project is based on SpringBoot 2.3.1, SpringCloud Hoxton.SR12

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
</dependencies>

Add the following configuration in the application.yml configuration file:

server:
  port: 8080

spring:
  application:
    name: consul-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        instance-id: ${
    
    spring.application.name}:${
    
    server.port}

Add annotations to the startup class@EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class ConsulProducerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ConsulProducerApplication.class, args);
    }
}

Start the project and view the Consul Web page to see that the service registration is successful.


Consul Configuration Center

Refer to the previous section to create the cousul-config project and introduce dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
</dependencies>

Add the following configuration in the bootstrap.yml configuration file (note that bootstrap must be used):

server:
  port: 8080

spring:
  application:
    name: consul-service
  # profiles:
    # active: dev # 指定环境,默认加载 default 环境
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        instance-id: ${
    
    spring.application.name}:${
    
    server.port}
      config:
        enabled: true # false禁用Consul配置,默认为true
        format: yaml  # 表示consul上面文件的格式,有四种:YAML、PROPERTIES、KEY-VALUE、FILES
        prefix: config  # 可以理解为配置文件所在的最外层目录
        default-context: consul-service # 设置应用的文件夹名称
        data-key: consul-service-config # Consul的Key/Values中的Key,Value对应整个配置文件
        # 以上配置可以理解为:加载config/consul-service/文件夹下Key为consul-service-config的Value对应的配置信息
        # 配置环境分隔符,默认值 "," 和 default-context 配置项搭配
        # 例如应用 consul-service 分别有环境 default、dev、test、prod
        # 只需在 config 文件夹下创建 consul-service、consul-service-dev、consul-service-test、consul-service-prod 文件夹即可
        # profile-separator: '-'
        watch:
          enabled: true # 是否开启自动刷新,默认值true开启
          delay: 1000 # 刷新频率,单位毫秒,默认值1000

Add annotations to the startup class@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
// 启用配置属性类,当SpringBoot程序启动时会立即加载@EnableConfigurationProperties注解中指定的类对象
@EnableConfigurationProperties({
    
    MySqlComplexConfig.class})
public class ConsulConfigApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ConsulConfigApplication.class, args);
    }
}

Define MysqlConfig configuration class

@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlConfig {
    
    

    private String host;
    private String username;
    private String password;

    public String getHost() {
    
    
        return host;
    }

    public void setHost(String host) {
    
    
        this.host = host;
    }

    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }
}

Develop ConfigController

@RefreshScope // 用于重新刷新作用域实现属性值自动刷新
@RestController
public class ConfigController {
    
    

    @Autowired
    private MysqlConfig mysqlConfig;

    @GetMapping("getConfig")
    public Map<String, String> getMysqlConfig() {
    
    
        HashMap<String, String> map = new HashMap<>();
        map.put("host", mysqlConfig.getHost());
        map.put("username", mysqlConfig.getUsername());
        map.put("password", mysqlConfig.getPassword());
        return map;
    }
}

Add configuration information in the Consul management interface, click Key/Value on the left menu, create the config/consul-service directory according to the configuration in bootstrap.yml, create key: consul-service-config in the consul-service directory, and add in value Configuration information

Insert image description here

Request http://localhost:8080/getConfig. You can see that the service will obtain the configuration from Consul and return

Guess you like

Origin blog.csdn.net/CSDN_handsome/article/details/132378229