SpringCloud Consul Config Configuration Center (2)

The last startup command of the configuration center is consul agent -dev, so the data will not be persisted, and the key/value configuration information will be lost after restarting consul.

To achieve data persistence, use the following command to start, -data-dir is the data storage directory:

consul agent -server -bootstrap-expect 1 -data-dir E:\software\consul_1.9.4\data -node=consulServer1 -bind 127.0.0.1 -ui -rejoin  -client 0.0.0.0

Based on the previous series, add a common module to the main project, provide Redis tool classes, read the Redis configuration from the Consul Config configuration center, and then reference the Common module in the order service.

1. Maven dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot中的redis依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis依赖commons-pool>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

Because it needs to be compiled independently, <dependencyManagement> must be removed, and the startup class must also be removed.

2. Adjust the maven dependency of the order service and add Consul Config and common modules.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
    <groupId>com.example.consul</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

3. Adjustment of order service configuration file

server:
  port: 8802
spring:
  application:
    name: orderservice
  profiles:
    active: dev
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: orderservice
      config:
        enabled: true
        format: PROPERTIES
        prefixes: config
        defaultContext: orderservice
        profileSeparator: ','
        data-key: data
  config:
    import: optional:consul:localhost:8500

4. Add Redis related attribute configuration in Consul Config Configuration Center

The key is set to:

config/orderservice,dev/data

The value is set to:

spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#关闭超时时间
#配置文件中出现spring.redis.lettuce.pool属性的时候,
# 才会使用连接池,才需要maven中配置commons-pool 这个依赖
spring.redis.lettuce.shutdown-timeout=100
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=200

5. Implement an API interface in the order service to test the Redis cache read and write function.

// redis中存储的过期时间60s
private static int ExpireTime = 60;

@Resource
private RedisUtil redisUtil;

@GetMapping("set")
public boolean redisset(String key) {
    return redisUtil.set(key,"Alice",ExpireTime);
}

@GetMapping("get")
public Object redisget(String key) {
    return redisUtil.get(key);
}

 

6. API interface test

First, set up the cache 

http://localhost:8802/set?key=key1 , return true, set successfully

Then, read the cache

http://localhost:8802/get?key=key1,返回 Alice

 

7. For the reuse implementation method of the configuration class, dynamically reading the properties from the Consul Config configuration center is the same as reading from the service configuration file, so the annotations on the configuration class are equally valid.

@Configuration
@ConfigurationProperties

For example, first write a configuration class,

@Configuration
@ConfigurationProperties(prefix = "order")
public class OrderConfig {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "OderConfig{" +
            "name = '" + name + '\'' +
            ", address = '" + address + '\'' +
            '}';
    }
}

Add key-value pairs in the configuration center:

order.name=book
order.address=beijing

Develop an API interface,

@Autowired
private OrderConfig orderConfig;
@GetMapping("/config")
public String config() {
    return orderConfig.toString();
}

Finally access the interface to test whether the configuration class is effective:

http://localhost:8802/config

returned messages:

OderConfig{name = 'book', address = 'beijing'}

 

 

 

 

Guess you like

Origin blog.csdn.net/suoyx/article/details/115021618