使用Spring Cloud Zookeeper实现服务的注册和发现

Spring Cloud Zookeeper provides Apache Zookeeper integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with Zookeeper. The patterns provided include Service Discovery and Distributed Configuration.

首先要安装zookeeper,我这里安装的是:zookeeper-3.4.6

本实例使用2个服务端,1个客户端

项目依赖:


	
		
			org.springframework.cloud
			spring-cloud-zookeeper-dependencies
			1.0.1.RELEASE
			pom
			import
		
	



	
		org.springframework.cloud
		spring-cloud-starter-zookeeper-all
	

服务端一:

package com.pp.zk.server1;

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

@SpringBootApplication
@EnableDiscoveryClient
public class AppServer {
    public static void main(String[] args) {
    	SpringApplication.run(AppServer.class, args);
    }
}

application.properties

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

bootstrap.properties

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

服务端二:

application.properties

server.port=8833
spring.application.name=tomcat
其余的代码、配置和上面的一样


分别启动这2个main方法

去zookeeper去查看节点信息

[zk: localhost:2181(CONNECTED) 70] ls /services/tomcat
[68e73968-9c1e-4362-a20c-ed505f772837, eeb02f9b-d115-4e18-ad31-cafc66093aa2]

这里可以看到,有2个临时节点,即有两个服务


客户端:

package com.pp.zk.client;

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 AppClient {
	@Autowired
	private LoadBalancerClient loadBalancer;

	@Autowired
	private DiscoveryClient discovery;
	
	@RequestMapping("/discovery")
	public Object discovery() {
		System.out.println(loadBalancer.choose("tomcat"));
		return "discovery";
	}
	
	@RequestMapping("/all")
	public Object all() {
		System.out.println(discovery.getServices());
		return "all";
	}

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

server.port=8844
spring.application.name=tomcat-client

bootstrap.properties

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

注意这里的spring.cloud.zookeeper.discovery.register必须配置为false,否则,应用启动之后,也去zookeeper里面注册了服务

启动main方法,

访问http://127.0.0.1:8844/discovery 系统会返回一个可用的服务,默认使用轮询的方法返回一个服务

你可能感兴趣的:(应用技术)