SpringCloud Tutorial 3, the usage of Feign;SpringCloud系列教程3,Feign的使用

显然,Spring Cloud项目除了生产者,还需要有消费者,Spring CLoud提供了两种方式的消费者,Ribbon和Feign。本文介绍Feign的使用。
Obviously, there are not only producers but also consumers in Spring Cloud projects. it provides two categories of producers, Ribbon and Feign. This blog talks about the usage of Feign.
首先,新建一个子模块,添加如下依赖。
Firstly, create a child moudle, and add the following dependencies.
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
<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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
第二,创建启动类。
Secondly, create the start class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.gerry.springcloudtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignConsumerApplication.class, args);
    }
}
第三,创建并设置application.yml
Thirdly, create and set the application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
  application:
    name: FEIGNCONSUMER

server:
  port: 8763  #the port on which the application runs

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
第四,创建CalculateService,声明接口的出入参数。并且可以声明,接口调用失败的返回。
Fourth, create CalculateService to declare the request and result params. Besides, you can also customize the result if the API fails.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.gerry.springcloudtest.feignservice;

import com.gerry.springcloudtest.feignservice.fallback.CalculateServiceFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "PRODUCER1",fallback = CalculateServiceFallBack.class)
public interface CalculateService {
    @RequestMapping(value = "/calculate/add",method = RequestMethod.GET)
    int add(
            @RequestParam(value = "a") int a,
            @RequestParam(value = "b") int b
    );
}

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.gerry.springcloudtest.feignservice.fallback;

import com.gerry.springcloudtest.feignservice.CalculateService;
import org.springframework.stereotype.Component;

@Component
public class CalculateServiceFallBack implements CalculateService {

    @Override
    public int add(int a, int b) {
        throw new RuntimeException("CalculateService.add fallback");
    }
}
第五,创建RestController,创建API接口并实现逻辑
Fifth, create a RestController to create the API and realize the logic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.gerry.springcloudtest.controller;

import com.gerry.springcloudtest.feignservice.CalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    CalculateService calculateService;

    @RequestMapping(value = "/test/add",method = RequestMethod.GET)
    public int add(int a,int b){
        return calculateService.add(a,b);
    }
}
最后,启动项目,浏览器查看http://127.0.0.1:8763/test/add?a=1&b=2,查看返回值是否正常,确认是否使用成功
Finally, run the application, and open http://127.0.0.1:8763/test/add?a=1&b=2 in the browser to check the result, so that you can know whether Feign is used successfully.
项目已托管在Github,地址https://github.com/tzry/springclouddemo
The project is hosted on Github, the url is https://github.com/tzry/springclouddemo

Leave a Reply

Your email address will not be published. Required fields are marked *