三、搭建分布式配置中心阿波罗

目录

1、下载安装

2、服务客户端集成配置文件

2.1 将本地配置文件配置到阿波罗

2.2 引入依赖

2.3 配置文件application.properties

2.4 网关项目开启阿波罗@EnableApolloConfig

2.5 自定义Swagger文档配置mayikt.zuul.swaggerDocument


1、下载安装

  1. 下载aploll配置中心 https://github.com/nobodyiam/apollo-build-scripts
  2. 上传apollo-build-scripts-master文件到服务器中
  3.  unzip apollo-build-scripts-master.zip 解压配置文件

如果没有unzip命令的话,安装zip插件 yum -y install zip unzip

配置数据策略

修改demo.sh账号:cdb-8y8qmojr.gz.tencentcdb.com:10010 root Ww861642669+

启动阿波罗 ./demo.sh start

systemctl stop firewalld.service  

默认账号密码 Apollo  admin

2、服务客户端集成配置文件

2.1 将本地配置文件配置到阿波罗

转换地址:http://www.toyaml.com/index.html

2.2 引入依赖

<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-client</artifactId>
			<version>1.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-core</artifactId>
			<version>1.0.0</version>
		</dependency>

2.3 配置文件application.properties

app.id=mayikt644064779
apollo.meta=http://192.168.212.236:8080

2.4 网关项目开启阿波罗@EnableApolloConfig

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@EnableApolloConfig
public class AppGateWay {

	// 获取ApolloConfig
	@ApolloConfig
	private Config appConfig;

	public static void main(String[] args) {
		SpringApplication.run(AppGateWay.class, args);
	}

	// 添加文档来源
	@Component
	@Primary
	class DocumentationConfig implements SwaggerResourcesProvider {
		@Override
		public List<SwaggerResource> get() {
			// 开启监听,配置文件发生改变需要更改
			appConfig.addChangeListener(new ConfigChangeListener() {

				@Override
				public void onChange(ConfigChangeEvent changeEvent) {
					get();
				}
			});
			return resources();
		}

		/**
		 * 从阿波罗服务器中获取resources
		 * 
		 * @return
		 */
		private List<SwaggerResource> resources() {

			List resources = new ArrayList<>();
			// app-itmayiedu-order
			// 网关使用服务别名获取远程服务的SwaggerApi
			String swaggerDocJson = swaggerDocument();
			JSONArray jsonArray = JSONArray.parseArray(swaggerDocJson);
			for (Object object : jsonArray) {
				JSONObject jsonObject = (JSONObject) object;
				String name = jsonObject.getString("name");
				String location = jsonObject.getString("location");
				String version = jsonObject.getString("version");
				resources.add(swaggerResource(name, location, version));
			}
			return resources;
		}

		/**
		 * 获取swaggerDocument配置
		 * 
		 * @return
		 */
		private String swaggerDocument() {
			String property = appConfig.getProperty("mayikt.zuul.swaggerDocument", "");
			return property;
		}

		private SwaggerResource swaggerResource(String name, String location, String version) {
			SwaggerResource swaggerResource = new SwaggerResource();
			swaggerResource.setName(name);
			swaggerResource.setLocation(location);
			swaggerResource.setSwaggerVersion(version);
			return swaggerResource;
		}

	}

}

2.5 自定义Swagger文档配置mayikt.zuul.swaggerDocument

[
    {
        "name": "app-mayikt-member",
        "location": "/app-mayikt-member/v2/api-docs",
        "version": "2.0"
    },
    {
        "name": "app-mayikt-weixin",
        "location": "/app-mayikt-weixin/v2/api-docs",
        "version": "2.0"
    }
]

每个项目通过appid来区分配置

监听配置:

@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
	@ApolloConfig
	private Config config;

	@Override
	public void run(String... args) throws Exception {
		config.addChangeListener(new ConfigChangeListener() {

			@Override
			public void onChange(ConfigChangeEvent changeEvent) {
				log.debug("####分布式配置中心监听#####" + changeEvent.changedKeys().toString());
			}
		});
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值