apollo集成consul和动态刷新数据源

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,并且

​	[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s8WG6uxc-1578299408616)(C:\Users\VIolet\AppData\Roaming\Typora\typora-user-images\image-20200106160224709.png)]

与改字段对应,如果将fat.meta打开那么数据库需要在dev后面添加一个fat 使用逗号分开

分别启动这三个微服务,启动顺序为config,admin,port,不要使用build.sh打包后启动,访问http://192.168.1.127:8070/在这里插入图片描述

这时候会发现没有了安全框架。

需要修改

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dUrgcX0p-1578299408617)(C:\Users\VIolet\AppData\Roaming\Typora\typora-user-images\image-20200106161645467.png)]

重启port,再次登录你会发现

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SdKy1d1A-1578299408617)(C:\Users\VIolet\AppData\Roaming\Typora\typora-user-images\image-20200106161748858.png)]

登录账号密码 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的配置中指定了读取该空间
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值