使用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个客户端

项目依赖:

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-zookeeper-dependencies</artifactId>
			<version>1.0.1.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-zookeeper-all</artifactId>
	</dependency>
</dependencies>

服务端一:

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 系统会返回一个可用的服务,默认使用轮询的方法返回一个服务

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值