Spring Cloud Tutorial No. 3: feign

Keywords: Programming Spring Maven Apache Java

The last article described how to consume services through RestTemplate+Ribbon. This article focuses on how to consume services through Feign.

Introduction to Feign

Feign is a declarative pseudo-Http client that makes it easier to write Http clients. With Feign, you only need to create an interface and annotate it. It has pluggable annotation features and can use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates Ribbon by default and combines with Eureka to achieve load balancing by default.

In short:

  • Feign uses interface-based annotations
  • Feign integrates ribbon

II. Preparations

Continue to use the previous section of the project, start eureka-server, port 8761; start service-hi twice, port 8762, 8773, respectively.

Create a feign service

A new spring-boot project, named serice-feign, introduces Feign into its pom file with the following codes: spring-cloud-starter-feign, spring-cloud-starter-eureka for Eureka and spring-boot-starter-web for Web:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.free</groupId>
	<artifactId>serice-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>serice-feign</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR3</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>
		<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>

In the engineering configuration file application.yml file, the specified program is named service-feign, the port number is 8765, and the service registration address is http://localhost:8761/eureka/. The code is as follows:

server:
  port: 8765

spring:
  application:
    name: service-feign

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

In the program startup class ServiceFeignApplication, add the @EnableFeignClients annotation to open Feign functions:

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SericeFeignApplication {

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

	}

}

Define a feign interface to specify which service to call through @ FeignClient ("service name"). For example, the "/hi" interface of the service-hi service is invoked in the code. The code is as follows:

@Service
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

In the controller layer of the Web layer, an API interface of "/ hi" is exposed, and services are consumed through the Feign client SchedualService Hi defined above. The code is as follows:

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

Start the program, visit http://localhost:8765/hi?name=zz many times, browser alternately shows:

hi zz,i am from port:8762

hi zz,i am from port:8763

Feign source code parsing: http://blog.csdn.net/forezp/article/details/73480304

This article source code download: https://gitee.com/freesystem/SpringCloudDemo/tree/master/serice-feign

V. References

spring-cloud-feign

Posted by dancing dragon on Wed, 25 Sep 2019 20:57:22 -0700