SpringCloud与Zookeeper整合(Zookeeper为注册中心)

可参照该博客:ZK可视化Web工具(zkui)安装与搭建(zookeeper)

SpringCloud与Zookeeper整合(Zookeeper为注册中心)_第1张图片

SpringCloud与Zookeeper完美结合,搭建简易的服务注册中心,依赖的三脚架jar包如下            


  org.springframework.cloud
  spring-cloud-starter-zookeeper-discovery
  2.1.2.RELEASE


  org.apache.zookeeper
  zookeeper
  3.4.10
  
    
      org.slf4j
      slf4j-log4j12
    
    
      log4j
      log4j
    
  

三脚架整合SpringCloud以Zookeeper作为服务注册中心。

服务提供者的pom.xml配置如下:

======================================pom.xml=========================




  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.2.RELEASE
    
  

  com.dubbo.zookeeper.register
  SSO-SpringCloud-Zookeeper
  1.0-SNAPSHOT

  SSO-SpringCloud-Zookeeper

  
  
    
      repos
      Repository
      http://maven.aliyun.com/nexus/content/groups/public
    
    
      central
      Maven Repository Switchboard
      default
      http://repo1.maven.org/maven2
      
        false
      
    
  
  
  
    
      repos
      Repository
      http://maven.aliyun.com/nexus/content/groups/public
    
  

  
    UTF-8
    1.8
    1.8
  

  
  
    org.springframework.boot
    spring-boot-starter-web
  
    
    
      org.springframework.cloud
      spring-cloud-starter-zookeeper-discovery
      2.1.2.RELEASE
    
    
    
      org.apache.zookeeper
      zookeeper
      3.4.10
      
        
          org.slf4j
          slf4j-log4j12
        
        
          log4j
          log4j
        
      
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  


相关的*.properties配置文件如下:

===============================application.properties=============

spring.profiles.active=dev

===============================application-dev.properties===========

##  配置引用名称,会在/zk-server下生成该名称的节点
spring.application.name=spring-cloud-zookeeper-provider
##  配置服务端口
server.port=8888
## 关闭安全控制
management.security.enabled=false
## connect-string:连接zk服务的连接串
spring.cloud.zookeeper.connect-string=centoshadoop1:2181,centoshadoop2:2181,centoshadoop3:2181
# register: 是否启动服务注册
spring.cloud.zookeeper.discovery.enabled=true
spring.cloud.zookeeper.discovery.register=true

instance-id: zk唯一id的标识,会在/zk-server/spring-cloud-zookeeper-provider/下面生成该节点
spring.cloud.zookeeper.discovery.instance-id=zk-00001
root: zk根节点名称,默认/services 
spring.cloud.zookeeper.discovery.root=/zk-server-provider

  

服务业务逻辑:

