The ninth chapter Spring Cloud Feign declarative call service

In the Spring Cloud Netflix stack, each microservice exposes its own services in the form of HTTP interface, so HTTP client must be used when calling remote services. We can use the native URLConnection of JDK, HttpClient of Apache and asynchronous HTTP of Netty Client, Spring's RestTemplate. But the most convenient and elegant thing to use is feign. In real projects, services are invoked declaratively. Instead of storing two copies of the same model and api definitions in the customer service end and the service end. Feign encapsulates RestTemplate to help us define and implement the definition of dependent service interface. Spring Cloud Feign based on Netflix Feign Implemented, organize Spring Cloud Ribbon And Spring CloudHystrix, and the implementation of declarative Web Services client definition.

The project structure is shown in the figure:

OrderController class

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.User;
import com.example.demo.feign.UserFeignClient;

//Declarative service
public class OrderController {
	  private UserFeignClient userFeignClient;

	  public User findById(@PathVariable Long id) {
	    return this.userFeignClient.findById(id);

	  public User testPost(User user) {
	    return this.userFeignClient.postUser(user);

	  public User testGet(User user) {
	    return this.userFeignClient.getUser(user);

User class

package com.example.demo.entity;

import java.math.BigDecimal;

public class User {
  private Long id;

  private String username;

  private String name;

  private Short age;

  private BigDecimal balance;

  public Long getId() {

  public void setId(Long id) { = id;

  public String getUsername() {
    return this.username;

  public void setUsername(String username) {
    this.username = username;

  public String getName() {

  public void setName(String name) { = name;

  public Short getAge() {
    return this.age;

  public void setAge(Short age) {
    this.age = age;

  public BigDecimal getBalance() {
    return this.balance;

  public void setBalance(BigDecimal balance) {
    this.balance = balance;


UserFeignClient class

package com.example.demo.feign;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.entity.User;

////Declarative service
@FeignClient("spring-cloud-user") // The name of which service to call
public interface UserFeignClient {

	@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
	public User findById(@PathVariable("id") Long id); // 1. @GetMapping does not support 2
														// @Setting value of PathVariable

	@RequestMapping(value = "/testPost", method = RequestMethod.POST)
	public User postUser(@RequestBody User user);

	// The request will not succeed. As long as the parameter is a complex object and even if the GET method is specified, feign will still send the request in the POST method
	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public User getUser(User user);

SpringCloudOrderApplication class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

 * Declarative service

@EnableFeignClients//Represent as a Fegin client
public class SpringCloudOrderApplication {
	public static void main(String[] args) {, args);

application.yml configuration

    name: spring-cloud-order-feign    #Name of the microservice
  port: 7906  #Microservice port number
      enabled: true   # Open health check
      defaultZone: http://user:123456@localhost:8761/eureka ා URL of eureka service
    prefer-ip-address: true
# Solution to the timeout exception of the first request:
            timeoutInMilliseconds: 5000
# Or:
# hystrix.command.default.execution.timeout.enabled: false
# Or:

# Timeout issue:
# Timeout solution:
# hystrix to configure:

pom.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""




		<!-- Encoding when copying files -->  
		  <!-- Compile time encoding -->  
        <!-- jdk Edition -->  

		<!-- Registration Center -->
		<!-- This library allows us to access a lot of information about the application,Include:/env,/info,/metrics,/health etc. -->
		<!-- Add declarative feign rely on -->
		<!-- For hot deployment -->
		<!-- Version dependency management, so you do not need to specify to add a dependency later version -->

		<!-- To think integration-test Provide support. -->

Tags: Spring Maven encoding Apache

Posted on Thu, 30 Apr 2020 22:26:04 -0700 by adksga