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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现

發布時間:2023/12/20 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

配置映射主機名和IP

為每臺虛擬主機的/etc/hosts文件加上如下內容,服務發現需要用到默認主機名作為訪問地址,這樣可以不用為每個服務實例配置IP

192.168.253.30 megumi-30 192.168.253.31 megumi-31 192.168.253.32 megumi-32

服務提供方

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? (這里配置應用程序的名稱,也就是服務的名稱,用于Feign查找服務,啟用服務發現和注冊)

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: true

4. App.java 用@EnableDiscoveryClient注解配置啟用服務發現

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

?

服務消費方

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? 這里選擇不注冊服務

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客戶端和服務發現

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客戶端調用的服務名稱,與服務提供方yml配置的spring.application.name一致,注意這里雖然能用RequestMapping注解,但是可能引發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();}}

?

?

服務啟動后,使用zkCli運行 ls /services/demo-service ,可以看到服務的各實例已經被注冊到這里。

使用瀏覽器訪問消費方的 /api/demo/remote-instance,不停刷新可以看到值是變化的,說明客戶端負載均衡已經在正常工作了。

?

轉載于:https://www.cnblogs.com/karascanvas/p/7521942.html

總結

以上是生活随笔為你收集整理的Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。