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

改造背景:

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

解决:

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

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

1、最外层pom.xml改造

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


   org.springframework.cloud
   spring-cloud-starter-zookeeper-all
   
      
         org.apache.zookeeper
         zookeeper
      
   


   org.apache.zookeeper
   zookeeper
   3.4.10
   
      
         org.slf4j
         slf4j-log4j12
      
   

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 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 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 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 instances = discoveryService.getMetaServiceInstances();
List result = instances.stream().map(new Function() {
  @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 instances = discoveryService.getConfigServiceInstances();
List result = instances.stream().map(new Function() {
  @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 instances = discoveryService.getAdminServiceInstances();
List result = instances.stream().map(new Function() {
  @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

你可能感兴趣的:(微服务-配置中心)