本文基于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