一、zookeeper简介
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
原理:
ZooKeeper是以Fast Paxos算法为基础的,Paxos 算法存在活锁的问题,即当有多个proposer交错提交时,有可能互相排斥导致没有一个proposer能提交成功,而Fast Paxos作了一些优化,通过选举产生一个leader (领导者),只有leader才能提交proposer,具体算法可见Fast Paxos。因此,要想弄懂ZooKeeper首先得对Fast Paxos有所了解。
ZooKeeper的基本运转流程:
- 1、选举Leader;
- 2、同步数据;
- 3、选举Leader过程中算法有很多,但要达到的选举标准是一致的;
- 4、Leader要具有最高的执行ID,类似root权限;
- 5、集群中大多数的机器得到响应并接受选出的Leader。
二、Zookeeper作为注册中心搭建SpringCloud实现服务注册及发现
1、创建工程,父pom.xml依赖文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>springcloud-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springcloud-zookeeper</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<modules>
<module>zookeeper-server</module>
<module>zookeeper-client</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Camden.SR7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、服务提供者
pom.xml依赖文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo.zookeeper</groupId>
<artifactId>zookeeper-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zookeeper-server</name>
<description></description>
<parent>
<groupId>com.example.demo</groupId>
<artifactId>springcloud-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件:
server:
port: 8081
spring:
application:
name: zookeeper-server
cloud:
zookeeper:
connect-string: localhost:2181
discovery:
enabled: true
register: true
暴露接口:
package com.example.demo.zookeeper.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* 路径:com.example.demo.zookeeper.controller
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2018/9/25 21:19
* 修改人:
* 修改备注:
* 修改时间:
*/
@RestController
public class TestController {
private static final Logger log = LoggerFactory.getLogger(TestController.class);
private static final UUID INSTANCE_UUID = UUID.randomUUID();
@GetMapping("/test")
public String test(){
log.info("test :" + INSTANCE_UUID.toString());
return INSTANCE_UUID.toString();
}
}
启动类添加注解@EnableDiscoveryClient标注为服务发现注解
package com.example.demo.zookeeper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperServerApplication.class, args);
}
}
3、服务消费者
pom.xml依赖文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo.zookeeper</groupId>
<artifactId>zookeeper-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zookeeper-client</name>
<parent>
<groupId>com.example.demo</groupId>
<artifactId>springcloud-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件:
server:
port: 8082
spring:
application:
name: zookeeper-client
cloud:
zookeeper:
connect-string: localhost:2181
discovery:
enabled: true
register: false
基于Feign的实现接口
package com.example.demo.zookeeper.service;
import com.example.demo.zookeeper.service.fallback.FeignFallback;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 路径:com.example.demo.zookeeper.service
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2018/9/25 21:39
* 修改人:
* 修改备注:
* 修改时间:
*/
@FeignClient(value = "zookeeper-server",fallback = FeignFallback.class)
public interface FeignService {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test();
}
fallback为断路器回调:
package com.example.demo.zookeeper.service.fallback;
import com.example.demo.zookeeper.service.FeignService;
import org.springframework.stereotype.Component;
/**
* 路径:com.example.demo.zookeeper.service.fallback
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2018/9/25 21:49
* 修改人:
* 修改备注:
* 修改时间:
*/
@Component
public class FeignFallback implements FeignService {
@Override
public String test() {
return "servie error!";
}
}
消费服务:
package com.example.demo.zookeeper.controller;
import com.example.demo.zookeeper.service.FeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* 路径:com.example.demo.zookeeper.controller
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2018/9/25 21:43
* 修改人:
* 修改备注:
* 修改时间:
*/
@RestController
public class TestController {
private static final Logger log = LoggerFactory.getLogger(TestController.class);
private static final UUID INSTANCE_UUID = UUID.randomUUID();
@Autowired
private FeignService feignService;
@GetMapping("/test")
public String test(){
log.info("test :" +INSTANCE_UUID.toString());
return feignService.test();
}
}
启动类添加注解@EnableCircuitBreaker为断路器的支持,@EnableFeignClients为Feign客户端的支持。
package com.example.demo.zookeeper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ZookeeperClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperClientApplication.class, args);
}
}
启动工程,访问 http://localhost:8082/test,浏览显示:
09969381-2857-455c-b9f2-6fd5d3d25b16
消费者接口日志显示:
2018-09-26 10:27:32.195 INFO 12176 --- [nio-8082-exec-1] c.e.d.z.controller.TestController : test :ae5bdfce-887d-433f-a90a-cb4b8a1f109d
服务者接口日志显示:
2018-09-26 10:27:33.188 INFO 8204 --- [nio-8081-exec-1] c.e.d.z.controller.TestController : test :09969381-2857-455c-b9f2-6fd5d3d25b16