SpringCloud(Feign)

1. Feign介绍

1.1 是什么:

  1. Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
  2. If Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker. Returning a com.netflix.hystrix.HystrixCommand is also available. This lets you use reactive patterns (with a call to .toObservable() or .observe() or asynchronous use (with a call to .queue()).

以上是官方文档给出的说法,Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。它是将Ribbon和Hystrix的功能整合在了一起,可以让我们不再需要显式地使用这两个组件。

1.2 Feign 特性

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

2 使用

2.1 maven依赖

注意:和Hystrix碰到的问题一样,注意一下自己的项目版本,如果版本是SpringBoot2.x.x版本,则应该引入以下依赖‘spring-cloud-starter-openfeign’,因为 ServerPropertiesAutoConfiguration 在SpringBoot 2.x.x 移除了。另一个容易迷惑的版本是’spring-cloud-starter-feign’,引入后会启动报错。

<dependency>
   <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     <version>2.1.2.RELEASE</version>
 </dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
     <version>2.1.2.RELEASE</version>
 </dependency>

不用引入Ribbon和Hystrix的依赖,因为openfeign已经集成这些依赖了,点进去可以看到的
在这里插入图片描述

2.2 配置

在配置文件中加入以下配置即可

server.port=3722
spring.application.name=05-springcloud-service-feign
#eureka的服务注册地址
eureka.client.service-url.defaultZone=http://eureka8085:8085/eureka/,http://eureka8086:8086/eureka/

3 注解

3.1 启动类
package com.kinglong.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients//开启Feign
public class FeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(FeignApplication.class, args);
	}

}
3.2 服务接口层

controller层正常编写即可

package com.kinglong.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 使用Feign的客户端注解绑定远程服务的名称
 * 远程服务的名称可以大写,也可以小写
 *
 *
 * @author haojinlong
 */
@FeignClient("01-springcloud-service-provider")
public interface HelloService {

	/**
	 * 声明一个方法,这个方法就是远程的服务提供者的那个方法
	 *
	 * @return
	 */
	@RequestMapping("/api/hellos/hello")
	public String hello();
	
}

最后启动所有相关服务,就可使用了。

4 负载均衡

Feign的负载均衡:定义一个注解有@FeignClient注解的接口,然后使用@RequestMapping注解到方法上映射远程的REST服务,此方法也是做好负载均衡配置的。具体就是下面的代码

/**
 * 使用fign的客户端注解绑定远程服务的名称
 * 远程服务的名称可以大写,也可以小写
 *
 *
 * @author haojinlong
 * @date 2019/7/297:58
 */
@FeignClient("01-springcloud-service-provider")
public interface HelloService {

	/**
	 * 声明一个方法,这个方法就是远程的服务提供者的那个方法
	 *
	 * @return
	 */
	@RequestMapping("/api/hellos/hello")
	public String hello();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值