1. 程式人生 > >Feign二:復寫Feign的默認配置

Feign二:復寫Feign的默認配置

new pid ext jar rop alt tor 9.png inf

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的默認配置