日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现

發(fā)布時(shí)間:2025/3/21 javascript 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前提:

先安裝好ZooKeeper的環(huán)境,搭建參考:http://www.cnblogs.com/EasonJim/p/7482961.html

說(shuō)明:

可以再簡(jiǎn)單的理解為有兩方協(xié)作,一個(gè)是服務(wù)提供這,另一個(gè)是服務(wù)消費(fèi)者。

搭建實(shí)例:

說(shuō)明:基于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><!-- 健康監(jiān)控 --><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><!-- 負(fù)載均衡 --><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> <!-- 測(cè)試 --><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>

服務(wù)提供者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

說(shuō)明:定義端口,應(yīng)用的名稱,服務(wù)在ZK的路徑,ZK的服務(wù)器地址,其中ZK的服務(wù)器地址可以有多個(gè),用逗號(hào)隔開(kāi)。

HelloController:

服務(wù)接口

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);} }

說(shuō)明:其中要@EnableDiscoveryClient標(biāo)注為服務(wù)發(fā)現(xiàn)注解。

服務(wù)消費(fèi)者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>

說(shuō)明:由于使用了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

說(shuō)明:由于服務(wù)提供者的應(yīng)用名使用了斜杠,所以必須采用依賴關(guān)系spring.cloud.zookeeper.dependencies進(jìn)行別名的選擇,注意,使用了這個(gè)之后要引入actuator健康監(jiān)控組件,不然調(diào)用時(shí)會(huì)報(bào)錯(cuò)。

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);} }

說(shuō)明:@EnableCircuitBreaker為斷路器的支持,@EnableFeignClients為Feign客戶端的支持。

Client的實(shí)現(xiàn):

主要有兩種,基于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";} }

說(shuō)明:@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); }

說(shuō)明:fallback為斷路器回調(diào)。

消費(fèi)服務(wù):

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);} }

總結(jié):

Spring Cloud在服務(wù)發(fā)現(xiàn)和注冊(cè)上其實(shí)很多坑,上面展示的只是核心代碼,最全的還是參考例子代碼進(jìn)行調(diào)試。

?

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

總結(jié)

以上是生活随笔為你收集整理的用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。