日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

spring cloud consul整合

發布時間:2023/12/10 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring cloud consul整合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文基于spring cloud?Finchley.SR1

consul如何搭建可以看文章consul docker方式搭建

本文章源碼位置:https://github.com/wanghongqi/springcloudconsul_test/

目錄

服務端

客戶端

測試


服務端

pom.xml

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

?

application.yml

server:port: 9201spring:application:name: springtest-servicecloud:consul:host: 192.168.140.128port: 8500discovery:register: truehostname: 10.1.69.72serviceName: ${spring.application.name}port: ${server.port}healthCheckPath: /homehealthCheckInterval: 15stags: urlprefix-/${spring.application.name}instanceId: ${spring.application.name}

?application.java

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient @RestController @SpringBootApplication public class SpringtestServerApplication {@Autowiredprivate DiscoveryClient discoveryClient;public static void main(String[] args) {SpringApplication.run(SpringtestServerApplication.class, args);}/*** 獲取所有服務*/@RequestMapping("/services")public Object services() {return discoveryClient.getServices();}@RequestMapping("/home")public String home() {return "Hello World";}@RequestMapping("/test")public String test(String id) {return "test"+id;} }

客戶端

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies>

application.yml

server:port: 9202spring:application:name: springtest-clientcloud:consul:host: 192.168.140.128port: 8500discovery:register: false

application.java

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient @RestController @SpringBootApplication public class SpringtestClientApplication {public static void main(String[] args) {SpringApplication.run(SpringtestClientApplication.class, args);}@Autowiredprivate LoadBalancerClient loadBalancer;@Autowiredprivate DiscoveryClient discoveryClient;/*** 從所有服務中選擇一個服務(輪詢)*/@RequestMapping("/discover")public Object discover() {return loadBalancer.choose("springtest-service").getUri().toString();}/*** 獲取所有服務*/@RequestMapping("/services")public Object services() {return discoveryClient.getInstances("springtest-service");} }

測試

訪問服務端的/services和/home

訪問客戶端的/services和/discover

Feign調用實現

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.yml

server:port: 9203 feign:hystrix:enabled: true ribbon:ReadTimeout: 30000ConnectTimeout: 15000 hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 10000

bootstrap.yml

spring:application:name: springtest-feigncloud:consul:host: 192.168.140.128port: 8500discovery:instance-id: ${spring.application.name}:${server.port}service-name: ${spring.application.name}config:discovery:enabled: trueservice-id: springcloud-config-serverfail-fast: true

Application.java

@EnableFeignClients @EnableCircuitBreaker @EnableHystrix @EnableDiscoveryClient @SpringBootApplication public class SpringtestFeignApplication {public static void main(String[] args) {SpringApplication.run(SpringtestFeignApplication.class, args);} }

MyFeignClient

@FeignClient(name = "springtest-service",fallback = HystrixFeignFallback.class) public interface MyFeignClient {//這里是使用feign請求的地址@RequestMapping("/home")String home();@RequestMapping("/test")String test(@RequestParam(value = "id")String id); }

HystrixFeignFallback? 斷路器

@Component public class HystrixFeignFallback implements MyFeignClient {@Overridepublic String home(){return "Hystrix home";}@Overridepublic String test(String id) {return "Hystrix test"+id;} }

TestController.java

@RestController public class TestController {@AutowiredMyFeignClient myFeignClient;@RequestMapping("/home")public String home(){return myFeignClient.home();}@RequestMapping("/test")public String test(String id){return myFeignClient.test(id);} }

測試

/home

/test?id=123

?

?

總結

以上是生活随笔為你收集整理的spring cloud consul整合的全部內容,希望文章能夠幫你解決所遇到的問題。

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