SpringCloud集成Zookeeper服务注册与发现
2020-01-15 本文已影响0人
程就人生
Zookeeper有一个很重要的功能,便是服务的注册与发现。在分布式系统设计中,有一个CAP原理(C:数据一致性;A:服务可用性;P:分区容错性,服务对网络分区故障的容错性),任何系统只能实现其中的两个,三个同时实现是不可能的。Zookeeper遵循的是CP原则,Eureka是AP原则。下面就看看SpringCloud集成的Zookeeper是如何实现服务注册与发现的。
环境要求:
安装Zookeeper3.4.14
SpringBoot 2.2
SpringCloud Hoxton.SR1
JDK1.8
Maven4.0
Zookeeper服务端代码
第一,在pom文件中导入必须的架包;
<!--springcloud 与Zookeeper的集成-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--由于兼容问题,这里使用Zookeeper3.4.12-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
第二步,配置文件如下;
#启动端口号设置
server.port=8822
#服务名称
spring.application.name=zookeeper-service
#Zookeeper服务器地址,也可以是集成的
spring.cloud.zookeeper.connect-string=localhost:2181
#注册到服务器上的host和port
spring.cloud.zookeeper.discovery.instance-host=localhost
spring.cloud.zookeeper.discovery.instance-port=${server.port}
#是否注册到服Zookeeper服务器上
spring.cloud.zookeeper.discovery.register=true
第三步,接口文件,被客户端调用;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/helloworld")
public String HelloWorld() {
return "Hello World!";
}
}
Zookeeper客户端代码
第一,在pom文件中导入必须的架包;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--由于兼容问题,这里使用Zookeeper3.4.12-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
第二步,配置文件;
#端口号
server.port=8083
#服务名称
spring.application.name=zookeeper-client
#连接的Zookeeper服务器,多个用逗号分隔
spring.cloud.zookeeper.connect-string: localhost:2181
#这里是客户端,无需注册到Zookeeper服务器上
spring.cloud.zookeeper.discovery.register=false
#打印错误日志
logging.level.org.apache.zookeeper.ClientCnxn: WARN
第三步,服务的发现;
mport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
/**
* 服务发现
* @EnableFeignClients 开启服务发现
**/
@Configuration
@EnableFeignClients
public class DiscoverServer {
@Autowired
private ServiceInterface serviceInterface;
public String HelloWorld() {
return serviceInterface.HelloWorld();
}
}
第四步,服务调用;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 服务调用
* @author 程就人生
* @date 2020年1月15日
*/
@FeignClient(name="zookeeper-service") //这里必须写注册的服务名称,与服务器的启动名称保持一致
public interface ServiceInterface {
@RequestMapping(path = "/helloworld", method = RequestMethod.GET)
@ResponseBody
String HelloWorld();
}
第五步,测试程序;
/**
* 获取服务测试
* @author 程就人生
* @date 2020年1月15日
*/
@RestController
public class IndexController {
@Autowired
private DiscoverServer discoverServer;
@GetMapping("/index")
public String index() {
return discoverServer.HelloWorld();
}
}
最后,测试;
分别启动服务端和客户端,然后再浏览器输入客户端地址,运行一下,测试结果如下;
把服务端的端口号改一下,再启动一个服务端,看下Zookeeper节点的存储结果图;
Zookeeper节点示意图
从图中可以看出,有两个节点,使用命令查看第二个节点,可以看到服务的地址与端口号等详细信息。 Zookeeper配置文件
注册的根节点在services下,这是配置文件里面默认的,可以修改这个默认配置重新设置,其他的参数同理。
总结
SpringCloud集成Zookeeper实现了服务的注册与发现,服务的节点信息都存储在Zookeeper服务器上,客户端只需要调用Zookeeper服务器,获取对应的节点信息,根据节点信息再调用服务即可。
可能遇到的问题:
- Zookeeper安装失败,如果是安装高版本的,比如3.5.6,则下载名字带bin的安装包,否则启动不成功,会报错。
- SpringCloud集成了Zookeeper,但是还是需要加入Apache的Zookeeper3.4版本的架包,最新版本的兼容性不是太好。