Spring Cloud Zookeeper的使用

Spring Cloud Zookeeper 帮助用户使用 Apache Zookeeper 注册自己的spring boot服务。

软件包版本
spring boot 2.0.6.RELEASE
zookeeper 3.4.12   服务端版本3.4.8
spring cloud zookeeper 2.0.0.RELEASE

1.安装zookeeper(这里需要注意,zk的客户端和服务端版本要一致),启动zk服务

2.导入包  pom.xml,这里主要两个部分,spring boot 和 spring cloud zookeeper 



    4.0.0

    com.hc
    z_11_2
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    


    
        
            
                org.springframework.cloud
                spring-cloud-zookeeper-dependencies
                
                    
                        org.apache.zookeeper
                        zookeeper
                    
                
                2.0.0.RELEASE
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
        
        
            org.apache.httpcomponents
            httpclient
            4.5.3
        
        
        
            org.apache.zookeeper
            zookeeper
            3.4.12
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        

    




3.配置spring boot服务配置文件 application.yml

spring:
  cloud:
    zookeeper:
      connect-string: 192.168.37.131:2181
  application:
    name: zkserv
server:
  port: 8888

4.spring boot服务代码

package zoo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Application {
    @RequestMapping("/")
    public String home() {
        return "Hello world"+serviceUrl();
    }
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    public String serviceUrl() {
        List list = discoveryClient.getInstances("zkserv");
        if (list != null && list.size() > 0 ) {
            return list.get(0).getUri().toString();
        }
        return null;
    }
}

以上步骤即可完成spring boot服务注册到zk的操作

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

激活服务发现,需要引入spring-cloud-starter-zookeeper-discovery包  , 服务上加上@EnableDiscoveryClient

对于web 需要引入spring-boot-starter-web包

你可能感兴趣的:(微服务,spring,cloud,zookeeper)