您的位置:首页 > 其它

Apollo配置中心改造二:注册中心Eureka替换为zookeeper

2019-05-07 10:08 1046 查看
改造背景:

许多公司微服务项目已经在使用zookeeper,为了方便服务管理,替换为zookeeper比较利于运维团队管理统一的注册中心。

解决:

按照本文如下五步对apollo配置中心改造,即可实现apollo注册中心替换为zookeeper。

注:保留原有组件之间调用关系,只是简单将原有去Eureka注册发现服务换为了Zookeeper,原有metaservice模块继续用作apollo内部组件服务注册发现,有时间可以自行将各个模块作为zk客户端改造下代码(只需将ServiceController类下放到各个模块查找服务即可)。
本文仅为zk服务端运维能监控和管理apollo组件。

1、最外层pom.xml改造

去除

spring-cloud-starter-netflix-eureka-client
相关依赖
添加zk相关依赖,让三个组件均注册到zk:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-all</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
2、apollo-biz改造

修改点一:

pom.xml
文件去除
spring-cloud-starter-netflix-eureka-client
相关依赖
修改点二:
删除此类
com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig

3、apollo-configservice改造

修改点一:
pom文件去除

spring-cloud-starter-netflix-eureka-server
相关依赖(zk服务端管理服务)
修改点二:
com.ctrip.framework.apollo.metaservice.service.DiscoveryService#getConfigServiceInstances()

出入参不变,方法体替换为

List<ServiceInstance> application = zookeeperDiscoveryClient.getInstances(ServiceNameConsts.APOLLO_CONFIGSERVICE);
if (application == null) {
Tracer.logEvent("Apollo.ZkDiscovery.NotFound", ServiceNameConsts.APOLLO_CONFIGSERVICE);
}
return application != null ? application : Collections.emptyList();

修改点三:

com.ctrip.framework.apollo.metaservice.service.DiscoveryService#getMetaServiceInstances()

出入参不变,方法体替换为:

List<ServiceInstance> application = zookeeperDiscoveryClient.getInstances(ServiceNameConsts.APOLLO_METASERVICE);
if (application == null) {
Tracer.logEvent("Apollo.ZkDiscovery.NotFound", ServiceNameConsts.APOLLO_METASERVICE);
}
return application != null ? application : Collections.emptyList();

修改点四:

com.ctrip.framework.apollo.metaservice.service.DiscoveryService#getAdminServiceInstances()

出入参不变,方法体替换为:

List<ServiceInstance> application = zookeeperDiscoveryClient.getInstances(ServiceNameConsts.APOLLO_ADMINSERVICE);
if (application == null) {
Tracer.logEvent("Apollo.ZkDiscovery.NotFound", ServiceNameConsts.APOLLO_ADMINSERVICE);
}
return application != null ? application : Collections.emptyList();

修改点五:

com.ctrip.framework.apollo.metaservice.controller.ServiceController#getMetaService()

出入参不变,方法体替换为:

List<ServiceInstance> instances = discoveryService.getMetaServiceInstances();
List<ServiceDTO> result = instances.stream().map(new Function<ServiceInstance, ServiceDTO>() {
@Override
public ServiceDTO apply(ServiceInstance instance) {
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getServiceId());
service.setInstanceId(null);
service.setHomepageUrl(instance.getUri().toString());
return service;
}
}).collect(Collectors.toList());
return result;

修改点六:

com.ctrip.framework.apollo.metaservice.controller.ServiceController#getConfigService( @RequestParam(value = "appId", defaultValue = "") String appId, @RequestParam(value = "ip", required = false) String clientIp)

出入参不变,方法体替换为:

List<ServiceInstance> instances = discoveryService.getConfigServiceInstances();
List<ServiceDTO> result = instances.stream().map(new Function<ServiceInstance, ServiceDTO>() {
@Override
public ServiceDTO apply(ServiceInstance instance) {
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getServiceId());
service.setInstanceId(null);
service.setHomepageUrl(instance.getUri().toString());
return service;
}
}).collect(Collectors.toList());
return result;

修改点七:

com.ctrip.framework.apollo.metaservice.controller.ServiceController#getAdminService()

出入参不变,方法体替换为:

List<ServiceInstance> instances = discoveryService.getAdminServiceInstances();
List<ServiceDTO> result = instances.stream().map(new Function<ServiceInstance, ServiceDTO>() {
@Override
public ServiceDTO apply(ServiceInstance instance) {
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getServiceId());
service.setInstanceId(instance.getHost()+"|"+instance.getScheme()+"|"+instance.getMetadata()+"|"+instance.getPort()+"&");
service.setHomepageUrl(instance.getUri().toString());
return service;
}
}).collect(Collectors.toList());
return result;
4、configservice、adminservice、portal组件bootstrap.properties均添加zk配置
################# 服务注册 #################
#注册中心,根存储路径,默认也是/service
spring.cloud.zookeeper.discovery.root=/services
#注册中心,连接串,多个地址逗号分隔
spring.cloud.zookeeper.connect-string=127.0.0.1:2181
#是否注册
spring.cloud.zookeeper.discovery.enabled=true
spring.cloud.zookeeper.discovery.register=true
spring.cloud.zookeeper.discovery.instance-host=${spring.cloud.client.ip-address}
################# 监控信息 #################
#修改访问路径  2.0之前默认是/   2.0默认是 /actuator
#不改此配置,服务调用会有异常,可以通过这个属性值修改
#management.endpoints.web.base-path=/actuator
#开放所有页面节点  默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
#management.endpoints.enabled=false
#显示健康具体信息  默认不会显示详细信息
#management.endpoint.health.show-details=always
5、configservice、adminservice、portal启动类修改

三个组件启动类去除注解

@EnableEurekaServer
@EnableEurekaClient
均替换为
@EnableDiscoveryClient

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: