【SpringCloud】(九):Feign的自定義配置

00潤物無聲00發表於2017-08-13

  上篇文章介紹了Feign以及它的基本使用,下面我們來完成Feign的定義配置,可以類比Ribbon的自定義的配置方式;



  配置中也存在@Configuration和@ComponentScan衝突,不要把Configuration的類配置在@ComponentScan能掃描的路徑


根據文件內容來自定義Feign;

1.啟動類

package com.dynamic.cloud;

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;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ComsumerMovieFeignApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ComsumerMovieFeignApplication.class, args);
	}
}

2.Controller

package com.dynamic.cloud.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.dynamic.cloud.entity.User;
import com.dynamic.cloud.feign.FeignClient2;
import com.dynamic.cloud.feign.UserFeignClient;

@RestController
public class MovieController {
	@Autowired
	private UserFeignClient userFeignClient;	
	@Autowired
	private FeignClient2 feignClient2;	
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Long id)
	{
		return this.userFeignClient.findById(id);
	}
	
	@GetMapping("/{serviceName}")
	public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName")String serviceName)
	{
		return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
	}
}

Controller中注入了兩個FeignClient ,分別配置不同的@Configuration。

一個是UserFeignClient:呼叫使用者微服務。

一個是FeignClient2:通過傳入使用者微服務的ServicId 從Eureka中獲取資訊。


UserFeignClient

package com.dynamic.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import com.dynamic.cloud.entity.User;
import com.dynamic.config.FooConfiguration;
import feign.Param;
import feign.RequestLine;

@FeignClient(name="microservice-provider-user",configuration = FooConfiguration.class)
public interface UserFeignClient {
	
	@RequestLine("GET /simple/{id}") 
	public User findById(@Param("id") Long id); 

}

配置類:FooConfiguration

package com.dynamic.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;

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

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

 Logger.Level.FULL設定列印日誌的級別


FeignClient2

package com.dynamic.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dynamic.config.AuthConfiguration;

@FeignClient(name="xxx",url="http://localhost:8761/",configuration=AuthConfiguration.class)
public interface FeignClient2 {
	
	@RequestMapping(value="/eureka/apps/{serviceName}")
	public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);

}

配置類:AuthConfiguration

設定 Eureka Server的訪問的使用者名稱和密碼

package com.dynamic.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.auth.BasicAuthRequestInterceptor;

@Configuration
public class AuthConfiguration {
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user","pass123");
    }
}







相關文章