pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.lxj</groupId>
<artifactId>micro-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>feign-sleuth-A</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 7074
spring:
application:
name: feign-sleuth-A
zipkin:
base-url: http://localhost:7082
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7070/eureka/
instance:
lease-renewal-interval-in-seconds: 1 #每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
feign:
hystrix:
enabled: true # 默认为true
ribbon:
eureka:
enabled: true # 默认为true。如果设置为false,Ribbon将不会从Eureka中获得服务列表,而是使用静态配置的服务列表。静态服务列表可使用:<client>.ribbon.listOfServers来指定。参考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka
A_Application.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication //SpringBoot应用
@EnableDiscoveryClient //在这表示连接Eureka服务的客户端
@EnableCircuitBreaker //表示允许断路器
@EnableFeignClients
public class A_Application {
public static void main(String[] args) {
new SpringApplicationBuilder(A_Application.class).web(true).run(args);
}
}
AController.java
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class AController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@Autowired
private TestFeignClient testFeignClient;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public String add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return "From feign-sleuth-A, Result is " + r;
}
//A服务调用B服务
@RequestMapping(value="feignSleuthB",method=RequestMethod.GET)
public String feignSleuthB(@RequestParam Integer a,@RequestParam Integer b){
// RestTemplate restTemplate=new RestTemplate();
// return restTemplate.getForObject("http://localhost:7075/add?a="+a+"&b="+b, String.class);
// return restTemplate.getForObject("http://hystrix-ribbon-sleuth-B/add?a="+a+"&b="+b, String.class); //需要负载均衡调才可以
return this.testFeignClient.add(a, b);
}
}
TestFeignClient.java
import org.lxj.spring.cloud.TestFeignClient.HystrixClientFallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "feign-sleuth-B", fallback = HystrixClientFallback.class)
public interface TestFeignClient {
@RequestMapping("/add")
public String add(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
@Component
static class HystrixClientFallback implements TestFeignClient {
private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class);
@Override
public String add(Integer a, Integer b) {
HystrixClientFallback.LOGGER.info("异常发生,进入fallback方法,接收的参数: {},{}", a, b);
return "feign-sleuth-A";
}
}
}