Commit 596669d7 by Nepxion

增加Redis为远程配置中心

parent 0f2066b1
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-common-redis</artifactId>
<name>Nepxion Discovery Common Redis</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
<url>http://www.nepxion.com</url>
<parent>
<groupId>com.nepxion</groupId>
<artifactId>discovery</artifactId>
<version>4.2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.common.redis.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.nepxion.discovery.common.redis.operation.RedisOperation;
@Configuration
public class RedisAutoConfiguration {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Bean
public HashOperations<String, String, String> hashOperations() {
return stringRedisTemplate.opsForHash();
}
@Bean
public RedisOperation redisOperation() {
return new RedisOperation();
}
}
\ No newline at end of file
package com.nepxion.discovery.common.redis.operation;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
public class RedisOperation {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private HashOperations<String, String, String> hashOperations;
public String getConfig(String group, String serviceId) {
return hashOperations.get(group, serviceId);
}
public boolean removeConfig(String group, String serviceId) {
return hashOperations.delete(group, serviceId) == 1;
}
public boolean publishConfig(String group, String serviceId, String config) {
hashOperations.put(group, serviceId, config);
stringRedisTemplate.convertAndSend(group + "-" + serviceId, config);
return true;
}
public void subscribeConfig(String config, RedisSubscribeCallback redisSubscribeCallback) {
redisSubscribeCallback.callback(config);
}
}
\ No newline at end of file
package com.nepxion.discovery.common.redis.operation;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
public interface RedisSubscribeCallback {
void callback(String config);
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-console-extension-redis</artifactId>
<name>Nepxion Discovery Console Extension Redis</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
<url>http://www.nepxion.com</url>
<parent>
<groupId>com.nepxion</groupId>
<artifactId>discovery</artifactId>
<version>4.2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-console</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-common-redis</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.console.extension.redis.adapter;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.redis.operation.RedisOperation;
import com.nepxion.discovery.console.remote.ConfigAdapter;
public class RedisConfigAdapter implements ConfigAdapter {
@Autowired
private RedisOperation redisOperation;
@Override
public boolean updateConfig(String group, String serviceId, String config) throws Exception {
return redisOperation.publishConfig(group, serviceId, config);
}
@Override
public boolean clearConfig(String group, String serviceId) throws Exception {
return redisOperation.removeConfig(group, serviceId);
}
@Override
public String getConfig(String group, String serviceId) throws Exception {
return redisOperation.getConfig(group, serviceId);
}
}
\ No newline at end of file
package com.nepxion.discovery.console.extension.redis.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.console.extension.redis.adapter.RedisConfigAdapter;
import com.nepxion.discovery.console.remote.ConfigAdapter;
@Configuration
public class RedisConfigAutoConfiguration {
@Bean
public ConfigAdapter configAdapter() {
return new RedisConfigAdapter();
}
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.common.redis.configuration.RedisAutoConfiguration,\
com.nepxion.discovery.console.extension.redis.configuration.RedisConfigAutoConfiguration
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-plugin-config-center-extension-redis</artifactId>
<name>Nepxion Discovery Plugin Config Center Extension Redis</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
<url>http://www.nepxion.com</url>
<parent>
<groupId>com.nepxion</groupId>
<artifactId>discovery</artifactId>
<version>4.2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-config-center</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-common-redis</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.redis.adapter;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.redis.operation.RedisOperation;
import com.nepxion.discovery.common.redis.operation.RedisSubscribeCallback;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
public class RedisConfigAdapter extends ConfigAdapter {
private static final Logger LOG = LoggerFactory.getLogger(RedisConfigAdapter.class);
@Autowired
protected PluginContextAware pluginContextAware;
@Autowired
private PluginAdapter pluginAdapter;
@Autowired
private RedisOperation redisOperation;
@Override
public String getConfig() throws Exception {
String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
LOG.info("Get config from Redis server, {}={}, serviceId={}", groupKey, group, serviceId);
return redisOperation.getConfig(group, serviceId);
}
public void subscribeConfig(String config) {
String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
LOG.info("Subscribe config from Redis server, {}={}, serviceId={}", groupKey, group, serviceId);
try {
redisOperation.subscribeConfig(config, new RedisSubscribeCallback() {
@Override
public void callback(String config) {
if (StringUtils.isNotEmpty(config)) {
LOG.info("Get config updated event from Redis server, {}={}, serviceId={}", groupKey, group, serviceId);
RuleEntity ruleEntity = pluginAdapter.getRule();
String rule = ruleEntity.getContent();
if (!StringUtils.equals(rule, config)) {
fireRuleUpdated(new RuleUpdatedEvent(config), true);
} else {
LOG.info("Retrieved config is same as current config, ignore to update, {}={}, serviceId={}", groupKey, group, serviceId);
}
} else {
LOG.info("Get config cleared event from Redis server, {}={}, serviceId={}", groupKey, group, serviceId);
fireRuleCleared(new RuleClearedEvent(), true);
}
}
});
} catch (Exception e) {
LOG.error("Subscribe config failed", e);
}
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.extension.redis.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.configcenter.extension.redis.adapter.RedisConfigAdapter;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@Configuration
public class RedisConfigAutoConfiguration {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Autowired
private PluginAdapter pluginAdapter;
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter messageListenerAdapter) {
String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId();
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(group + "-" + serviceId));
return redisMessageListenerContainer;
}
@Bean
public MessageListenerAdapter messageListenerAdapter(ConfigAdapter configAdapter) {
return new MessageListenerAdapter(configAdapter, "subscribeConfig");
}
@Bean
public ConfigAdapter configAdapter() {
return new RedisConfigAdapter();
}
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.common.redis.configuration.RedisAutoConfiguration,\
com.nepxion.discovery.plugin.configcenter.extension.redis.configuration.RedisConfigAutoConfiguration
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment