天天看點

SpringCloud (三)、Feign使用示例Feign

Feign

Feign是一個聲明式的Web Service用戶端,它使得編寫Web Serivce用戶端變得更加簡單。我們隻需要使用Feign來建立一個接口并用注解來配置它既可完成。它具備可插拔的注解支援,包括Feign注解和JAX-RS注解。Feign也支援可插拔的編碼器和解碼器。Spring Cloud為Feign增加了對Spring MVC注解的支援,還整合了Ribbon和Eureka來提供均衡負載的HTTP用戶端實作。

Spring Cloud Netflix 的微服務都是以 HTTP 接口的形式暴露的,是以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去調用,而 Feign 是一個使用起來更加友善的 HTTP 客戶端,使用起來就像是調用自身工程的方法,而感覺不到是調用遠端方法

代碼實作

結尾有Github位址的代碼示例

一、使用SpringMVC注解

1、添加依賴

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

2、加注解

在啟動類上加注解

@EnableFeignClients

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

3、建立服務接口類

定義一個

producer

服務的接口類

@FeignClient(name="producer")
public interface FeignClientService {

	@RequestMapping(value="/item/{id}",method=RequestMethod.GET)
	public Object detai(@PathVariable("id") String id);
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Object add(Item item);
}
           

4、控制層調用

@RestController
public class TestController {
	
	@Autowired
	private FeignClientService feignClientService;
	
	@GetMapping("/provider/{id}")
	public Object test(@PathVariable String id) {
		return feignClientService.detai(id);
	}
	
	@GetMapping("/add")
	public Object test(Item item) {
		return feignClientService.add(item);
	}
}
           

5、配置檔案

其實配置檔案

application.yml

沒什麼特殊的

server:
  port: 8903
  
eureka:
  client:
    serviceUrl:
      defaultZone: http://rstyro:[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
spring:
  application:
    name: customer-feign
           

啟動eureka 和兩個生産者與feign,檢視結果,還是實作了負載均衡

SpringCloud (三)、Feign使用示例Feign
SpringCloud (三)、Feign使用示例Feign
SpringCloud (三)、Feign使用示例Feign

上面我們用的是

SpringMVC

的注解,下面我們用feign 預設的注解,檢視Github的位址 裡面介紹了基本用法

二、使用預設的注解

1、自定義一個配置類FeignConfig

@Configuration
public class FeignConfig {

	/**
	 * 使用它的預設配置
	 * 
	 * @return
	 */
	@Bean
	public Contract feignContract() {
		return new feign.Contract.Default();
	}
	
	/**
	 * 加日志的
	 * http://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#_feign_logging
	 * @return
	 */
	@Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}
           

2、自定義一個服務接口類FeignClientService

@FeignClient(name="producer",configuration=FeignConfig.class)
public interface FeignClientService {

	//https://github.com/OpenFeign/feign 有例子
	@RequestLine("GET /item/{id}")
	public Object detai(@Param("id") String id);
	
	
	@RequestLine("POST /add")
	public Object add(Item item);
}
           

3、調用

@RestController
public class TestController {
	
	@Autowired
	private FeignClientService feignClientService;
	
	
	@GetMapping("/provider/{id}")
	public Object test(@PathVariable String id) {
		return feignClientService.detai(id);
	}
	
	@GetMapping("/add")
	public Object test(Item item) {
		return feignClientService.add(item);
	}
	
}
           

4、配置檔案

如果機子的性能比較差什麼的,第一次請求會報一個請求逾時異常,解決方案看下面配置檔案

server:
  port: 8904
  
eureka:
  client:
    serviceUrl:
      defaultZone: http://rstyro:[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
spring:
  application:
    name: customer-feign-default
   

logging:
  level:
    top.lrshuai.cloud.springcloud.feign.FeignClientService: DEBUG
    
# 解決第一次請求報逾時異常的方案:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix

           
逾時的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
逾時的解決方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
上面的Github代碼位址:demo1、demo2

收工

繼續閱讀