spring cloud 中的feign能做哪些操作

查了一下 spring cloud中的feign自带负载均衡?

feign

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

下面,通过一个例子来展现Feign如何方便的声明对上述computer-service服务的定义和调用。

创建一个Spring Boot工程,配置pom.xml,将上述的配置中的ribbon依赖替换成feign的依赖即可,具体如下:

1

2

3

4

5

扫描二维码关注公众号,回复: 2478723 查看本文章

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.5.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</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-web</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>Brixton.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

在应用主类中通过@EnableFeignClients注解开启Feign功能,具体如下:

1

2

3

4

5

6

7

8

9

10

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class FeignApplication {

public static void main(String[] args) {

SpringApplication.run(FeignApplication.class, args);

}

}

定义compute-service服务的接口,具体如下:

1

2

3

4

5

6

7

@FeignClient("compute-service")//相当于eureka中的注册的ip

public interface ComputeClient {

@RequestMapping(method = RequestMethod.GET, value = "/add")

Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);

}

  • 使用@FeignClient("compute-service")注解来绑定该接口对应compute-service服务
  • 通过Spring MVC的注解来配置compute-service服务下的具体实现。

在web层中调用上面定义的ComputeClient,具体如下:

1

2

3

4

5

6

7

8

9

10

11

12

@RestController

public class ConsumerController {

@Autowired

ComputeClient computeClient;

@RequestMapping(value = "/add", method = RequestMethod.GET)

public Integer add() {

return computeClient.add(10, 20);

}

}

application.properties中不用变,指定eureka服务注册中心即可,如:

1

2

3

4

spring.application.name=feign-consumer

server.port=3333

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:2222/eureka/

启动该应用,访问几次:http://localhost:3333/add

再观察日志,可以得到之前使用Ribbon时一样的结果,对服务提供方实现了均衡负载。

这一节我们通过Feign以接口和注解配置的方式,轻松实现了对compute-service服务的绑定,这样我们就可以在本地应用中像本地服务一下的调用它,并且做到了客户端均衡负载。

猜你喜欢

转载自blog.csdn.net/adsadadaddadasda/article/details/81282550