用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现
前提:
先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html
说明:
可以再简单的理解为有两方协作,一个是服务提供这,另一个是服务消费者。
搭建实例:
说明:基于Maven的模块工程
父工程POM:
<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.jsoft.testzookeeper</groupId><artifactId>zookeeperdemo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>ZooKeeperDemo</name><description>This is ZookKeeperDemo</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.5.RELEASE</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-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><!-- ZK依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId></dependency><!-- Web支持 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> <!-- 测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency> </dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR7</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></dependencyManagement> <modules><module>zookeeperservice</module><module>zookeeperclient</module></modules> </project>
服务提供者POM:
<?xml version="1.0"?> <projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.jsoft.testzookeeper</groupId><artifactId>zookeeperdemo</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.jsoft.testzookeeper</groupId><artifactId>zookeeperservice</artifactId><version>0.0.1-SNAPSHOT</version><name>zookeeperservice</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies></dependencies> </project>
application.properties:
server.port=8800 spring.application.name=/service-zookeeper spring.cloud.zookeeper.discovery.root=/spring-cloud-service spring.cloud.zookeeper.connect-string=localhost:2181
说明:定义端口,应用的名称,服务在ZK的路径,ZK的服务器地址,其中ZK的服务器地址可以有多个,用逗号隔开。
HelloController:
服务接口
package com.jsoft.testzookeeper.service.controller;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {private static final Logger log = LoggerFactory.getLogger(HelloController.class);@RequestMapping(value = "/hello", method = RequestMethod.GET)public String sayHello(@RequestParam(name = "name") String name) {log.info("param:name->{}", name);return "hello: " + name;} }
App:
程序入口
package com.jsoft.testzookeeper.service;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class App {public static void main( String[] args ){SpringApplication.run(App.class, args);} }
说明:其中要@EnableDiscoveryClient标注为服务发现注解。
服务消费者POM:
<?xml version="1.0"?> <projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.jsoft.testzookeeper</groupId><artifactId>zookeeperdemo</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.jsoft.testzookeeper</groupId><artifactId>zookeeperclient</artifactId><version>0.0.1-SNAPSHOT</version><name>zookeeperclient</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- Feign客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies> </project>
说明:由于使用了Feign客户端,所以引入Feign依赖。
application.properties:
server.port=8810 spring.application.name=/client-zookeeper spring.cloud.zookeeper.discovery.register=false spring.cloud.zookeeper.discovery.root=/spring-cloud-service spring.cloud.zookeeper.connect-string=localhost:2181 spring.cloud.zookeeper.dependencies.service-zookeeper.required=true spring.cloud.zookeeper.dependencies.service-zookeeper.path=/service-zookeeper spring.cloud.zookeeper.dependencies.service-zookeeper.loadBalancerType=ROUND_ROBIN
说明:由于服务提供者的应用名使用了斜杠,所以必须采用依赖关系spring.cloud.zookeeper.dependencies进行别名的选择,注意,使用了这个之后要引入actuator健康监控组件,不然调用时会报错。
App:
程序入口
package com.jsoft.testzookeeper.client;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker @EnableFeignClients public class App {public static void main( String[] args ){SpringApplication.run(App.class,args);} }
说明:@EnableCircuitBreaker为断路器的支持,@EnableFeignClients为Feign客户端的支持。
Client的实现:
主要有两种,基于RestTemplate和Feign
RestTemplate
package com.jsoft.testzookeeper.client.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;@Service public class HelloService {@Autowiredprivate RestTemplate restTemplate;@HystrixCommand(fallbackMethod = "sayHelloFallback")public String sayHello(String name) {return restTemplate.getForEntity("http://service-zookeeper/hello?name=" + name, String.class).getBody();}private String sayHelloFallback(String name) {return "service error";} }
说明:@HystrixCommand为断路器写法。
Feign
package com.jsoft.testzookeeper.client.service;import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;import com.jsoft.testzookeeper.client.service.fallback.FeignFallback;@FeignClient(value = "service-zookeeper", fallback = FeignFallback.class) public interface ServiceFeign {@RequestMapping(value = "/hello")String sayHello(@RequestParam(name = "name") String name); }
说明:fallback为断路器回调。
消费服务:
package com.jsoft.testzookeeper.client.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import com.jsoft.testzookeeper.client.service.HelloService; import com.jsoft.testzookeeper.client.service.ServiceFeign;@RestController public class HelloController {@Autowiredprivate HelloService helloService;@Autowiredprivate ServiceFeign serviceFeign;@RequestMapping(value = "hello")public String hello(@RequestParam String name) {return helloService.sayHello(name);}@RequestMapping(value = "hello2")public String hello2(@RequestParam String name) {return serviceFeign.sayHello(name);} }
总结:
Spring Cloud在服务发现和注册上其实很多坑,上面展示的只是核心代码,最全的还是参考例子代码进行调试。
Maven示例:
https://github.com/easonjim/spring-cloud-demo/tree/master/ZooKeeper
参考:
http://theseus.iteye.com/blog/2366237
http://www.cnblogs.com/skyblog/p/5133752.html
http://blog.csdn.net/mn960mn/article/details/51803703
http://www.jianshu.com/p/775c363d0fda
用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现相关推荐
- 如何优化Spring Cloud微服务注册中心架构?
作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...
- 搭建基于Spring Cloud的微服务应用
原文链接 在2017云栖大会-上海峰会上阿里云技术专家李斌做了题为<搭建基于spring Cloud的微服务应用>的分享.随着时代的发展,用户对于应用服务的要求越来越高,单体应用已经无法满 ...
- 基于 Spring Cloud 的服务治理实践
http://www.infoq.com/cn/articles/spring-cloud-based-service-governance 大家好,我是来自贝壳金控的赵文乐,目前主要从事架构方面的工 ...
- 贝壳金控赵文乐:基于 Spring Cloud 的服务治理实践
大家好,我是来自贝壳金控的赵文乐,目前主要从事架构方面的工作.今天我想跟大家分享<基于 Spring Cloud 的服务治理实践>.我先简单向大家介绍一下服务治理的概念,然后介绍实际案例中 ...
- Spring cloud实现服务注册及发现
为什么80%的码农都做不了架构师?>>> 服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访 ...
- Spring Cloud之服务注册与发现机制
一.复杂与简单并存 1.背景: 到底是复杂好还是简单好,这是一个没有答案的问题,也是一个哲学问题.见仁见智啦.事物整体肯定是向复杂化方向发展,但是向人们呈现时应尽量简单化.用一句话来说就是:功能复杂化 ...
- 基于Spring Cloud Gateway 和 注册中心实现灰度发布
什么是灰度发布? 灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式.在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B ...
- 【Spring Cloud Alibaba】Spring Cloud Alibaba 服务注册与发现实践
1. 简介 服务注册与发现是微服务架构体系中最关键的组件之一.如果尝试着用手动的方式来给每一个客户端来配置所有服务提供者的服务列表是一件非常困难的事,而且也不利于服务的动态扩缩容.Nacos Disc ...
- 引入springcloud报错。common依赖找不到_微服务架构:spring cloud之服务注册和服务发现...
SpringCloud主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路有(Zuul),客户端负载均衡(Ribbon),Archaius,Turbine,Eureka为微服 ...
最新文章
- 光学传输与摄像头光学技术
- 帝国cms后台使用savesufer.js生成大音频audio彩色频谱数据
- 可以获取python整数类型帮助的是什么-Python 的数值类型(整数、长整数、浮点数和复数)...
- SparkStreamingStateful
- golang 中的sort 包
- android getview方法,android 获取view的getLeft(), getRight(), getTop(),... - 简书
- 百度外卖接口调试 C#版
- linux检查磁盘空间使用情况df 命令
- SAP Spartacus store里引用的library是如何编译出来的
- 操作方法:Maven的Spring Boot和Thymeleaf
- QT下添加*.qrc(图标Icon、图像)资源
- 第 19 章 保护方法调用
- 计算机应用专业可以考教师资格证不,电大的大专学历可以考教师资格证吗?
- 为什么JavaScript仅在IE中打开开发人员工具一次后才能工作?
- Nginx下root指令问题
- 大学生计算机考试系统软件,我爱C”《大学计算机基础》考试系统学生端软件使用说明.doc...
- 解析仿人化机器人技术的路径
- 供水供气管道泄漏监测系统原理
- 这年头数学不好,连表情包都看不懂了…
- OpenERP-指定动作视图
热门文章
- 学习笔记Flink(七)—— Flink Kafka插件
- 如何设计登录接口,十分钟内连续登录5次失败,需要等待30分钟才能登录
- java头像交互式差分演变_一种基于交互式差分进化计算的用户知识需求获取方法与流程...
- 树莓派:在ubuntu20-server安装和卸载桌面
- python基础知心得总结_【python】基础学习心得总结(一)
- 3des java ecb_PHP 3DES加密 与JAVA通用 加密模式:ECB
- vue 表格内容跳转页面_Vue项目实战系列文章(一)项目预热
- 查看操作系统版本linux_查看电脑操作系统版本(适用于Mac OS)
- Matlab实用程序--图形应用-区域图形
- python3 os模块