1.apollo集成consul
Apollo 本地安装所需要的环境
Java版本要求 1.8+ ;
Mysql版本要求 5.6.5+
consul注册中心
代码拉取
这里借用一下大佬的阿云仓库,已经将配置中心修改成了consul
集群环境
Apollo目前支持以下环境:
- DEV >开发环境
- FAT > 测试环境,相当于alpha环境(功能测试)
- UAT > 集成环境,相当于beta环境(回归测试)
- PRO > 生产环境
部署思路
每一套环境都部署一套(apollo-configservice+apollo-adminservice+configdb数据库表)
前台页面仅需要部署一套(apollo-portal+portaldb数据库表)即可集群管理多套环境
注:同一个环境的apollo服务的多个节点使用同一个数据库,不同环境的apollo集群使用不同的数据库,多个不同环境的apollo集群使用一个portal就可以管理。
创建,修改数据库连接(需要对应修改三个服务的数据库连接,这里只写一个)
portal的数据库连接
spring.datasource.url=jdbc:mysql://192.168.1.127:3306/Apolloportaldb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
configservice,adminservice 的数据库连接
spring.datasource.url = jdbc:mysql://192.168.1.127:3306/ApolloConfigDB?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
注意事项
portal的地址指向configservice,并且
与改字段对应,如果将fat.meta打开那么数据库需要在dev后面添加一个fat 使用逗号分开
分别启动这三个微服务,启动顺序为config,admin,port,不要使用build.sh打包后启动,访问http://192.168.1.127:8070/![在这里插入图片描述](https://img-blog.csdnimg.cn/20200106163242862.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1Zpb2xldF8yMDE5MDMwMjc=,size_16,color_FFFFFF,t_70)
这时候会发现没有了安全框架。
需要修改
重启port,再次登录你会发现
登录账号密码 apollo admin
使用springcloud项目动态刷新数据源
需要添加的依赖和测试的Controller
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo.client.version}</version>
</dependency>
package com.yango.mgt.api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestApi {
@Value("${test}")
private String categoryFile;
@RequestMapping(value = "list", method = RequestMethod.GET)
public String updateMgtUserInfoGroup() {
return categoryFile;
}
}
配置文件添加
app:
id: test #apollo识别项目的该id后会分配到对应的配置文件中去
apollo:
meta: http://192.168.1.127:1600 #指定需要获取配置文件的cpnfigsevice的地址,端口
bootstrap:
enabled: true
namespaces: datasource,application #读取的appid的namespace空间,默认读取application,这个地方是为了后面动态刷新数据源使用
然后启动项目会发现,这是在向configserver请求,但是你会发现会报错,原因是 @Value("${test}")在配置文件中找不到,所以需要在apollo中进行相应的配置,然后启动demo
在apollo中进行配置,应用id与你的appid相同
``
在该网页增加一个配置,这里的key与demo里面的Value的${}的key要相同,然后提交,发布,启动demo的项目
请求结果
这里已经搭建完成了,可以试着修改这个值,发布后生效然后请求改地址会发现,值得变化
添加一个动态修改数据源
package com.yango.mgt.config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Slf4j
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfig {
@Autowired
ApplicationContext context;
@Autowired
private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;
@ApolloConfigChangeListener("dataSource")
private void onChange(ConfigChangeEvent changeEvent) {
DataSourceProperties dataSourceProperties = context.getBean(DataSourceProperties.class);
changeEvent.changedKeys().stream().forEach(s -> {
if (s.contains("spring.datasource.password")) {
dataSourceProperties.setPassword(changeEvent.getChange(s).getNewValue());
}
});
boolean dataSource = refreshScope.refresh("dataSource");
System.out.println(dataSource);
}
@RefreshScope
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
System.out.println(1);
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
当项目中出现,2个数据源时,改代码无效,ApolloConfigChangeListener中的dataSource指定的是namespace里面的,需要直接创建
然后项目启动时,会读取该namespace空间。一定要在前面的port的配置中指定了读取该空间