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打印

猜你喜欢

转载自www.cnblogs.com/li-zhan/p/9401692.html