Feign二:复写Feign的默认配置

Posted achengmu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Feign二:复写Feign的默认配置相关的知识,希望对你有一定的参考价值。

Feign二:复写Feign的默认配置

技术分享图片

1.在启动文件加入feign注解:@EnableFeignClients

FeignApp.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp {


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

  

2.新建一个Feign的默认配置类,并创建一个bean,返回: feign.Contract.Default 默认配置

import org.springframework.context.annotation.Bean;

import feign.Contract;

public class FooConfiguration {

	@Bean
	public Contract feignContract()
	{
		return new feign.Contract.Default();
	}
	
}

  

3.新建Feign客户端调用类,调用第三方接口的,并引入第2步复写的Feign配置

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;

import com.muyang.bootmovie.configs.FooConfiguration;
import com.muyang.bootmovie.entity.User;

import feign.RequestLine;

@FeignClient(name="spring-boot-user", configuration=FooConfiguration.class)
public interface UserFeignClient {

	@RequestLine("GET /simple/{id}")
	public User findById(@PathVariable("id") Long id);
}

  

4.创建controller类,并调用feign客户端类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyang.bootmovie.entity.User;
import com.muyang.bootmovie.feign.UserFeignClient;

import feign.Param;


@RestController
public class UserController {

	@Autowired
	private UserFeignClient userFeignClient;
	
	@GetMapping("/user/{id}")
	public User findById(@Param("id") Long id) {
		return this.userFeignClient.findById(id);
	}
	
	
}

 

5。一些其他参考

user.java

import java.math.BigDecimal;

public class User {

	private  Long id;
	
	private String username;
	
	private String name;
	
	private int age;
	
	private BigDecimal balance;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public BigDecimal getBalance() {
		return balance;
	}

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

  

pom.xml

<?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>

	<artifactId>boot-spring-movie-feign-override</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.muyang</groupId>
		<artifactId>boot-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.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-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>	
		
	</dependencies>

	


</project>

  

 

  

 

以上是关于Feign二:复写Feign的默认配置的主要内容,如果未能解决你的问题,请参考以下文章

Feign自定义配置详解

spring cloud feign覆写默认配置级feign client的日志打印

SpringCloud http客户端Feign -- 自定义Feign的配置(一般情况下需要配置的是日志级别)Feign的配置优化

sprincloud-Feign配置二

微服务5声明式 HTTP 客户端 —— Feign

feign服务间调用超时时间