SpringCloud——Zookeeper(註冊中心)
阿新 • • 發佈:2018-12-12
一、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);
}
}
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