Spring Cloud Feign

一、FeignClient简介

1.JAVA常见接口调用方式

  • Apache:Http Client
  • Netty:HTTP Client
  • JDK:URL Connection
  • Spring:RestTemplate

2.Feign简介

Spring Cloud开发微服务应用时中,
各个微服务提供者都是以 HTTP接口的形式对外提供服务
因此服务消费者要通过HTTP Client的方式访问服务。

Feign是一种声明式模板化HTTP客户端
Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。
而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。
开发者完全感知不到这是远程方法,
更感知不到这是个HTTP请求。

SpringCloud对Feign进行了封装,
使其支持SpringMVC标准注解和HttpMessageConverters。

3.Feign特性

  • 可插拔的注解支持,包括Feign注解和JAX-RS注解
  • 支持可插拔的HTTP编码器和解码器
  • 支持Hystrix和它的Fallback
  • 支持Ribbon的负载均衡
  • 支持HTTP请求和响应的压缩

二、FeignClient工作原理

  • 1.首先通过@EnableFeignCleints注解开启FeignCleint
  • 2.根据Feign的规则实现接口,并加@FeignCleint注解
  • 3.程序启动后,会进行包扫描,扫描所有的@ FeignCleint的注解的类,并将这些信息注入到ioc容器中
  • 4.当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
  • 5.RequesTemplate在生成Request
  • 6.Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
  • 7.最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡

三、FeignClient的使用

1.Eureka-Server注册中心

1)application.yml配置
	server:
	  port: 1111
	spring:
	  application:
		name:eureka-server
	eureka:
	  instance:
		hostname: localhost
	  client:
		#不注册自己
		register-with-eureka: false
		#不获取eureka服务器注册信息
		fetch-registry: false
		server-url:
		  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2)Application配置
	@EnableEurekaServer
	@SpringBootApplication
	public class EurekaServerApplication {
	  public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class,args)
	  }
	}

2.Eureka-Producer配置

1)application.yml配置
	server:
	  port: 1112
	spring:
	  application:
		name: eureka-producer
	eureka:
	  client:
		server-url:
		  defaultZone: http://localhost:1111/eureka/
2)Application配置
	@EnableDiscoveryClient
	@SpringBootApplication
	public class EurekaProducerApplication {
	  public static void main(String[] args) {
		SpringApplication.run(EurekaProducerApplication.class,args)
	  }
	}
3)提供服务配置
	@RestController
	public class HelloController {
	  @GetMapping("/hello")
	   public String hello(@RequstParam String name) {
		  return "hello" + name + ", I'm eureka producer service!";
	   }
	}

3.Eureka-Consumer配置

1)application.yml配置
	server:
	  port: 1113
	spring:
	  application:
		name: eureka-consumer
	eureka:
	  client:
		server-url:
		  defaultZone: http://localhost:1111/eureka
2)Application配置
//声明是一个Feign的客户端
@EnableFeignClients
//使得注册中心发现该服务
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
  public static void main(String[] args) {
	SpringApplication.run(EurekaConsumerApplication.class,args)
  }
}
3)调用服务配置
  • Interface 声明
//Feign声明的客户端
//value或name参数值,表明它将去调用服务提供商的eureka-producer服务
@FeignClient(name="eureka-producer")
public interface HelloRemote {
   @RequstMapping("/hello")
	String hello(@RequstParam(value="name") String name);
}
  • Controller 调用
   	@RestController
   	public class ConsumerController {
   		@Autowired
   		HelloRemote helloRemote;
   	
   		@RequestMapping("/hello/{name}")
   		public String hello(@PathVariable("name") String name) {
   			return helloRemote.hello(name);
   		}
   	}

四、Feign的HTTP连接

1.默认连接——》JDK的URLConnection

特点:没有连接池,对每个地址会保持一个长连接,即利用HTTP的persistence connection

2.替换连接——》Apache的HTTP Client

特点:获取连接池、超时时间等与性能息息相关的控制能力
注意:Spring Cloud从Brixtion.SR5版本开始支持这种替换

1)pom配置
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>${feign-httpclient}</version>
        </dependency>
2)application.yml配置
feign:
	httpclient:
		enabled:true

五、Feign对Hystrix的支持

1.开启Feign对Hystrix的支持

1)application.yml配置
feign:
  hystrix:
    enabled: true

2.在Feign的基础上添加Hystrix

@FeignClient(name = "这里写服务名称",fallback = "UserServiceHystrix.class")
public interface UserServiceAPI {
	@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
	public BaseResponse<UserInfo> getUserInfo(@RequestParam("userId")Integer userId);
}

3.编写UserServiceHystrix类

@Component
public class UserServiceHystrix implement UserServiceAPI{
  @Override
  public BaseResponse<UserInfo> getUserInfo(Integer userId){
    return new BaseResponse<>().fail("调用服务失败");
  }
}

六、总结

通过Feign, 我们能把HTTP远程调用对开发者完全透明,得到与调用本地方法一致的编码体验。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值