springCloud集成zookeper


最近梳理springboot相關知識。看到分布式鎖,其中有一種是使用zookeeper實現的,就學習一下zookeeper。本來是使用springboot和zookeeper集成的,但是試了半天,好像不行。pom文件一直沖突。無奈,參考  https://start.spring.io/ ,生成了一個小的demo,發現該demo是使用springcloud,遂棄之springboot,使用springcloud,其中的原因我猜測可能是zookeeper是一個組件,屬於springcloud體系。

下面是zookeeper官網對zookeeper的介紹

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

zookeeper是被適用於分手不是應用中的。springboot是一個單體的微服務,多個單體的微服務構成分布式服務。而springcloud是一個服務治理的集成者。

 

——————————分割線———————————————

springCloud集成zookeeper 

1,創建一個maven工程;

2:,引入依賴

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- 提供zookeeper整合的包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
        </dependency>
        <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>
        <!-- 熱部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR2</version>
                <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>

說明:注意引入的依賴之間是否沖突。

2,配置文件 application.properties

#系統中用到的參數配置  編碼格式
com.interview.question=springboot有哪些配置的注解
logging.level.root=INFO
logging.file=D:/logs/hello.log
server.port=8081
spring.application.name=info     ——————————————必須有該屬性配置,否則啟動時報空指針異常

配置文件

#spring.cloud.zookeeper.connectString=127.0.0.1:2181
3spring.cloud.zookeeper.discovery.instanceHost=127.0.0.1
#spring.cloud.zookeeper.discovery.instancePort=${server.port}

## 啟用zookeeper作為配置中心
spring.cloud.zookeeper.config.enabled = true

## 配置根路徑
spring.cloud.zookeeper.config.root = config

## 配置默認上下文
spring.cloud.zookeeper.config.defaultContext = zook

## 配置profile分隔符
spring.cloud.zookeeper.config.profileSeparator = -

3:,啟動類

package zookeper;

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

/**
*
* 項目名稱:zookeper
* 類名稱:App
* 類描述:
* 創建人:john
* 創建時間:2018年7月31日 下午3:36:01
* 修改人:john
* 修改時間:2018年7月31日 下午3:36:01
* 修改備注:
* @version
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ZookApp {
   public static void main(String[] args){
       SpringApplication.run(ZookApp.class, args);
       System.out.println("hello springcloud");
   }
}

4,controller 

package zookeper.controller;

import java.util.List;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 項目名稱:zookeper 類名稱:ZookController 類描述: 創建人:john 創建時間:2018年7月31日 下午3:51:56
 * 修改人:john 修改時間:2018年7月31日 下午3:51:56 修改備注:
 * 
 * @version
 *
 */
@RestController
@RequestMapping("/zook")
public class ZookController {
    @Autowired
    private DiscoveryClient client;
    @Autowired
    private Environment environment;

    public String getZook() {
        return "";
    }

    @RequestMapping("/getServices")
    public String discoveryClent() {
        List<String> serviceList = client.getServices();
        System.out.println("注冊服務的數量>>>>>>>>>>>>>>>>>" + serviceList.size());
        for (String service : serviceList) {
            System.out.println("注冊的服務>>>>>>" + service);
        }
        return "info";
    }

    @GetMapping("/env")
    public String test() {
        String[] profiles = environment.getActiveProfiles();
        System.out.println("profiles>>>>>>>" + profiles.length);
        for (String item : profiles) {
            System.out.println("item>>>>>>>>>>>>>>>" + item);
        }

        String name = environment.getProperty("url");
        System.out.println(name);

        return "Hello," + name;
    }
}

5,驗證 

瀏覽器驗證

 

 

elipse打印

 

 

6,添加操作zookeeper的客戶端  CuratorFramework(直接注入即可)

package zookeper.controller;

import java.util.List;
import org.springframework.core.env.Environment;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 項目名稱:zookeper 類名稱:ZookController 類描述: 創建人:john 創建時間:2018年7月31日 下午3:51:56
 * 修改人:john 修改時間:2018年7月31日 下午3:51:56 修改備注:
 * 
 * @version
 *
 */
@RestController
@RequestMapping("/zook")
public class ZookController {
    @Autowired
    private DiscoveryClient client;
    @Autowired
    private Environment environment;
    @Autowired
    private CuratorFramework curatorFramework;

    public String getZook() {
        return "";
    }

    @RequestMapping("/getServices")
    public String discoveryClent() {
        List<String> serviceList = client.getServices();
        List<ServiceInstance> list=client.getInstances("info");
         //獲取實例化的服務
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0 ) {
            sb.append(list.get(0).getUri()+",");
            System.out.println(">>>>>>>>>>>>>>>>"+list.get(0).isSecure());
        }
        System.out.println("sb>>>>>"+sb);
        System.out.println("注冊服務的數量>>>>>>>>>>>>>>>>>" + serviceList.size());
        for (String service : serviceList) {
            System.out.println("注冊的服務>>>>>>" + service);
        }
        return "info";
    }

    @GetMapping("/env")
    public String test() {
        String[] profiles = environment.getActiveProfiles();
        System.out.println("profiles>>>>>>>" + profiles.length);
        for (String item : profiles) {
            System.out.println("item>>>>>>>>>>>>>>>" + item);
        }
        
        String name = environment.getProperty("url");
        
        try {
            List <String> listChildren=curatorFramework.getChildren().forPath("/config/zook");
            for(String child:listChildren ){
                System.out.println("child>>>>>>>"+child);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println(name);

        return "Hello," + name;
    }
}

 


免責聲明!

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



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