上次配置中心启动命令为consul agent -dev,这样数据不会持久化,重新启动consul后key/value配置的信息会丢失。
实现数据持久化,使用如下命令启动,-data-dir 为数据保存目录:
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
在之前系列基础上,在主工程里添加一个common模块,提供 Redis 工具类, 从Consul Config 配置中心读取 Redis 配置,然后在订单服务里引用Common模块。
1、maven依赖
<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>
因为要独立编译,所以要去掉<dependencyManagement>,另外启动类也要删除掉。
2、调整订单服务的maven依赖,添加 Consul Config 和 common 模块。
<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、订单服务配置文件调整
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、在 Consul Config 配置中心添加 Redis 相关属性配置
key设置为:
config/orderservice,dev/data
value设置为:
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、在订单服务中实现一个API接口,测试 Redis 缓存读写功能。
// 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接口测试
首先,设置缓存
http://localhost:8802/set?key=key1,返回 true,设置成功
然后,读取缓存
http://localhost:8802/get?key=key1,返回 Alice
7、对于配置类的重用实现方法,从Consul Config 配置中心动态读取属性,和从服务的配置文件中读取是一样,所以配置类上的注解一样有效。
@Configuration
@ConfigurationProperties
举个例子,首先写个配置类,
@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 + '\'' +
'}';
}
}
在配置中心添加键值对:
order.name=book
order.address=beijing
开发一个API接口,
@Autowired
private OrderConfig orderConfig;
@GetMapping("/config")
public String config() {
return orderConfig.toString();
}
最后访问接口,测试配置类是否生效:
返回信息:
OderConfig{name = 'book', address = 'beijing'}