首先引入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>SpringCloudTest</artifactId>
<groupId>com.xc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>EurekaService_feign_hystrix</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
</project>
package com.xc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class Feign_hystrixAPP {
public static void main(String args[]){
SpringApplication.run(Feign_hystrixAPP.class,args);
}
}
写一个feign客户端
package com.xc.service;
import com.xc.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="cloud-test")
public interface FeignService {
@RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
public String sayHello(@PathVariable("words") String words);
}
feign与Hystrix结合使用十分简单,只需要给feign写一个实现类 然后在feign客户端 填写回调地址:
实现类如下:
package com.xc.service;
import org.springframework.stereotype.Component;
@Component
public class TestFallBack implements FeignService{
@Override
public String sayHello(String words) {
return "TestFallBack";
}
}
@component注解不加 启动会报错
然后feign客户端:
package com.xc.service;
import com.xc.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="cloud-test",fallback = TestFallBack.class)
public interface FeignService {
@RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
public String sayHello(@PathVariable("words") String words);
}
写一个Rest服务:
package com.xc.controller;
import com.xc.entity.User;
import com.xc.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignHystrixController {
@Autowired
private FeignService feignService;
@RequestMapping(value="/getuser/{words}",method = RequestMethod.GET)
public String getUser(@PathVariable("words")String words){
return feignService.sayHello(words);
}
}
OK,测试代码已经写好,开始测试:
1,访问接口:http://localhost:7778/getuser/haha
返回正常。
2.现在我们断掉cloud-test微服务,再访问:http://localhost:7778/getuser/haha
成功跳到失败回调方法。。测试完成!