Feign 的超时时间设置

sc-goods-service 服务提供者

GoodsServiceApp

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.api.service")
public class GoodsServiceApp 
{
    public static void main( String[] args )
    {
        SpringApplication.run(GoodsServiceApp.class, args);
    }
    
//    @LoadBalanced
//    @Bean
//    RestTemplate restTemplate(){
//    	return new RestTemplate();
//    }
}

GoodsController

package cn.mn.app.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
public class GoodsController {
	private final Logger logger=LoggerFactory.getLogger(GoodsController.class);
	@GetMapping("/q")
	public String query(){
		return "hello word";
	}
}

配置

server.port=3001
spring.application.name=sc-goods-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

logging.level.org.springframework=DEBUG

sc-goods-api feign接口

package cn.mn.app.api.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-goods-service")
@FeignClient(name = "sc-goods-service", fallback = GoodsFeignClientFallBack.class)
public interface GoodsFeignClient {
	@RequestMapping(value = "/goods/q", method = RequestMethod.GET)
	String query();
}
package cn.mn.app.api.service;

import org.springframework.stereotype.Component;

@Component
public class GoodsFeignClientFallBack implements GoodsFeignClient{

	@Override
	public String query() {
		// TODO Auto-generated method stub
		return "服务超时,请重试";
	}
	
}

sc-consumer 服务消费方

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.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages = { "cn.mn.app.controller","cn.mn.app.api.service" })
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients("cn.mn.app.api.service")
public class ConsumerApp 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConsumerApp.class, args);
    }
    
//    @LoadBalanced
//    @Bean
//    RestTemplate restTemplate(){
//    	return new RestTemplate();
//    }
}
package cn.mn.app.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.api.service.GoodsFeignClient;

@RestController
@RequestMapping("/goods")
public class GoodsController {
	private final Logger logger=LoggerFactory.getLogger(GoodsController.class);
	@Autowired
	private GoodsFeignClient goodsClient;
	@GetMapping("/q")
	public String query(){
		logger.info("================== query  ===========");
		return goodsClient.query();
	}
}

配置

server.port=3002
spring.application.name=sc-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

logging.level.org.springframework=DEBUG

feign.hystrix.enabled=true

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了 上面设置为5秒

猜你喜欢

转载自blog.csdn.net/liangwenmail/article/details/82967614