Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现
配置映射主機(jī)名和IP
為每臺虛擬主機(jī)的/etc/hosts文件加上如下內(nèi)容,服務(wù)發(fā)現(xiàn)需要用到默認(rèn)主機(jī)名作為訪問地址,這樣可以不用為每個服務(wù)實例配置IP
192.168.253.30 megumi-30 192.168.253.31 megumi-31 192.168.253.32 megumi-32服務(wù)提供方
1.Maven依賴
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-zookeeper-all</artifactId></dependency></dependencies>2. 父模塊的依賴管理
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.3.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-zookeeper-dependencies</artifactId><version>1.1.1.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-netflix-dependencies</artifactId><version>1.3.4.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>io.reactivex</groupId><artifactId>rxjava</artifactId><version>1.3.1</version></dependency></dependencies></dependencyManagement>3. bootstrap.yml? (這里配置應(yīng)用程序的名稱,也就是服務(wù)的名稱,用于Feign查找服務(wù),啟用服務(wù)發(fā)現(xiàn)和注冊)
spring:application:name: demo-servicecloud:zookeeper:connect-string: 192.168.253.30:2181,192.168.253.31:2181,192.168.253.32:2181discovery:enabled: trueregister: true4. App.java 用@EnableDiscoveryClient注解配置啟用服務(wù)發(fā)現(xiàn)
package demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient @SpringBootApplication public class App {public static void main(String[] args) throws Exception {SpringApplication.run(App.class, args);}}5. DemoController.java
package demo;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping(value = "/api/demo") public class DemoController {private static final UUID INSTANCE_UUID = UUID.randomUUID();@GetMapping(value = "/instance")public Object instance() {return INSTANCE_UUID.toString();}}?
服務(wù)消費方
1. Maven依賴 (和提供方同父模塊)
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-zookeeper-all</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies>2. bootstrap.yml? 這里選擇不注冊服務(wù)
spring:application:name: demo-appcloud:zookeeper:connect-string: 192.168.253.30:2181,192.168.253.31:2181,192.168.253.32:2181discovery:enabled: trueregister: false?
3. App.java? 同樣用注解啟用Feign客戶端和服務(wù)發(fā)現(xiàn)
package demo2;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;@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class App {public static void main(String[] args) throws Exception {SpringApplication.run(App.class, args);}}?
4. DemoService.java 用注解聲明Feign客戶端調(diào)用的服務(wù)名稱,與服務(wù)提供方y(tǒng)ml配置的spring.application.name一致,注意這里雖然能用RequestMapping注解,但是可能引發(fā)URL路由注冊沖突的問題。
package demo2;import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value = "demo-service", path = "/api/demo") //@RequestMapping(value = "/api/demo") public interface DemoService {@GetMapping(value = "/instance")String instance(); }?
5. DemoController.java
package demo2;import java.util.UUID;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping(value = "/api/demo") public class DemoController {private static final UUID INSTANCE_UUID = UUID.randomUUID();private final DemoService remoteService;public DemoController(final DemoService remoteService) {this.remoteService = remoteService;}@GetMapping(value = "/remote-instance")public Object remoteInstance() {return this.remoteService.instance();}@GetMapping(value = "/instance")public Object instance() {return INSTANCE_UUID.toString();}}?
?
服務(wù)啟動后,使用zkCli運行 ls /services/demo-service ,可以看到服務(wù)的各實例已經(jīng)被注冊到這里。
使用瀏覽器訪問消費方的 /api/demo/remote-instance,不停刷新可以看到值是變化的,說明客戶端負(fù)載均衡已經(jīng)在正常工作了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/karascanvas/p/7521942.html
總結(jié)
以上是生活随笔為你收集整理的Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#网站部署问题
- 下一篇: JavaEE-05 分页与文件上传