The original address: blog.csdn.net/forezp/arti… This article is from Fang Zhipeng’s blog

In my previous article, I talked about consuming services through RestTemplate+Ribbon. This article focuses on consuming services through Feign.

I. Introduction to Feign

Feign is a declarative pseudo Http client that makes writing Http clients much easier. With Feign, you just create an interface and annotate it. It has pluggable annotations that use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates the Ribbon by default, and when combined with Eureka, implements load balancing by default.

In short:

  • Feign uses interface-based annotations
  • Feign integrates the Ribbon with load balancing capabilities
  • Integrated with Hystrix, with fuse breaker capability

Second, preparation

Continue the previous section, start eureka-server, port 8761; Start service-HI twice with port 8762 and 8773 respectively.

Create a Feign service

Create a new spring-boot project called Serice-Feign, In its POM file, Feign startup depends on Spring-cloud-starter-feign, Eureka startup depends on Spring-cloud-starter-Netflix-Eureka-client, and Web startup depends on Spring-boot -starter-web, the code is as follows:

<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.forezp</groupId>
	<artifactId>service-feign</artifactId>
	<version>0.01.-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-feign</name>
	<description>Demo project for Spring Boot</description>


	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>spring-cloud</artifactId>
		<version>0.01.-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
	</dependencies>

</project>
Copy the code

In the engineering application configuration file. The yml file, specify the program called service – feign, port number is 8765, the registered service address for http://localhost:8761/eureka/, the code is as follows:

Port =8765 port=8765 port=8765 Name =service-feign eureka.client. Register-with-eureka =true Eureka. Client. The fetch - registry = true # # registration service center configuration eureka. Client. The service - url. DefaultZone = http://localhost:8761/eureka/Copy the code

Add the @enableFeignClients annotation to the ServiceFeignApplication startup class to enable Feign:

package com.forezp.servicefeign;

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
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

	public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); }}Copy the code

Define a Feign interface to specify which service to invoke via @FeignClient (” service name “). For example, the “/hi” interface of the service-hi service is called in code like this:

package com.forezp.servicefeign;

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 = "service-hi")
public interface SchedualServiceHi {

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}
Copy the code

In the Controller layer of the Web layer, a “/hi” API is exposed to consume the service through the Feign client SchedualServiceHi defined above. The code is as follows:

package com.forezp.servicefeign.controller;

import com.forezp.servicefeign.SchedualServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiController {


    // The compiler reported an error. Because this Bean is injected at startup time, the compiler does not sense it and therefore reports an error.
    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        returnschedualServiceHi.sayHiFromClientOne( name ); }}Copy the code

Launch the program and visit http://localhost:8765/hi multiple times? Name =forezp, browser alternate display:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

Feign source code parsing: blog.csdn.net/forezp/arti…

This article source download: gitee.com/tangzhengfe…

5. Reference materials

This article refers to the following:

Blog.csdn.net/forezp/arti…

Cloud. Spring. IO/spring – clou…