前提:

先安装好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"?>
<project
xsi: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"?>
<project
xsi: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 { @Autowired
private 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 { @Autowired
private HelloService helloService; @Autowired
private 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实现服务注册与发现的更多相关文章

  1. 如何优化Spring Cloud微服务注册中心架构?

    作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...

  2. spring cloud(服务注册中心及服务提供者——初学一)

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  3. Spring cloud实现服务注册及发现

    服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...

  4. Spring Cloud 之 服务注册与发现

    作为微服务框架,提供服务注册发现是最基本的功能.Spring Cloud 针对服务注册发现 提供了 Eureka版本的实现 .Zookeeper版本的实现.Consul版本的实现.由于历史原因 Eur ...

  5. spring cloud(二)服务(注册)中心Eureka

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  6. Spring Cloud 之服务注册中心高可用

    服务注册中心高可用 服务注册中心 eureka-server 高可用实施 版本 Spring Boot 版本 # Spring Boot 版本: <parent> <groupId& ...

  7. Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼

    Spring Cloud并不与Spring MVC类似是一个开源框架,而是一组解决问题的规范(个人理解).解决哪些问题呢?如下: 1)服务管理:⾃动注册与发现.状态监管 2)服务负载均衡 3)熔断 4 ...

  8. SpringCloud的服务注册中心(四)- 高可用服务注册中心的搭建

    一.双 服务注册注册中心 1.服务注册中心的服务端 - EurekaServer 1.1.EurekaServer1 String.application.name=eureka-server ser ...

  9. Spring Cloud Eureka 服务注册中心(二)

    序言 Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Clie ...

随机推荐

  1. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  2. 数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension Req ...

  3. 转 mysql 5.7版本修改编码为utf-8

    刚开始学习MySQL,下载的是官网最新版本 5..7.14,使用cmd输入中文时报错,于是开始修改mysql默认编码(windows下) 首先通过 show variables like 'chara ...

  4. 【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  5. .Net实战之反射外卖计费

    场景 叫外卖支付,可以有以下优惠: 1.  满30元减12 2.  是会员减配送费,比如5元 3.  优惠券 …. 问题? 如何在不改代码的情况下更灵活的去控制优惠的变化??? 有些代码与实际业务可能 ...

  6. jQuery伪分页效果

    如图,我们首先分析在一个页面存放4条内容,其余的超出隐藏(因为这里没有后台数据,所以我们把内容‘写死’),然后就是下面两个按钮(这里我们不用button,因为button有自带的提交功能),然后我们可 ...

  7. 全志A33平台编译linux(分色排版)sina33

    全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...

  8. key-value键值型数据库:Redis

    key-value键值型数据库:Redis redis Redis是in-memory型(内存型)的键值数据库,数据在磁盘上是持久的,键类型是字符串,值类型是字符串.字符串集合(Set).sorted ...

  9. golang zip 压缩,解压(含目录文件)

    每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...

  10. codeforces_456C_dp

    链接:http://codeforces.com/problemset/problem/456/C C. Boredom time limit per test 1 second memory lim ...