【移除】Apollo 配置中心,简化学习成本
parent
b3d8c503f5
commit
a8cdf74120
@ -1,37 +0,0 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>yudao-spring-boot-starter-config</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>配置中心,基于 Apollo 魔改实现</description>
|
||||
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Config 配置中心相关 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId> <!-- 引入 Apollo Client 库,实现内嵌的配置中心 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,75 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.apollo.internals;
|
||||
|
||||
import cn.iocoder.yudao.framework.apollo.spi.DBConfigFactory;
|
||||
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
|
||||
import com.ctrip.framework.apollo.internals.*;
|
||||
import com.ctrip.framework.apollo.spi.*;
|
||||
import com.ctrip.framework.apollo.tracer.Tracer;
|
||||
import com.ctrip.framework.apollo.util.ConfigUtil;
|
||||
import com.ctrip.framework.apollo.util.factory.DefaultPropertiesFactory;
|
||||
import com.ctrip.framework.apollo.util.factory.PropertiesFactory;
|
||||
import com.ctrip.framework.apollo.util.http.DefaultHttpClient;
|
||||
import com.ctrip.framework.apollo.util.yaml.YamlParser;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Guice injector
|
||||
*
|
||||
* 基于 Guice 注入器实现类
|
||||
*
|
||||
* @author Jason Song(song_s@ctrip.com)
|
||||
*/
|
||||
public class DefaultXInjector implements Injector {
|
||||
|
||||
private final com.google.inject.Injector m_injector;
|
||||
|
||||
public DefaultXInjector() {
|
||||
try {
|
||||
m_injector = Guice.createInjector(new ApolloModule());
|
||||
} catch (Throwable ex) {
|
||||
ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex);
|
||||
Tracer.logError(exception);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> clazz) {
|
||||
try {
|
||||
return m_injector.getInstance(clazz);
|
||||
} catch (Throwable ex) {
|
||||
Tracer.logError(ex);
|
||||
throw new ApolloConfigException(String.format("Unable to load instance for %s!", clazz.getName()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> clazz, String name) {
|
||||
// Guice does not support get instance by type and name
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ApolloModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(ConfigManager.class).to(DefaultConfigManager.class).in(Singleton.class);
|
||||
bind(ConfigFactoryManager.class).to(DefaultConfigFactoryManager.class).in(Singleton.class);
|
||||
bind(ConfigRegistry.class).to(DefaultConfigRegistry.class).in(Singleton.class);
|
||||
|
||||
// 自定义 ConfigFactory 实现,使用 DB 作为数据源
|
||||
bind(ConfigFactory.class).to(DBConfigFactory.class).in(Singleton.class);
|
||||
|
||||
bind(ConfigUtil.class).in(Singleton.class);
|
||||
bind(DefaultHttpClient.class).in(Singleton.class);
|
||||
bind(ConfigServiceLocator.class).in(Singleton.class);
|
||||
bind(RemoteConfigLongPollService.class).in(Singleton.class);
|
||||
bind(YamlParser.class).in(Singleton.class);
|
||||
bind(PropertiesFactory.class).to(DefaultPropertiesFactory.class).in(Singleton.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.apollo.internals.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 配置 Response DTO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Data
|
||||
public class ConfigRespDTO {
|
||||
|
||||
/**
|
||||
* 参数键名
|
||||
*/
|
||||
private String key;
|
||||
/**
|
||||
* 参数键值
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
private Boolean deleted;
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.apollo.spi;
|
||||
|
||||
import cn.iocoder.yudao.framework.apollo.internals.DBConfigRepository;
|
||||
import com.ctrip.framework.apollo.Config;
|
||||
import com.ctrip.framework.apollo.ConfigFile;
|
||||
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
|
||||
import com.ctrip.framework.apollo.internals.ConfigRepository;
|
||||
import com.ctrip.framework.apollo.internals.DefaultConfig;
|
||||
import com.ctrip.framework.apollo.spi.ConfigFactory;
|
||||
|
||||
/**
|
||||
* 基于 DB 的 ConfigFactory 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class DBConfigFactory implements ConfigFactory {
|
||||
|
||||
@Override
|
||||
public Config create(String namespace) {
|
||||
return new DefaultConfig(namespace, this.createDBConfigRepository(namespace));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
|
||||
throw new UnsupportedOperationException("暂不支持 Apollo 配置文件");
|
||||
}
|
||||
|
||||
private ConfigRepository createDBConfigRepository(String namespace) {
|
||||
return new DBConfigRepository(namespace);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
cn.iocoder.yudao.framework.apollo.internals.DefaultXInjector
|
||||
@ -1,2 +0,0 @@
|
||||
org.springframework.boot.env.EnvironmentPostProcessor=\
|
||||
cn.iocoder.yudao.framework.apollo.spring.boot.ApolloApplicationContextInitializer
|
||||
@ -1,42 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.mysql.config;
|
||||
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.iocoder.yudao.framework.apollo.internals.ConfigFrameworkDAO;
|
||||
import cn.iocoder.yudao.framework.apollo.internals.dto.ConfigRespDTO;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ConfigDAOImpl 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class ConfigDAOImpl implements ConfigFrameworkDAO {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public ConfigDAOImpl(String jdbcUrl, String username, String password) {
|
||||
DataSource dataSource = new DriverManagerDataSource(jdbcUrl, username, password);
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime) {
|
||||
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM infra_config WHERE update_time > ?",
|
||||
Integer.class, maxUpdateTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigRespDTO> selectList() {
|
||||
return jdbcTemplate.query("SELECT config_key, value, update_time, deleted FROM infra_config",
|
||||
(rs, rowNum) -> new ConfigRespDTO().setKey(rs.getString("config_key"))
|
||||
.setValue(rs.getString("value"))
|
||||
.setUpdateTime(LocalDateTimeUtil.of(rs.getDate("update_time")))
|
||||
.setDeleted(rs.getBoolean("deleted")));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.mq.consumer.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.apollo.internals.DBConfigRepository;
|
||||
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
|
||||
import cn.iocoder.yudao.module.infra.mq.message.config.ConfigRefreshMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 针对 {@link ConfigRefreshMessage} 的消费者
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ConfigRefreshConsumer extends AbstractChannelMessageListener<ConfigRefreshMessage> {
|
||||
|
||||
@Override
|
||||
public void onMessage(ConfigRefreshMessage message) {
|
||||
log.info("[onMessage][收到 Config 刷新消息]");
|
||||
DBConfigRepository.noticeSync();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.mq.message.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 配置数据刷新 Message
|
||||
*/
|
||||
@Data
|
||||
public class ConfigRefreshMessage extends AbstractChannelMessage {
|
||||
|
||||
@Override
|
||||
public String getChannel() {
|
||||
return "infra.config.refresh";
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.mq.producer.config;
|
||||
|
||||
import cn.iocoder.yudao.module.infra.mq.message.config.ConfigRefreshMessage;
|
||||
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Config 配置相关消息的 Producer
|
||||
*/
|
||||
@Component
|
||||
public class ConfigProducer {
|
||||
|
||||
@Resource
|
||||
private RedisMQTemplate redisMQTemplate;
|
||||
|
||||
/**
|
||||
* 发送 {@link ConfigRefreshMessage} 消息
|
||||
*/
|
||||
public void sendConfigRefreshMessage() {
|
||||
ConfigRefreshMessage message = new ConfigRefreshMessage();
|
||||
redisMQTemplate.send(message);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue