1. 程式人生 > 實用技巧 >服務註冊與發現【Zookeeper】

服務註冊與發現【Zookeeper】

Zookeeper 伺服器取代 Eureka 伺服器,作為服務註冊中心(因我本地沒有安裝Zookeeper,則下面內容無本人實戰案例,引用的其他案例來說明)

服務提供者:

新建一個服務提供者 cloud-provider-payment8004(與前面Eureka服務提供者區分),埠8004

引入POM,其他的依賴和其他服務提供者一樣,只是少了EurekaClient依賴,多了Zookeeper依賴:

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery 
--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency>

YML

server:
  port: 8004

spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect
-string: 192.168.136.140:2181

主啟動類:添加註解:@EnableDiscoveryClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderPaymentApplication8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }

啟動問題:

如果引入的Zookeeper jar包版本和安裝的Zookeeper版本不一致,就會啟動報錯,類似如下問題:

此時,我們就可以排除Zookeeper的POM中自帶的 zookeeper*. jar,然後引入和我們安裝版本對應的jar包版本:

 
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--排除zk3.5.3-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
            <!--新增zk 3.4,9版本-->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>
 

服務消費者:

新建 cloud-consumerzk-order8081

POM:

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

YML:

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: 192.168.136.140:2181
 

主啟動:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain8081 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZKMain8081.class,args);
    }
}
 

其他的業務實現,和Eureka一樣。

總結:

將服務註冊中心,從Eureka 換成Zookeeper 後,涉及到需要修改的地方就只有:

1. 服務提供者和消費者,從引入EurekaClient 依賴,切換成引入 Zookeeper 依賴。

2. yml 檔案修改:原來配置的Eureka註冊中心地址,換成Zookeeper註冊中心地址。

3. 主啟動類:去掉 @EnableEurekaClient 的 Eureka註解,改為 @EnableDiscoveryClient 的Zookeeper註解。

4. 如果遇到jar包版本衝突而導致的啟動失敗問題,那就剔除掉預設引入的jar包而重新引入和安裝的Zookeeper版本一致的jar包。