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

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

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

spring cloud consul整合

發(fā)布時(shí)間:2023/12/10 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring cloud consul整合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文基于spring cloud?Finchley.SR1

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

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

目錄

服務(wù)端

客戶(hù)端

測(cè)試


服務(wù)端

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);}/*** 獲取所有服務(wù)*/@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;} }

客戶(hù)端

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;/*** 從所有服務(wù)中選擇一個(gè)服務(wù)(輪詢(xún))*/@RequestMapping("/discover")public Object discover() {return loadBalancer.choose("springtest-service").getUri().toString();}/*** 獲取所有服務(wù)*/@RequestMapping("/services")public Object services() {return discoveryClient.getInstances("springtest-service");} }

測(cè)試

訪問(wèn)服務(wù)端的/services和/home

訪問(wèn)客戶(hù)端的/services和/discover

Feign調(diào)用實(shí)現(xiàn)

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請(qǐng)求的地址@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);} }

測(cè)試

/home

/test?id=123

?

?

總結(jié)

以上是生活随笔為你收集整理的spring cloud consul整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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