我的Spring Cloud(七):Feign 声明式接口调用

一、什么是Feign

    Feign也是去实现负载均衡,但是它的使用要比Ribbon更加简化,它实际上是基于Ribbon进行了封装,让我们可以通过调用接口的方式实现负载均衡。Feign和Ribbon都是由Netflix提供的,Feign是一个声明式、模板化的Web Service客户端,它简化了开发者编写Web服务客户端的操作,开发者可以通过简单的接口和注解来调用HTTP API,使得开发变得更加简化、快捷。Spring Cloud Feign也是基于Netflix Feign的二次开发,它整合了Ribbon和Hystrix,具有可插拔、基于注解、负载均衡、服务熔断等一系列的便捷功能,也就是说我们在实际开发中可以用Feign来取代Ribbon。

    相比较于Ribbon+RestTemplate的方式,Feign大大简化了代码的开发,Feign支持多种注解,包括Feign注解、JAX-RS注解、Spring MVC注解等,Spring Cloud对Feign进行了优化,整合了Ribbon和Eureka,从而让Feign使用更加方便。

二、Ribbon和Feign的区别

    Ribbon是一个通用的HTTP客户端工具,Feign是基于Ribbon实现的。

三、Feign的优点

    1.Feign是一个声明式的Web Service客户端。

    2.支持Feign注解、Spring MVC注解、JAX-RS注解

    3.Feign是基于Ribbon实现,使用起来更加方便

    4.Feign集成了Hystrix,具备服务熔断的功能

四、实战!

    1.创建Module,配置pom.xml如下:

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

    2.创建配置文件application.yml,配置如下:

server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true

    3.创建启动类,代码如下:

package com.zing;

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

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

}

    注解说明:

        * @EnableFeignClients:声明其为Feign客户端

    4.创建声明式接口,代码如下:

package com.zing.feign;

import java.util.Collection;

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

import com.zing.entity.Student;

@FeignClient(value = "provider")
public interface IFeignService {
	
	@GetMapping("/student/findAll")
	public Collection<Student> findAll();
	
	@GetMapping("/student/index")
	public String index();
}

    5.Handler代码如下:

package com.zing.controller;

import java.util.Collection;

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 com.zing.entity.Student;
import com.zing.feign.IFeignService;

@RestController
@RequestMapping("/feign")
public class FeignHandler {

	@Autowired
	private IFeignService feignservice;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
		return feignservice.findAll();
	}
	
	@GetMapping("/index")
	public String index() {
		return feignservice.index();
	}
	
}

    6.测试feign的负载均衡功能

        (1)分别启动注册中心,两个不同端口的服务提供者,feign,注册中心页面如下:

        (2)多次访问 http://localhost:8050/feign/index ,我们可以看到两个端口地址交替出现,证明Feign实现了负载均衡。如下图:

    7.测试Feign的熔断机制

        (1)我们先停掉所有的服务提供者,只保留注册中心和Feign的服务,打开注册中心如下图:

        (2)再次访问 http://localhost:8050/feign/index 可看到如下图的内容:

        (3)为了不直接暴露错误信息,我们需要添加服务熔断机制,修改application.yml如下:

server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true

        配置说明:

            * feign.hystrix.enable:是否开启熔断机制,默认false。

        (4)创建IFeignService的实现类FeignServiceImpl,在里面定义容错处理机制,通过@Component注解将FeignServiceImpl实例注入到IOC容器中,代码如下:

package com.zing.feign.impl;

import java.util.Collection;

import org.springframework.stereotype.Component;

import com.zing.entity.Student;
import com.zing.feign.IFeignService;

@Component
public class FeignServiceImpl implements IFeignService{

	@Override
	public Collection<Student> findAll() {
		return null;
	}

	@Override
	public String index() {
		return "服务器维护中。。。";
	}

}

        (5)在IFeignService接口定义处定义@FeignClient的fallback属性来做降级处理,设置映射,映射到FeignServiceImpl中去。修改IFeignService后代码如下:

package com.zing.feign;

import java.util.Collection;

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

import com.zing.entity.Student;
import com.zing.feign.impl.FeignServiceImpl;

@FeignClient(value = "provider",fallback = FeignServiceImpl.class)
public interface IFeignService {
	
	@GetMapping("/student/findAll")
	public Collection<Student> findAll();
	
	@GetMapping("/student/index")
	public String index();
}

        (6)重复(1)、(2)步,出现以下界面,证明服务熔断机制起效。如图:

五、总结

    我们在本次的代码中,采用Feign实现了负载均衡和服务熔断。了解了Feign的配置以及使用的基本原理,那么我们再来仔细学习下Hystix容错机制。让我们期待下一篇《我的Spring Cloud(八):Hystrix 容错机制与数据监控》。

    更多精彩内容,敬请扫描下方二维码,关注我的微信公众号【Java觉浅】,获取第一时间更新哦!

猜你喜欢

转载自blog.csdn.net/qq_34942272/article/details/106353680