构建分布式配置中心阿波罗(Apollo)

说明:本人书写该篇博客原因主要有两个:一、方便本人查阅,二、为全小白且想学微服务的朋友进行查阅。以下内容主要来源于余胜军,本人在他基础上将步骤进行细化,使小白也能看懂,请大家在转载的时候也引入余胜军的链接

1. 构建分布式配置中心阿

1.1. 搭建分布式配置中心阿

1.1.1. 下载aploll配置中心

 https://github.com/nobodyiam/apollo-build-scripts

1.1.2. 使用sz命令上传配置中心文件

上传apollo-build-scripts-master文件到服务器中

1.1.3.  解压配置中心文件

unzip apollo-build-scripts-master.zip 解压配置文件

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

1.1.4. 将解压出的数据库脚本导入数据库中

1.1.5. 配置数据策略

修改demo.sh文件

切记,输入数据库名称的时候一定要区分大小写

1.1.6.  启动阿波罗

./demo.sh start

1.1.7. 关闭防火墙

systemctl stop firewalld.service  

1.1.8. 阿波罗默认账号密码

Apollo  admin

1.1.9. 阿波罗其他配置中心介绍

https://github.com/ctripcorp/apollo/wiki/Apollo%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E4%BB%8B%E7%BB%8D

1.2. yml文件配置发布到阿波罗分布式配置中心

以下以微信服务的配置文件放入阿波罗配置中心为例

1.2.1. 在阿波罗平台中创建项目

1.2.2. 新增配置方法-表格形式

以表格形式添加,以下文章主要以第二种文本形式介绍:

1.2.3. 新增配置方法-文本形式

使用文本方式新增,直接粘贴properties格式的配置文件

1.2.4. 在线将yml文件转成properties文件

进入网站:https://www.toyaml.com/index.html

切记,将eureka的地址要改为Apolloeureka地址,即http://localhost:8100改为:http://192.168.1.151:8080

然后发布配置

同理,将member服务的配置文件也发布到Apollo配置中心上。

1.3. 服务客户端集成配置文件

1.3.1. 引入Maven依赖

将依赖放入到服务impl的pom文件中

<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>

  

1.3.2. 更改项目配置

yml配置文件删除,并增加application.properties配置文件

1.3.4. 在启动类上加注解

@EnableApolloConfig

以上即配置完成

1.4. 网关集成阿波罗

@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;
		}

	}

}

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

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

项目启动监听

@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());
			}
		});
	}

}

  以上配置中也有部分方案代码省略。对微服务感兴趣的朋友可以进行关注讨论,本人博客地址为:https://www.cnblogs.com/chenyuanbo/。转载文章时也请大家进行转载说明,并将文件中余胜军链接进行引入。

猜你喜欢

转载自www.cnblogs.com/chenyuanbo/p/12183369.html
今日推荐