構建分佈式配置中心阿波羅(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/。轉載文章時也請大家進行轉載說明,並將文件中餘勝軍鏈接進行引入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章