以zookeeper為注冊中心搭建spring cloud環境


在spring cloud體系中,有多種手段實現注冊中心,本例中采用zookeeper作為注冊中心的角色。服務提供者向zookeeper注冊,服務消費者從zookeeper中發現服務提供者的相關信息,從而遠程調用服務提供方。

 

spring cloud與zookeeper的集成主要依賴spring-cloud-starter-zookeeper-discovery模塊,下面是實例:

1、服務提供者zktest

引入相關依賴pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dxz.zktestclient</groupId>
    <artifactId>zktestclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--設置字符編碼及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--用於測試的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依賴管理,用於管理spring-cloud的依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官網為Angel.SR4版本,但是我使用的時候總是報錯 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--使用該插件打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

啟動類

package com.dxz.zktest;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ZktestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZktestApplication.class, args);
    }
}

配置信息

1、application.properties

server.port=8822
spring.application.name=zktest

2、bootstrap.properties

spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.instanceHost=192.168.5.3
spring.cloud.zookeeper.discovery.instancePort=${server.port}

2、服務消費者zktestClient

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dxz.zktestclient</groupId>
    <artifactId>zktestclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--設置字符編碼及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--用於測試的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依賴管理,用於管理spring-cloud的依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官網為Angel.SR4版本,但是我使用的時候總是報錯 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--使用該插件打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

啟動類

package com.dxz.zktestclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ZktestClientApplication {

    @Autowired
    private LoadBalancerClient loadBalancer;

    @Autowired
    private DiscoveryClient discovery;

    @RequestMapping("/discovery")
    public Object discovery() {
        System.out.println(loadBalancer.choose("zktest"));
        return "discovery";
    }

    @RequestMapping("/all")
    public Object all() {
        System.out.println(discovery.getServices());
        return "all";
    }

    public static void main(String[] args) {
        SpringApplication.run(ZktestClientApplication.class, args);
    }
}

配置信息

1、application.properties

server.port=8844
spring.application.name=zktestclient

2、bootstrap.properties

spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.register=false

3、zookeeper環境搭建

3.1、zookeeper安裝

http://mirrors.shuosc.org/apache/zookeeper/zookeeper-3.4.11/

見《zk 03之 linux上的zookeeper單機與偽集群安裝

4、測試

啟動zookeeper后, 先啟動zktest項目,日志如下

查看zookeeper的節點情況,

在zookeeper所在bin目錄下, 執行如下命令

[root@localhost bin]# ./zkCli.sh -server localhost,登錄成功會展示如下:

查看zookeeper上的"/services/zktest"目錄

[zk: localhost(CONNECTED) 1] ls /services/zktest
[22aa5d6a-f8a9-4053-bdc4-1b926ddc10b4]
[zk: localhost(CONNECTED) 2] 

啟動zktestClient項目

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM