Spring Cloud fegin实现声明式接口调用

  |  


Spring Cloud fegin实现声明式接口调用


背景

前面我们介绍了Ribbon和Hystrix结合,实现对RestTemplate调用的负载均衡服务降级。而Spring Cloud Fegin在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Spring Cloud Fegin的实现下,我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。SpringCloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。


使用Spring Cloud Fegin

新建Fegin项目

和之前一样,通过spring initializr新建项目,选择导入

Discovery``` 和 ```Fegin```依赖,这里还是用的springboot 1.5.19.RELEASE
1
2
3
4
5
6
7
8
9
10

```properties
<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>

修改启动类

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {

public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}

}

这里主要添加

1
2
3
4
5
6
7
8
9
10

### 创建Fegin Client

```java
@FeignClient("consumer2")
public interface ConsumerService {

@RequestMapping("/hello")
String hello();
}

我们这里还是调用之前的consumer2服务提供的hello接口,上面已经指定好了

1
2
3
4
5
6
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello host:"+instance.getHost()+"server_id"+instance.getServiceId()+"端口:"+instance.getPort());
return "hello";
}

然后在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

```java
package com.dailingnan.feign.controller;

import com.dailingnan.feign.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

@Autowired
ConsumerService consumerService;

@RequestMapping(value="/fegin-consumer")
public String helloConsumer(){
return consumerService.hello();
}

}

启动服务测试

启动之前的Eureka服务、consumer2服务还有fegin服务,全部启动后,服务列表如下

调用fegin

这里可以看到我们能够成功调用consumer2的hello接口,取代了之前通过

1
2
3
4
5
6
7
8
9
10
11
12
13
14

<br>

## Fegin 实现Hystrix

### 修改Fegin Client

```java
@FeignClient(name="consumer2",fallback = HelloServiceFallback.class)
public interface ConsumerService {

@RequestMapping("/hello")
String hello();
}

这里需要指定回调类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

### 实现HelloServiceFallback

```java
package com.dailingnan.feign.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Component
public class HelloServiceFallback implements ConsumerService {
@Override
public String hello() {
return "error hello";
}
}

调用测试类ConsumerController

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class ConsumerController {

@Autowired
ConsumerService consumerService;

@RequestMapping(value="/fegin-consumer")
public String helloConsumer(){
return consumerService.hello();
}

}

此时停止consumer2实例,直接调用,可以触发Hystrix降级

总结

​ 通过Spring Cloud Feign来实现服务调用的方式更加简单了,通过@FeignClient定义的接口来统一的生命我们需要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用即可。由于Feign是基于Ribbon实现的,所以它自带了客户端负载均衡功能,也可以通过Ribbon的IRule进行策略扩展。

文章目录
  1. 1. Spring Cloud fegin实现声明式接口调用
  2. 2. 背景
    1. 2.1. 使用Spring Cloud Fegin
      1. 2.1.1. 新建Fegin项目
      2. 2.1.2. 修改启动类
      3. 2.1.3. 启动服务测试
      4. 2.1.4. 调用测试类ConsumerController
    2. 2.2. 总结
|
本站总访问量 载入天数...载入时分秒...