ZooKeeper作为注册中心时,Java应用接入方式

服务提供者

  • 引入相关依赖
    compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-client', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-x-discovery', version: '2.10.0'

  • 定义服务注册接口
   private ServiceDiscovery serviceDiscovery;
   private final CuratorFramework client;
   public ServiceRegistry(CuratorFramework client, String basePath) throws Exception {
      this.client = client;
      JsonInstanceSerializer serializer = new JsonInstanceSerializer(
            InstanceDetails.class);
      serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).serializer(serializer)
            .basePath(basePath).build();
      serviceDiscovery.start();
   }
   public void registerService(ServiceInstance serviceInstance) throws Exception {
      serviceDiscovery.registerService(serviceInstance);
   }
   public void unregisterService(ServiceInstance serviceInstance) throws Exception {
      serviceDiscovery.unregisterService(serviceInstance);
   }
   public void updateService(ServiceInstance serviceInstance) throws Exception {
      serviceDiscovery.updateService(serviceInstance);
   }
   public void close() throws IOException {
      serviceDiscovery.close();
   }
}

  • 服务注册
   public static void main(String[] args) throws Exception {
   //zookeeper ip 地址,重试策略
      CuratorFramework client = CuratorFrameworkFactory.newClient("10.113.90.160:2181", new ExponentialBackoffRetry(1000, 3));
      client.start();
      //定义根节点
      ServiceRegistry serviceRegistrar = new ServiceRegistry(client,"services");
      //获取相关的服务配置信息 InstanceDetails 服务对象(包含相关服务吗 端口号,ip属性)
      ServiceInstance instance1 = ServiceInstance.builder()
            .name("service1")
            .port(12345)
            .address("192.168.1.100")   //address不写的话,会取本地ip
            .payload(new InstanceDetails(UUID.randomUUID().toString(),"192.168.1.100",12345,"Test.Service1"))
            .uriSpec(new UriSpec("{scheme}://{address}:{port}"))
            .build();
      ServiceInstance instance2 = ServiceInstance.builder()
            .name("service2")
            .port(12345)
            .address("192.168.1.100")
            .payload(new InstanceDetails(UUID.randomUUID().toString(),"192.168.1.100",12345,"Test.Service2"))
            .uriSpec(new UriSpec("{scheme}://{address}:{port}"))
            .build();
            //注册服务
      serviceRegistrar.registerService(instance1);
      serviceRegistrar.registerService(instance2);
      System.out.println("aaaaaaaa");
      Thread.sleep(Integer.MAX_VALUE);
   }
}

服务消费方

  • 引入相关依赖
    compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-client', version: '2.10.0'
    compile group: 'org.apache.curator', name: 'curator-x-discovery', version: '2.10.0'

  • 服务发现接口
   private ServiceDiscovery serviceDiscovery;
   private Map> providers = Maps.newHashMap();
   private List closeableList = Lists.newArrayList();
   private Object lock = new Object();

   public ServiceDiscoverer(CuratorFramework client, String basePath) throws Exception {
      JsonInstanceSerializer serializer = new JsonInstanceSerializer(
            InstanceDetails.class);
      serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(basePath)
            .serializer(serializer).build();

      serviceDiscovery.start();
   }

   public ServiceInstance getInstanceByName(String serviceName) throws Exception {
      ServiceProvider provider = providers.get(serviceName);
      if (provider == null) {
         synchronized (lock) {
            provider = providers.get(serviceName);
            if (provider == null) {
               provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName)
                     .providerStrategy(new RandomStrategy()).build();
               provider.start();
               closeableList.add(provider);
               providers.put(serviceName, provider);
            }
         }
      }
      return provider.getInstance();
   }
   public synchronized void close() {
      for (Closeable closeable : closeableList) {
         CloseableUtils.closeQuietly(closeable);
      }
   }
}

  • 服务发现
   public static void main(String[] args) throws Exception {
      CuratorFramework client = CuratorFrameworkFactory.newClient("10.113.90.160:2181", new ExponentialBackoffRetry(1000, 3));
      client.start();
      ServiceDiscoverer serviceDiscoverer = new ServiceDiscoverer(client,"services");

      ServiceInstance instance1 = serviceDiscoverer.getInstanceByName("service1");

      System.out.println(instance1.buildUriSpec());
      System.out.println(instance1.getPayload());

      ServiceInstance instance2 = serviceDiscoverer.getInstanceByName("service1");

      System.out.println(instance2.buildUriSpec());
      System.out.println(instance2.getPayload());

      serviceDiscoverer.close();
      CloseableUtils.closeQuietly(client);
   }
}

参考链接
[https://www.cnblogs.com/hupengcool/p/3976362.html][[https://blog.csdn.net/jrn1012/article/details/77009025]

你可能感兴趣的:(ZooKeeper作为注册中心时,Java应用接入方式)