Spring Cloud Consul Configuration Center

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/it_lihongmin/article/details/91374313

table of Contents

1. Find the Key / Value Menu Consul UI interface in

2, and add annotations @EnableDiscoveryClient @EnableConfigurationProperties ({RedisConfig.class}) in the startup class

3, to add a class Controller

4, the configuration will be directly injected into the Bean annotated with @ConfigurationProperties

5, configuration information

6, start the service

1), in your browser to http: // localhost: 8506 / kevinConfig

2), in your browser to http: // localhost: 8506 / redisConfig

3), change the value of the server's configuration


    Previous Consul built in the server cluster, and the cluster service registration has been calling services. Consul addition to the functions of a registry key and value is based on the configuration of the center, you can substitute Spring Cloud Config.

The basic concept Spring Cloud Consul configuration:

    1, the Spring Cloud Config, like, Consul configuration also provides a configuration-based isolation Spring Profiles:

spring.profiles.active = dev

    2, Consul configuration server side key value Type Allowed values are: YAML, the PROPERTIES, KEY-of VALUE, the FILES

# 存储在Consul服务端的配置格式:YAML PROPERTIES KEY-VALUE FILES
spring.cloud.consul.config.format = YAML

    3, the configuration of the data key, default data

# 配置数据key,默认为data
spring.cloud.consul.config.data-key=data

    4, prefix data stored default config

# 数据配置的前缀,默认为config
spring.cloud.consul.config.prefix=config

    5, the configuration is stored in accordance with the project

Here began Consul configure the server demo, Project address: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/consul-demo/consul-config

1. Find the Key / Value Menu Consul UI interface in

    It is configured in the following manner in the menu, as follows:

    Then on one, Consul of the cluster center, has been created in the project consul-config sub- projects.

2, add annotations in the startup class @EnableDiscoveryClient and @EnableConfigurationProperties ({RedisConfig.class})

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@EnableConfigurationProperties({RedisConfig.class})
@SpringBootApplication
public class ConsulConfigApplication {

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

}

3, to add a class Controller

    In Spring Cloud Config project ( Spring Cloud Config service in three configurations ), we already know the configuration changes to the server configuration, the client will not take effect immediately, unless you add @RefreshScope several previous annotations, or specific reference changes effective ( when github repository remote property change, can not get the updated values )

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class ConsulConfigController {

    @Value("${kevin}")
    private String kevinConfig;

    @Autowired
    private RedisConfig redisConfig;

    @GetMapping("kevinConfig")
    public String getConfigValue() {

        return "consul 配置中kevin的值为:" + kevinConfig;
    }

    @GetMapping("redisConfig")
    public String getRedisConfig() {

        return "consul 配置中redis的值为:" + redisConfig.toString();
    }
}

4, with @ConfigurationProperties directly injected into the Bean configuration annotations

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 *  redis配置
 */
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
    /** redis ip地址 */
    private String ip;
    /** redis port */
    private String port;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return "RedisConfig{" + "ip='" + ip + ", port='" + port + '}';
    }
}

5, configuration information

application.properties

# 服务端口
server.port=8506
# 服务名称
spring.application.name=consul-config

bootstrap.properties

# 注册集群的节点地址(ip和端口)
spring.cloud.consul.host=192.168.0.103
spring.cloud.consul.port=8500

# 是否开启Consul服务
spring.cloud.consul.enabled=true
# 是否开启Consul配置服务
spring.cloud.consul.config.enabled=true
# 存储在Consul服务端的配置格式:YAML PROPERTIES KEY-VALUE FILES
spring.cloud.consul.config.format = YAML
# 配置数据key,默认为data
spring.cloud.consul.config.data-key=data
# 数据配置的前缀,默认为config
spring.cloud.consul.config.prefix=config
# 激活的Spring Profiles,多个使用逗号进行间隔
spring.profiles.active = dev
# 配置中应用名称与Spring Profiles的间隔符
spring.cloud.consul.config.profile-separator=.

6, start the service

1), accessible in the browser http: // localhost: 8506 / kevinConfig

2) access in the browser http: // localhost: 8506 / redisConfig

3), change the value of the server's configuration

    With the addition of the Controller @RefreshScope , after performing modify the configuration values in the configuration, visit:

 

Guess you like

Origin blog.csdn.net/it_lihongmin/article/details/91374313