Commit c4c614592a4ae457923f9a6425b41b207077dc78
1 parent
6bed7778
实现内嵌的 apollo 的配置中心的支持
Showing
13 changed files
with
220 additions
and
6 deletions
pom.xml
| @@ -167,6 +167,13 @@ | @@ -167,6 +167,13 @@ | ||
| 167 | <version>${redisson.version}</version> | 167 | <version>${redisson.version}</version> |
| 168 | </dependency> | 168 | </dependency> |
| 169 | 169 | ||
| 170 | + <!-- --> | ||
| 171 | + <dependency> | ||
| 172 | + <groupId>com.ctrip.framework.apollo</groupId> | ||
| 173 | + <artifactId>apollo-client</artifactId> | ||
| 174 | + <version>1.7.0</version> | ||
| 175 | + </dependency> | ||
| 176 | + | ||
| 170 | <!-- 监控相关 --> | 177 | <!-- 监控相关 --> |
| 171 | <dependency> | 178 | <dependency> |
| 172 | <groupId>org.apache.skywalking</groupId> | 179 | <groupId>org.apache.skywalking</groupId> |
src/main/java/cn/iocoder/dashboard/framework/apollo/internals/DBConfigRepository.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.apollo.internals; | ||
| 2 | + | ||
| 3 | +import com.ctrip.framework.apollo.Apollo; | ||
| 4 | +import com.ctrip.framework.apollo.build.ApolloInjector; | ||
| 5 | +import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; | ||
| 6 | +import com.ctrip.framework.apollo.enums.ConfigSourceType; | ||
| 7 | +import com.ctrip.framework.apollo.internals.AbstractConfigRepository; | ||
| 8 | +import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
| 9 | +import com.ctrip.framework.apollo.tracer.Tracer; | ||
| 10 | +import com.ctrip.framework.apollo.util.ConfigUtil; | ||
| 11 | +import lombok.extern.slf4j.Slf4j; | ||
| 12 | + | ||
| 13 | +import java.util.Properties; | ||
| 14 | +import java.util.concurrent.Executors; | ||
| 15 | +import java.util.concurrent.ScheduledExecutorService; | ||
| 16 | +import java.util.concurrent.atomic.AtomicInteger; | ||
| 17 | +import java.util.concurrent.atomic.AtomicReference; | ||
| 18 | + | ||
| 19 | +@Slf4j | ||
| 20 | +public class DBConfigRepository extends AbstractConfigRepository { | ||
| 21 | + | ||
| 22 | + private final static ScheduledExecutorService m_executorService; | ||
| 23 | + | ||
| 24 | + static { | ||
| 25 | + m_executorService = Executors.newScheduledThreadPool(1, | ||
| 26 | + ApolloThreadFactory.create(DBConfigRepository.class.getSimpleName(), true)); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + private final ConfigUtil m_configUtil; | ||
| 30 | + | ||
| 31 | + private final AtomicReference<Properties> m_configCache; | ||
| 32 | + private final String m_namespace; | ||
| 33 | + | ||
| 34 | + public DBConfigRepository(String namespace) { | ||
| 35 | + // 初始化变量 | ||
| 36 | + this.m_namespace = namespace; | ||
| 37 | + m_configCache = new AtomicReference<>(); | ||
| 38 | + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); | ||
| 39 | + | ||
| 40 | + // 初始化加载 | ||
| 41 | + this.trySync(); | ||
| 42 | + // 初始化定时任务 | ||
| 43 | + this.schedulePeriodicRefresh(); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + private AtomicInteger index = new AtomicInteger(); | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + protected void sync() { | ||
| 50 | + System.out.println("我同步啦"); | ||
| 51 | + | ||
| 52 | + index.incrementAndGet(); | ||
| 53 | + Properties properties = new Properties(); | ||
| 54 | + properties.setProperty("demo.test", String.valueOf(index.get())); | ||
| 55 | + m_configCache.set(properties); | ||
| 56 | + super.fireRepositoryChange(m_namespace, properties); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @Override | ||
| 60 | + public Properties getConfig() { | ||
| 61 | + // 兜底,避免可能存在配置为 null 的情况 | ||
| 62 | + if (m_configCache.get() == null) { | ||
| 63 | + this.trySync(); | ||
| 64 | + } | ||
| 65 | + // 返回配置 | ||
| 66 | + return m_configCache.get(); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) { | ||
| 71 | + // 啥事不做 | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + @Override | ||
| 75 | + public ConfigSourceType getSourceType() { | ||
| 76 | + return ConfigSourceType.REMOTE; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + private void schedulePeriodicRefresh() { | ||
| 80 | + log.debug("Schedule periodic refresh with interval: {} {}", | ||
| 81 | + m_configUtil.getRefreshInterval(), m_configUtil.getRefreshIntervalTimeUnit()); | ||
| 82 | + m_executorService.scheduleAtFixedRate(() -> { | ||
| 83 | + Tracer.logEvent("Apollo.ConfigService", String.format("periodicRefresh: %s", m_namespace)); | ||
| 84 | + log.debug("refresh config for namespace: {}", m_namespace); | ||
| 85 | + | ||
| 86 | + // 执行同步 | ||
| 87 | + trySync(); | ||
| 88 | + | ||
| 89 | + Tracer.logEvent("Apollo.Client.Version", Apollo.VERSION); | ||
| 90 | + }, m_configUtil.getRefreshInterval(), m_configUtil.getRefreshInterval(), | ||
| 91 | + m_configUtil.getRefreshIntervalTimeUnit()); | ||
| 92 | +// TimeUnit.SECONDS); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | +} |
src/main/java/cn/iocoder/dashboard/framework/apollo/internals/DefaultXInjector.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.apollo.internals; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.dashboard.framework.apollo.spi.DBConfigFactory; | ||
| 4 | +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; | ||
| 5 | +import com.ctrip.framework.apollo.internals.*; | ||
| 6 | +import com.ctrip.framework.apollo.spi.*; | ||
| 7 | +import com.ctrip.framework.apollo.tracer.Tracer; | ||
| 8 | +import com.ctrip.framework.apollo.util.ConfigUtil; | ||
| 9 | +import com.ctrip.framework.apollo.util.factory.DefaultPropertiesFactory; | ||
| 10 | +import com.ctrip.framework.apollo.util.factory.PropertiesFactory; | ||
| 11 | +import com.ctrip.framework.apollo.util.http.HttpUtil; | ||
| 12 | +import com.ctrip.framework.apollo.util.yaml.YamlParser; | ||
| 13 | +import com.google.inject.AbstractModule; | ||
| 14 | +import com.google.inject.Guice; | ||
| 15 | +import com.google.inject.Singleton; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * Guice injector | ||
| 19 | + * | ||
| 20 | + * 基于 Guice 注入器实现类 | ||
| 21 | + * | ||
| 22 | + * @author Jason Song(song_s@ctrip.com) | ||
| 23 | + */ | ||
| 24 | +public class DefaultXInjector implements Injector { | ||
| 25 | + | ||
| 26 | + private final com.google.inject.Injector m_injector; | ||
| 27 | + | ||
| 28 | + public DefaultXInjector() { | ||
| 29 | + try { | ||
| 30 | + m_injector = Guice.createInjector(new ApolloModule()); | ||
| 31 | + } catch (Throwable ex) { | ||
| 32 | + ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex); | ||
| 33 | + Tracer.logError(exception); | ||
| 34 | + throw exception; | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + public <T> T getInstance(Class<T> clazz) { | ||
| 40 | + try { | ||
| 41 | + return m_injector.getInstance(clazz); | ||
| 42 | + } catch (Throwable ex) { | ||
| 43 | + Tracer.logError(ex); | ||
| 44 | + throw new ApolloConfigException(String.format("Unable to load instance for %s!", clazz.getName()), ex); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public <T> T getInstance(Class<T> clazz, String name) { | ||
| 50 | + // Guice does not support get instance by type and name | ||
| 51 | + return null; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + private static class ApolloModule extends AbstractModule { | ||
| 55 | + | ||
| 56 | + @Override | ||
| 57 | + protected void configure() { | ||
| 58 | + bind(ConfigManager.class).to(DefaultConfigManager.class).in(Singleton.class); | ||
| 59 | + bind(ConfigFactoryManager.class).to(DefaultConfigFactoryManager.class).in(Singleton.class); | ||
| 60 | + bind(ConfigRegistry.class).to(DefaultConfigRegistry.class).in(Singleton.class); | ||
| 61 | + | ||
| 62 | + // 自定义 ConfigFactory 实现,使用 DB 作为数据源 | ||
| 63 | + bind(ConfigFactory.class).to(DBConfigFactory.class).in(Singleton.class); | ||
| 64 | + | ||
| 65 | + bind(ConfigUtil.class).in(Singleton.class); | ||
| 66 | + bind(HttpUtil.class).in(Singleton.class); | ||
| 67 | + bind(ConfigServiceLocator.class).in(Singleton.class); | ||
| 68 | + bind(RemoteConfigLongPollService.class).in(Singleton.class); | ||
| 69 | + bind(YamlParser.class).in(Singleton.class); | ||
| 70 | + bind(PropertiesFactory.class).to(DefaultPropertiesFactory.class).in(Singleton.class); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | +} |
src/main/java/cn/iocoder/dashboard/framework/apollo/package-info.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.apollo; |
src/main/java/cn/iocoder/dashboard/framework/apollo/spi/DBConfigFactory.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.apollo.spi; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.dashboard.framework.apollo.internals.DBConfigRepository; | ||
| 4 | +import com.ctrip.framework.apollo.Config; | ||
| 5 | +import com.ctrip.framework.apollo.ConfigFile; | ||
| 6 | +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; | ||
| 7 | +import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
| 8 | +import com.ctrip.framework.apollo.internals.DefaultConfig; | ||
| 9 | +import com.ctrip.framework.apollo.spi.ConfigFactory; | ||
| 10 | + | ||
| 11 | +public class DBConfigFactory implements ConfigFactory { | ||
| 12 | + | ||
| 13 | + @Override | ||
| 14 | + public Config create(String namespace) { | ||
| 15 | + return new DefaultConfig(namespace, this.createDBConfigRepository(namespace)); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) { | ||
| 20 | + throw new UnsupportedOperationException("暂不支持 Apollo 配置文件"); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + private ConfigRepository createDBConfigRepository(String namespace) { | ||
| 24 | + return new DBConfigRepository(namespace); // TODO 芋艿:看看怎么优化 | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | +} |
src/main/java/cn/iocoder/dashboard/modules/system/controller/config/SysConfigController.java
| @@ -28,7 +28,7 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.CO | @@ -28,7 +28,7 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.CO | ||
| 28 | @RequestMapping("/system/config") | 28 | @RequestMapping("/system/config") |
| 29 | public class SysConfigController { | 29 | public class SysConfigController { |
| 30 | 30 | ||
| 31 | - @Value("${demo.test:false}") | 31 | + @Value("${demo.test}") |
| 32 | private String demo; | 32 | private String demo; |
| 33 | 33 | ||
| 34 | @GetMapping("/demo") | 34 | @GetMapping("/demo") |
src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java
| @@ -8,8 +8,8 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRo | @@ -8,8 +8,8 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRo | ||
| 8 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; | 8 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| 9 | import com.baomidou.mybatisplus.core.metadata.IPage; | 9 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 10 | import org.apache.ibatis.annotations.Mapper; | 10 | import org.apache.ibatis.annotations.Mapper; |
| 11 | +import org.springframework.lang.Nullable; | ||
| 11 | 12 | ||
| 12 | -import javax.annotation.Nullable; | ||
| 13 | import java.util.Collection; | 13 | import java.util.Collection; |
| 14 | import java.util.List; | 14 | import java.util.List; |
| 15 | 15 |
src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysPostService.java
| @@ -6,8 +6,8 @@ import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostExport | @@ -6,8 +6,8 @@ import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostExport | ||
| 6 | import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostPageReqVO; | 6 | import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostPageReqVO; |
| 7 | import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostUpdateReqVO; | 7 | import cn.iocoder.dashboard.modules.system.controller.dept.vo.post.SysPostUpdateReqVO; |
| 8 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysPostDO; | 8 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysPostDO; |
| 9 | +import org.springframework.lang.Nullable; | ||
| 9 | 10 | ||
| 10 | -import javax.annotation.Nullable; | ||
| 11 | import java.util.Collection; | 11 | import java.util.Collection; |
| 12 | import java.util.List; | 12 | import java.util.List; |
| 13 | 13 |
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java
| 1 | package cn.iocoder.dashboard.modules.system.service.permission; | 1 | package cn.iocoder.dashboard.modules.system.service.permission; |
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; | 3 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; |
| 4 | +import org.springframework.lang.Nullable; | ||
| 4 | 5 | ||
| 5 | -import javax.annotation.Nullable; | ||
| 6 | import java.util.Collection; | 6 | import java.util.Collection; |
| 7 | import java.util.List; | 7 | import java.util.List; |
| 8 | import java.util.Set; | 8 | import java.util.Set; |
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java
| @@ -6,8 +6,8 @@ import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRole | @@ -6,8 +6,8 @@ import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRole | ||
| 6 | import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; | 6 | import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; |
| 7 | import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; | 7 | import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; |
| 8 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; | 8 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; |
| 9 | +import org.springframework.lang.Nullable; | ||
| 9 | 10 | ||
| 10 | -import javax.annotation.Nullable; | ||
| 11 | import java.util.Collection; | 11 | import java.util.Collection; |
| 12 | import java.util.List; | 12 | import java.util.List; |
| 13 | import java.util.Set; | 13 | import java.util.Set; |
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
| @@ -18,11 +18,11 @@ import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; | @@ -18,11 +18,11 @@ import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; | ||
| 18 | import com.baomidou.mybatisplus.core.metadata.IPage; | 18 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 19 | import com.google.common.collect.ImmutableMap; | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import lombok.extern.slf4j.Slf4j; | 20 | import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.springframework.lang.Nullable; | ||
| 21 | import org.springframework.stereotype.Service; | 22 | import org.springframework.stereotype.Service; |
| 22 | import org.springframework.transaction.annotation.Transactional; | 23 | import org.springframework.transaction.annotation.Transactional; |
| 23 | import org.springframework.util.StringUtils; | 24 | import org.springframework.util.StringUtils; |
| 24 | 25 | ||
| 25 | -import javax.annotation.Nullable; | ||
| 26 | import javax.annotation.PostConstruct; | 26 | import javax.annotation.PostConstruct; |
| 27 | import javax.annotation.Resource; | 27 | import javax.annotation.Resource; |
| 28 | import java.util.*; | 28 | import java.util.*; |
src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector
0 → 100644
| 1 | +cn.iocoder.dashboard.framework.apollo.internals.DefaultXInjector |
src/main/resources/application.yaml
| @@ -47,6 +47,14 @@ yudao: | @@ -47,6 +47,14 @@ yudao: | ||
| 47 | file: | 47 | file: |
| 48 | base-path: http://127.0.0.1:1024/api/file/get/ | 48 | base-path: http://127.0.0.1:1024/api/file/get/ |
| 49 | 49 | ||
| 50 | +# Apollo 配置中心 | ||
| 51 | +apollo: | ||
| 52 | + bootstrap: | ||
| 53 | + enabled: true | ||
| 54 | + eagerLoad: | ||
| 55 | + enabled: true | ||
| 56 | + autoUpdateInjectedSpringProperties: true | ||
| 57 | + | ||
| 50 | # MyBatis Plus 的配置项 | 58 | # MyBatis Plus 的配置项 |
| 51 | mybatis-plus: | 59 | mybatis-plus: |
| 52 | configuration: | 60 | configuration: |