在之前讲解了 spring cloud eureka和ribbon,现在开始讲解spring cloud中声明式调用方式feign的相关使用,关于eureka搭建参见 spring cloud eureka和ribbon
搭建consumer-feign,pom依赖如下:
<?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>spring-cloud-test-001</artifactId>
<groupId>com.leo.test</groupId>
<version>1.0.0-snapshot</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-conumer-feign-001</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
consumer-feign模块配置文件 application.yml如下:
spring:
application:
name: service-consumer-feign-001
server:
port: 9020
eureka:
client:
service-url:
defaultZone: http://leo-node:8081/eureka
feign:
hystrix:
enabled: true
搭建启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerFeignApplication.class);
}
}
构造feign调用接口:
import com.leo.test.spring.cloud.test.service.consumer.feign.configuration.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name="service-provider-001",path="/provider/",configuration = FeignConfig.class,fallback = HystrixClient.class)
public interface ConsumerFeignClient {
@GetMapping(value = "/sayHello")
String sayHello();
}
可以看到相比于ribbon调用,ribbon需要手动拼接url来进行访问,而feign则通过声明与调用方一样的方法来进行调用,这样简化了请求的复杂度。
feign重试的配置:
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
public class FeignConfig {
@Bean
public Retryer getFeignRetryer() {
return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1L),5);
}
}
并且在这里我们通过hystrix,指定了如果调用服务失败需要怎么处理,
import org.springframework.stereotype.Component;
@Component
public class HystrixClient implements ConsumerFeignClient{
public String sayHello() {
return "consumer feign error from hystrix";
}
}
编写controller访问层:
import com.leo.test.spring.cloud.test.service.consumer.feign.client.ConsumerFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/consumer/feign")
public class ConsumerController {
@Autowired
private ConsumerFeignClient feignClient;
@RequestMapping("/sayHello")
public String sayHello(){
return feignClient.sayHello();
}
}
.我们启动eureka-server和服务提供端,然后启动consumer-feign: 访问: http://localhost:9020/consumer/feign/sayHello,可以看到负载均衡分别调用,显示如下:
停止其中一个服务提供者,再次访问,显示如下:
到此feign结合hystrix示例完成