SpringCloud——Zookeeper(注册中心)

一、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依赖文件



	4.0.0

	com.example.demo
	springcloud-zookeeper
	0.0.1-SNAPSHOT
	pom

	springcloud-zookeeper

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.5.RELEASE
	

	
		zookeeper-server
		zookeeper-client
	

	
		UTF-8
		UTF-8
		1.8
		Camden.SR7
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2、服务提供者

pom.xml依赖文件:



	4.0.0

	com.example.demo.zookeeper
	zookeeper-server
	0.0.1-SNAPSHOT
	jar

	zookeeper-server
	

	
		com.example.demo
		springcloud-zookeeper
		0.0.1-SNAPSHOT
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		

		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		

		
			org.springframework.cloud
			spring-cloud-starter-ribbon
		

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

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



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依赖文件:



	4.0.0

	com.example.demo.zookeeper
	zookeeper-client
	0.0.1-SNAPSHOT
	jar

	zookeeper-client

	
		com.example.demo
		springcloud-zookeeper
		0.0.1-SNAPSHOT
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-actuator
		

		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		

		
			org.springframework.cloud
			spring-cloud-starter-ribbon
		

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

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.cloud
			spring-cloud-starter-feign
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



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

源码下载:https://download.csdn.net/download/typ1805/10687809

 

你可能感兴趣的:(springcloud,zookeeper)