springCloud Eureka服务消费者-Feign(Finchley版本)

Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

准备工作

  1. 启动注册中心EurekaServer
  2. 启动服务端Eureka-privider
  3. 将服务端Eureka-privider端口改为9001, 再启动一个实例

启动后, 可以看到服务端有两个实例在运行, 准备工作结束了, 进入主题哦

代码示例

POM.XML

1
2
3
4
5
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>consume project for Spring Cloud</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<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>
<!-- Finchley版本的为openfeigh -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</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

  1. 设置端口为1002
  2. 设置服务名为Erueka-consume-feign
  3. 设置注册中心地址为http://localhost:8761/eureka/
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    server:
    port: 1002

    spring:
    application:
    name: Erueka-consume-feign

    eureka:
    client:
    # 从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒
    registry-fetch-interval-seconds: 5
    service-url:
    # 注册中心地址
    defaultZone: http://localhost:8761/eureka/

启动类

  1. 使用@EnableFeignClients开启Feign
  2. 使用@EnableEurekaClient开启Eureka
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    /**
    * 使用@EnableFeignClients开启Feign
    */
    @EnableFeignClients
    @EnableEurekaClient
    @SpringBootApplication
    public class DemoApplication {

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

编写调用接口

Feign使用的是接口, 具有可拔插的特性

  1. 使用@FeignClient注解来设置调用的生产者的服务名
  2. 设置调用的接口名
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @FeignClient(name = "spring-cloud-producer")
    public interface SchedualServiceHi {

    /**
    * 调用生产者的hi接口
    */
    @RequestMapping(value = "/hi")
    String hi();
    }

消费hi服务

请求服务端的hi接口

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

@Autowired
private SchedualServiceHi schedualServiceHi;

@GetMapping("/feign")
public String hiConsume() {
return schedualServiceHi.hi();
}
}

测试

访问http://localhost:1002/feign, 返回hi I am clouc-producer, i am from port:9000, 说明调用接口正常
多次访问同样和Ribbon示例一样实现了负载均衡。

结语

起风了,唯有努力生存