03-Config
1. @ConfigurationProperties
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "ooxx")
public class OoxxConfig {
private String appKey;
private String secret;
private String host;
private Map<String , String> urls;
public String getUrl(String key) {
return host + urls.get(key);
}
}
# 相关的配置
ooxx:
appKey: app20817600197291
secret: 6cc568b0328841181546e52c677d137cbdbac054739ed12ee6
host: http://ip:port
urls:
imp: /biz/company/import
activation: /biz/company/activation
logout: /biz/company/logout
2. @Configuration
1. bean配置注入
初始化了两个bean:
redisTemplateString
: value字符串序列化redisTemplateObj
: value进行json序列化,可以存json
/**
* redisTemplateObj
*
* @param redisConnectionFactory redisConnectionFactory
* @return RedisTemplate<String, Object>
*/
@Bean(name = "redisTemplateObj")
public RedisTemplate<String, Object> redisTemplateObj(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
// ooxxConfig bean 初始化,
// 服务启动删掉redis中ooxxConfig,@Bean注入内存ooxxConfig,定时任务初始化redis中ooxxConfig
redisTemplate.delete(RedisKeyUtils.getOoxxConfigKey());
return redisTemplate;
}
/**
* redisTemplate
*
* @param redisConnectionFactory redisConnectionFactory
* @return RedisTemplate<String, Object>
*/
@Bean(name = "redisTemplateString")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 使用fastjson序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// value值的序列化采用: StringRedisSerializer
template.setValueSerializer(stringRedisSerializer);
template.setHashValueSerializer(stringRedisSerializer);
// key的序列化采用: StringRedisSerializer
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setConnectionFactory(redisConnectionFactory);
return template;
}
3. 分布式服务动态配置
1. 老方式
- 服务启动注入bean,并删除Redis里的config
@Configuration
public class InitBeanConfig {
@Resource
private OoxxConfigMapper ooxxConfigMapper;
/**
* 初始化OoxxConfig配置
*
* @return OoxxConfig
*/
@Bean(name = "ooxxConfig")
public OoxxConfig ooxxConfig() {
OoxxConfig param = new OoxxConfig();
param.setAppId("32487378776789623");
return ooxxConfigMapper.selectOne(param);
}
}
- 更新DB、Redis
/**
* ooxxConfigUpd
*
* @param ooxxConfigUpd ooxxConfigUpd
* @return OoxxConfig
*/
@PostMapping("/ooxxConfigUpd")
public OoxxConfig ooxxConfigUpd(@RequestBody(required = false) OoxxConfig ooxxConfigUpd) {
if (ooxxConfigUpd != null) {
ConfigHolder.getServiceAgentConfiguration().setServiceURI(ooxxConfigUpd.getOxServer() + "/ox_frontmv_serv");
ooxxConfigMapper.updateByPrimaryKey(ooxxConfigUpd);
return redisService.ooxxConfig(true);
} else {
return ooxxConfig;
}
}
- 定时刷新每个节点
/**
* 更新ooxxConfig(一分钟一次)
*/
@Scheduled(cron = "0 */1 * * * ?")
public void updOoxxConfig() {
BeanUtil.copyProperties(redisService.ooxxConfig(false), ooxxConfig);
ConfigHolder.getServiceAgentConfiguration().setServiceURI(ooxxConfig.getOxServer() + "/ox_frontmv_serv");
}
- redis
/**
* 查询ooxxConfig
*
* @param isRefreshCache isRefreshCache
* @return OoxxConfig
*/
@Override
public OoxxConfig ooxxConfig(Boolean isRefreshCache) {
String localKey = RedisKeyUtils.authConfigKey();
if (!isRefreshCache) {
Object o = redisTemplateObj.opsForValue().get(localKey);
if (o != null) {
return (OoxxConfig) o;
}
}
List<OoxxConfig> ooxxConfigs = ooxxConfigMapper.selectAll();
if (ooxxConfigs.size() > 0) {
OoxxConfig one = ooxxConfigs.get(0);
redisTemplateObj.opsForValue().set(localKey, one, Duration.ofDays(RandomUtil.randomInt(7, 14)));
return one;
}
return null;
}
2. 新方式
redis里不存配置信息
- 服务启动创建bean,数据库查询
@Configuration
public class InitBeanConfig {
@Resource
private OoxxConfigMapper ooxxConfigMapper;
/**
* 初始化OoxxConfig配置
*
* @return OoxxConfig
*/
@Bean(name = "ooxxConfig")
public OoxxConfig ooxxConfig() {
OoxxConfig param = new OoxxConfig();
param.setAppId("32487378776789623");
return ooxxConfigMapper.selectOne(param);
}
- 一个节点获取所有节点信息,并向所有节点发请求,更新所有节点配置
@Resource
private SpringClientFactory factory;
@Resource(name = "ooxxConfig")
public OoxxConfig ooxxConfig;
/**
* ooxxConfigUpd
*
* @param ooxxConfigUpd ooxxConfigUpd
* @return OoxxConfig
*/
@PostMapping("/ooxxConfigUpd")
public OoxxConfig ooxxConfigUpd(@RequestBody(required = false) OoxxConfig ooxxConfigUpd) {
if (ooxxConfigUpd != null) {
ooxxConfigMapper.updateByPrimaryKey(ooxxConfigUpd);
ILoadBalancer lb = factory.getLoadBalancer("listao-ooxx");
List<Server> upServers = lb.getReachableServers();
for (Server one : upServers) {
String id = one.getId();
String url = "http://" + id + "/ooxx/ooxxConfigOneNode";
httpRequestService.httpsUtil(url, gson.toJson(ooxxConfigUpd));
}
return ooxxConfigUpd;
} else {
return ooxxConfig;
}
}
/**
* ooxxConfigOneNode
*
* @param ooxxConfigUpd ooxxConfigUpd
*/
@RequestMapping("/ooxxConfigOneNode")
public void ooxxConfigOneNode(@RequestBody OoxxConfig ooxxConfigUpd) {
System.out.println(gson.toJson(ooxxConfigUpd));
BeanUtil.copyProperties(ooxxConfigUpd, ooxxConfig);
ConfigHolder.getServiceAgentConfiguration().setServiceURI(ooxxConfig.getOxServer() + "/ox_frontmv_serv");
}
List<Server>
对象应该是从nacos里获取的
[
{
"host": "192.168.*.*",
"port": port,
"scheme": null,
"id": "192.168.*.*:port",
"zone": "UNKNOWN",
"readyToServe": true,
"metaInfo": {
"serverGroup": null,
"serviceIdForDiscovery": null,
"instanceId": "192.168.*.*#port#DEFAULT#DEFAULT_GROUP@@listao-ooxx",
"appName": "listao-ooxx"
},
"instance": {
"instanceId": "192.168.*.*#port#DEFAULT#DEFAULT_GROUP@@listao-ooxx",
"ip": "192.168.*.*",
"port": port,
"weight": 1.0,
"healthy": true,
"enabled": true,
"ephemeral": true,
"clusterName": "DEFAULT",
"serviceName": "listao-ooxx",
"metadata": {
"preserved.register.source": "SPRING_CLOUD",
"version": "1.0.0"
},
"instanceHeartBeatInterval": 5000,
"instanceHeartBeatTimeOut": 15000,
"ipDeleteTimeout": 30000
},
"metadata": {
"preserved.register.source": "SPRING_CLOUD",
"version": "1.0.0"
},
"alive": true,
"hostPort": "192.168.*.*:port"
},
{
"host": "192.168.*.*",
"port": port,
"scheme": null,
"id": "192.168.*.*:port",
"zone": "UNKNOWN",
"readyToServe": true,
"metaInfo": {
"serverGroup": null,
"serviceIdForDiscovery": null,
"instanceId": "192.168.*.*#port#DEFAULT#DEFAULT_GROUP@@listao-ooxx",
"appName": "listao-ooxx"
},
"instance": {
"instanceId": "192.168.*.*#port#DEFAULT#DEFAULT_GROUP@@listao-ooxx",
"ip": "192.168.*.*",
"port": port,
"weight": 1.0,
"healthy": true,
"enabled": true,
"ephemeral": true,
"clusterName": "DEFAULT",
"serviceName": "listao-ooxx",
"metadata": {
"preserved.register.source": "SPRING_CLOUD",
"version": "1.0.0"
},
"instanceHeartBeatInterval": 5000,
"instanceHeartBeatTimeOut": 15000,
"ipDeleteTimeout": 30000
},
"metadata": {
"preserved.register.source": "SPRING_CLOUD",
"version": "1.0.0"
},
"alive": true,
"hostPort": "192.168.*.*:port"
}
]