spring cloud consul整合
生活随笔
收集整理的這篇文章主要介紹了
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: falseapplication.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: 10000bootstrap.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: trueApplication.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)題。
- 上一篇: 保存的离线网页总是自动跳转
- 下一篇: 本地仓库推送到远程仓库:fatal: r