欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud feign無法注入接口的問題

 更新時(shí)間:2023年06月02日 14:57:19   作者:要爭氣  
這篇文章主要介紹了SpringCloud feign無法注入接口的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringCloud feign無法注入接口

接口:

package cn.mn.app.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="sc-provider")
public interface IGoodsClient {
	@RequestMapping(value = "/goods/query",method = RequestMethod.GET)
	String query();
}

controller

package cn.mn.app.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.mn.app.service.IGoodsClient;
@RestController
@RequestMapping("/goods")
public class GoodsController {
	@Autowired
	private IGoodsClient goodsClient;
	@GetMapping("/q")
	public String query(){
		return goodsClient.query();
	}
}

這里把接口直接注入。

啟動(dòng)類:

package cn.mn.app.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication(scanBasePackages = { "cn.mn.app.controller" })
@EnableDiscoveryClient
@EnableFeignClients
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
//    @LoadBalanced
//    @Bean
//    RestTemplate restTemplate(){
//    	return new RestTemplate();
//    }
}

啟動(dòng)報(bào)錯(cuò),無法注入IGoodsClient。

原因是沒有使用@EnableFeignClients掃描指定包。

修改啟動(dòng)類

package cn.mn.app.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication(scanBasePackages = { "cn.mn.app.controller" })
@EnableDiscoveryClient
@EnableFeignClients("cn.mn.app.service")
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
//    @LoadBalanced
//    @Bean
//    RestTemplate restTemplate(){
//    	return new RestTemplate();
//    }
}

正常。

springcloud中feign調(diào)用常見問題

注: 本文基于Springcloud Edgware版本

Feign調(diào)用首次失敗問題

1、Feign簡介

Feign是一個(gè)聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口并注解。

它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解,F(xiàn)eign 整合了Ribbon。

2、原因分析

Feign整合Hystrix組件后,Hystrix默認(rèn)的超時(shí)時(shí)間是1秒,如果超過這個(gè)時(shí)間尚未響應(yīng),將會(huì)進(jìn)入自定義的fallback代碼,往往首次請求會(huì)比較慢(由于Ribbon是懶加載的,在首次請求時(shí),才會(huì)開始初始化相關(guān)類),這個(gè)響應(yīng)時(shí)間可能就大于1秒了,出現(xiàn)調(diào)用失敗

3、解決方案

(1)增加Hystrix的超時(shí)時(shí)間,默認(rèn)為1s

hystrix:
? command:
? ? default:
? ? ? execution:
? ? ? ? isolation:
? ? ? ? ? thread:
? ? ? ? ? ? timeoutInMilliseconds: ?10000

(2)配置餓加載(推薦使用)

ribbon:
? eager-load:
? ? clients: ?project1,project2
? ? enabled: ?true

(3)禁用Hystrix超時(shí)(不推薦使用)

hystrix:
? command:
? ? default:
? ? ? execution:
? ? ? ? timeout:
? ? ? ? ? enabled: ?false

(4)為fegin全局禁用hystrix(此種方式較為極端,不建議使用)

feign:
?? ?hystrix:
?? ??? ?enabled: false

Feign整合Hystrix之后日志顯示問題

1、解決方案

Feign整合Hystrix之后,當(dāng)調(diào)用失敗會(huì)走fallback邏輯,造成日志不顯示,往往我們需要看日志分析原因,進(jìn)行故障排查。

(1)在application中配置,開區(qū)Feign對(duì)Hystrix的支持

feign:
? hystrix:
? ? enabled: true

(2)編寫Feigin的客戶端以及回滾類

在客戶端FeignClient注解配置相對(duì)應(yīng)的回滾類,fallbackFactory = LogFallbackFactory.class,name屬性為注冊中心其他服務(wù)的名稱

/**
?* @description:fegin調(diào)用客戶端
?*
?* @author: LUOYUAN
?* @date: 2019-08-07-10:33
?* @function:
?*/
@FeignClient(name = "eureka-log",path = "/api/log",fallbackFactory = LogFallbackFactory.class)
public interface LogFeignClient {
? ? @RequestMapping(value = "list", method = RequestMethod.GET)
? ? public String logList();
}
/**
?* @description:feign調(diào)用失敗邏輯
?* @author: LUOYUAN
?* @date: 2019-08-07-10:34
?* @function:
?*/
@Slf4j
public class LogFallbackFactory implements FallbackFactory<LogFeignClient> {
? ? @Override
? ? public LogFeignClient create(Throwable throwable) {
? ? ? ? return new LogFeignClient() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public String logList() {
? ? ? ? ? ? ? ? log.info("query log fallback reason was:",throwable);
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? };
? ? }
}

調(diào)用失敗會(huì)打印異常信息

query log fallback reason was:
feign.RetryableException: Connection refused: connect executing GET http://eureka-log/api/log/list
    at feign.FeignException.errorExecuting(FeignException.java:132)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
    at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:109)
    at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
    at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)

Feign調(diào)用時(shí)定義的fallback不生效

Springcloud Dalston之前得到版本,F(xiàn)eign默認(rèn)已經(jīng)開啟了Hystrix熔斷器,從Dalaton版本開始,默認(rèn)關(guān)閉Hystrix支持,需手動(dòng)開啟,在application.yaml中添加如下配置

feign:
? hystrix:
? ? enabled: true

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論