package com.dubbo.zookeeper.register.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class ZKUserController {
    @Value("${server.port}")
    private int port;
    @Value("${spring.application.name}")
    private String applicationName;

    @GetMapping("/list")
    public List findZkUser(){
        List resultList =  new ArrayList<>();
        resultList.add(new Zkuser("zk-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
        resultList.add(new Zkuser("zk-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
        resultList.add(new Zkuser("zk-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
        resultList.add(new Zkuser("zk-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
        return resultList;
    }
}

引用的辅助实体类:
 

package com.dubbo.zookeeper.register.controller;

import java.io.Serializable;

public class Zkuser implements Serializable {
    private String name;
    private int age;
    private int port;

    public Zkuser(String name, int age, int port) {
        this.name = name;
        this.age = age;
        this.port = port;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return "Zkuser{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ",server port=" + port +
                '}';
    }
}

服务提供者启动类:

package com.dubbo.zookeeper.register;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 *
 *zookeeper配置注册中心
 */
@SpringBootApplication
public class SpringCloudZookeeperApplication {
    public static void main( String[] args ) {
        SpringApplication.run(SpringCloudZookeeperApplication.class,args);
    }
}

启动服务提供者:
访问:http://localhost:8888/list  ,返回业务数据:

[
  {"name":"zk-server-spring-cloud-zookeeper-server-5701","age":32,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-server-5338","age":96,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-server-5520","age":65,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-server-7225","age":65,"port":8888}
]

=================服务消费者相关配置========================================

pom.xml配置




  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.2.RELEASE
    
  
  com.zookeeper.dubbo.register.consumer
  SSO-SpringCloud-Zookeeper-Consumer
  1.0-SNAPSHOT

  SSO-SpringCloud-Zookeeper-Consumer
  
  
    
      repos
      Repository
      http://maven.aliyun.com/nexus/content/groups/public
    
    
      central
      Maven Repository Switchboard
      default
      http://repo1.maven.org/maven2
      
        false
      
    
  
  
  
    
      repos
      Repository
      http://maven.aliyun.com/nexus/content/groups/public
    
  
  
    UTF-8
    1.8
    1.8
    Finchley.SR2
  

  
    
      org.springframework.cloud
      spring-cloud-starter-feign
      1.4.6.RELEASE
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      org.springframework.cloud
      spring-cloud-starter-zookeeper-discovery
      2.1.2.RELEASE
    
    
    
      org.apache.zookeeper
      zookeeper
      3.4.10
      
        
          org.slf4j
          slf4j-log4j12
        
        
          log4j
          log4j
        
      
    
  

  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud.version}
        pom
        import
      
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

application-dev.properties 配置如下:

##  配置引用名称,会在/zk-server下生成该名称的节点
spring.application.name=spring-cloud-zookeeper-consumer
##  配置服务端口
server.port=7777
## 关闭安全控制
management.security.enabled=false
## connect-string:连接zk服务的连接串
spring.cloud.zookeeper.connect-string=centoshadoop1:2181,centoshadoop2:2181,centoshadoop3:2181
# register: 是否启动服务注册
spring.cloud.zookeeper.discovery.enabled=true
spring.cloud.zookeeper.discovery.register=true
# instance-id: zk唯一id的标识,会在/zk-server/spring-cloud-zookeeper-prover/下面生成该节点
spring.cloud.zookeeper.discovery.instance-id=zk-00001
# # root: zk根节点名称,默认/services,必須與服務提供者的相同 ,否則報錯com.netflix.client.ClientException: Load 
# # balancer does not have available server for client: spring-cloud-zookeeper-provider
spring.cloud.zookeeper.discovery.root=/zk-server-provider

消费者代理接口
package com.zookeeper.dubbo.register.consumer.service;
import com.zookeeper.dubbo.register.consumer.domain.Zkuser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Service
@FeignClient("spring-cloud-zookeeper-provider") // 该值为服务提供者的应用名称
public interface ZkUserService {
    @GetMapping("/list")
    List getList();
}

图为服务提供者,在zookeeper注册中心注册的服务提供者名称 spring-cloud-zookeeper-provider

controler类

package com.zookeeper.dubbo.register.consumer.controller;

import com.zookeeper.dubbo.register.consumer.domain.Zkuser;
import com.zookeeper.dubbo.register.consumer.service.ZkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ZkUserController {
    @Autowired
    private ZkUserService zkUserService;

    @GetMapping("/consumerList")
    public List getList() {
        return zkUserService.getList();
    }
}

启动类:

package com.zookeeper.dubbo.register.consumer;

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

/**
 * zookeeper註冊中心消費者
 *
 */
@SpringBootApplication
@EnableDiscoveryClient  // 開啟服務方向機制
@EnableFeignClients
public class ZkConsumerApplication {
    public static void main( String[] args ) {
        SpringApplication.run(ZkConsumerApplication.class,args);
    }
}
Zkuser类同提供者

访问路径:http://localhost:7777/consumerList

[
  {"name":"zk-server-spring-cloud-zookeeper-provider-5421","age":85,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-provider-8779","age":13,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-provider-6341","age":63,"port":8888},
  {"name":"zk-server-spring-cloud-zookeeper-provider-903","age":16,"port":8888}
]

可以在Zookeeper WebUI中看到已经注册的服务  zk-server-provider

SpringCloud与Zookeeper整合(Zookeeper为注册中心)_第2张图片

完美SpringCloud以Zookeeper为注册中心整合成功!!

尽请关注下一篇:SpringCloud与Consul(Consul为注册中心)....

SpringCloud与Consul整合

 

你可能感兴趣的:(Zookeeper集群搭建,分布式常用注册中心)