1.Springcloud的分布式配置中心组件
组件一般有两个,springcloud config,apollo。apollo有一个好处是apollo有界面,这是apollo的一个优势。
springcloud config将配置文件存放在Git上,而apollo将配置文件存放在数据库中。
阿波罗apollo官方服务器演示http://106.12.25.204:8070/
2.各模块概要介绍
https://github.com/ctripcorp/apollo/wiki/Apollo配置中心设计
基础模型:
- 用户在配置中心对配置进行修改并发布
- 配置中心通知Apollo客户端有配置更新
- Apollo客户端从配置中心拉取最新的配置、更新本地配置并通知到应用
架构概览:
- Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
- Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
- Config Service和Admin Service都是多实例、无状态部署,所以需要将自己注册到Eureka中并保持心跳
- 在Eureka之上我们架了一层Meta Server用于封装Eureka的服务发现接口
- Client通过域名访问Meta Server获取Config Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Client侧会做load balance、错误重试
- Portal通过域名访问Meta Server获取Admin Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Portal侧会做load balance、错误重试
- 为了简化部署,我们实际上会把Config Service、Eureka和Meta Server三个逻辑角色部署在同一个JVM进程中
配置发布过程:
- 用户在Portal操作配置发布
- Portal调用Admin Service的接口操作发布
- Admin Service发布配置后,发送ReleaseMessage给各个Config Service
- Config Service收到ReleaseMessage后,通知对应的客户端
2.1 Config Service
- 提供配置获取接口
- 提供配置更新推送接口(基于Http long polling)
- 服务端使用Spring DeferredResult实现异步化,从而大大增加长连接数量
- 目前使用的tomcat embed默认配置是最多10000个连接(可以调整),使用了4C8G的虚拟机实测可以支撑10000个连接,所以满足需求(一个应用实例只会发起一个长连接)。
- 接口服务对象为Apollo客户端
2.2 Admin Service
- 提供配置管理接口
- 提供配置修改、发布等接口
- 接口服务对象为Portal
2.3 Meta Server
- Portal通过域名访问Meta Server获取Admin Service服务列表(IP+Port)
- Client通过域名访问Meta Server获取Config Service服务列表(IP+Port)
- Meta Server从Eureka获取Config Service和Admin Service的服务信息,相当于是一个Eureka Client
- 增设一个Meta Server的角色主要是为了封装服务发现的细节,对Portal和Client而言,永远通过一个Http接口获取Admin Service和Config Service的服务信息,而不需要关心背后实际的服务注册和发现组件
- Meta Server只是一个逻辑角色,在部署时和Config Service是在一个JVM进程中的,所以IP、端口和Config Service一致
2.4 Eureka
- 基于Eureka和Spring Cloud Netflix提供服务注册和发现
- Config Service和Admin Service会向Eureka注册服务,并保持心跳
- 为了简单起见,目前Eureka在部署时和Config Service是在一个JVM进程中的(通过Spring Cloud Netflix)
2.5 Portal
- 提供Web界面供用户管理配置
- 通过Meta Server获取Admin Service服务列表(IP+Port),通过IP+Port访问服务
- 在Portal侧做load balance、错误重试
2.6 Client
- Apollo提供的客户端程序,为应用提供配置获取、实时更新等功能
- 通过Meta Server获取Config Service服务列表(IP+Port),通过IP+Port访问服务
- 在Client侧做load balance、错误重试
3.apollo配置中心搭建
创建mysql及表结构,目前唯一的外部依赖是MySQL
安装java环境
参考此文章即可https://mp.csdn.net/postedit/83781047
下载apollo配置中心 并进行上传到服务器中,解压配置文件
https://github.com/nobodyiam/apollo-build-scripts
//如果没有unzip命令的话,安装zip插件
cd ..
ls
cd usr/
ls
yum -y install zip unzip
unzip apollo-build-scripts-master.zip
配置数据策略,修改demo.sh账号,修改后从本地通过xftp上传到文件夹中
cd apollo-build-scripts-master
ls
启动阿波罗
//启动apollo
./demo.sh start
//关闭防火墙
systemctl stop firewalld.service
访问apollo:http://134.175.193.156:8070 默认账号:apollo admin
apollo自带erueka:http://134.175.193.156:8080
4.apollo创建项目并进行配置文件上传及调用
environment(4种环境)
- DEV(开发环境)
- FAT(功能测试)
- UAT(验收测试)
- PRO(生产环境)
创建项目
不同的项目通过appid进行区分
上传配置文件并发布
apollo默认的文件格式是application.properties。我们在项目中的配置文件格式如果是yml的需要进行转换,通过https://www.toyaml.com/index.html
application.yml
###服务启动端口号
server:
port: 8200
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-tx-weixin
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
####swagger相关配置
swagger:
base-package: com.tx.serviceweixin.impl
title: SpringCloud2.x构建微服务电商项目-微信服务接口
description: 该项目“基于SpringCloud2.x构建微服务电商项目”未经过允许的情况下,私自分享视频和源码属于违法行为。
version: 1.1
terms-of-service-url: www.tx.com
contact:
name: sunny
email: 860074898@qq.com
在工程对应的项目中添加Maven依赖
<!--阿波罗整理springboot-->
<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>
在配置文件中application.properties
阿波罗配置文件一般都是在内网内访问,不需要访问密码这些设置过于繁琐,用户密码一般用于外网访问
app.id=dhcc-shop-service-weixin
apollo.meta=http://134.175.193.156:8080
在应用启动类上增加启动阿波罗配置的注解
@EnableApolloConfig
不是所有的配置在阿波罗修改后会立即生效。
删掉原有的application.yml,启动服务,配置文件从阿波罗服务器中进行加载
5.动态监听从apollo服务器获取配置文件-整合zull网关
添加配置,主要关注tx.zuul.swaggerDocument这个key
server.port = 8000
spring.application.name = app-tx-zuul
eureka.client.service-url.defaultZone = http://106.12.25.204:8080/eureka
zuul.routes.api-a.path = /api-weixin/**
zuul.routes.api-a.serviceId = app-tx-weixin
zuul.routes.api-b.path = /api-member/**
zuul.routes.api-b.serviceId = app-tx-member
tx.zuul.swaggerDocument = [\n {\n "name": "app-tx-member",\n "location": "/app-tx-member/v2/api-docs",\n "version": "2.0"\n },\n {\n "name": "app-tx-weixin",\n "location": "/app-tx-weixin/v2/api-docs",\n "version": "2.0"\n }\n]
增加配置
##使用携程阿波罗示例的服务端正常
app.id = 1000000003
apollo.meta = http://106.12.25.204:8080/
##腾讯云,阿里云服务器内外网是映射关系,得固定IP才可以解决此问题
#app.id = 8000000001
#apollo.meta = http://39.100.106.209:8080/
# will inject 'application' namespace in bootstrap phase
apollo.bootstrap.enabled = true
# put apollo initialization before logging system initialization
apollo.bootstrap.eagerLoad.enabled=true
添加依赖
<!--阿波罗整理springboot-->
<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>
增加注解
@EnableApolloConfig
动态获取
package com.tx.zuul;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
/***
* @Author Sunny
* @Description //TODO Zuul微服务网关服务
* @Date 11:14 2019/9/17
* @Param
* @return
*/
@SpringBootApplication
@EnableSwagger2Doc
@EnableEurekaClient
@EnableZuulProxy
@EnableApolloConfig
public class DhccShopBasicsSpringcloudZuulApplication {
// 获取ApolloConfig
@ApolloConfig
private Config appConfig;
public static void main(String[] args) {
SpringApplication.run(DhccShopBasicsSpringcloudZuulApplication.class, args);
}
// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
// 访问swagger-ui页面每次都会访问get方法
@SuppressWarnings("rawtypes")
@Override
public List<SwaggerResource> get() {
// 开启监听,配置文件发生改变需要更改
appConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent){
get();
}
});
return resources();
// List resources = new ArrayList();
// // app-itmayiedu-order
// // 网关使用服务别名获取远程服务的SwaggerApi
// resources.add(swaggerResource("app-tx-member", "/app-tx-member/v2/api-docs", "2.0"));
// resources.add(swaggerResource("app-tx-weixin", "/app-tx-weixin/v2/api-docs", "2.0"));
// return resources;
}
// 从阿波罗服务器中获取resources
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() {
//tx.zuul.swaggerDocument为阿波罗配置文件中的key值,通过下面获取value,如果没有获取到指定一个默认值
String property = appConfig.getProperty("tx.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;
}
}
}
运行测试