spring cloud之Feign (四)

什么是Feign

官方解释: 读官方文档,更好的理解

http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

声明性REST客户端:Feign

Feign是一个声明性的Web服务客户端。它使编写Web服务客户端变得更容易。要使用Feign,请创建一个界面并对其进行注释。它具有可插入的注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用了HttpMessageConvertersSpring Web中默认使用的注释。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。

 

Declarative REST Client: Feign

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.

Example spring boot app

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class Application {

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

}

Feign是一个声明性的Web服务客户端。它使编写Web服务客户端变得更容易.

只需要创建一个接口,然后在上面添加注解即可。

参考官网: https://github.com/OpenFeign/feign

Feign是怎么来的

之前大家用ribbon进行负载均衡,功能很强大,甚至可以自己自定义算法

a.大部分的我们都可以介绍,直接调用我们的微服务来进行访问

//    public static final String REST_URL_PREFIX = "http://localhost:8001";

修改为

       public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-MODEL-DEPT";

 

b. 我们目前都习惯面向接口编程,比如我们的Dao接口/WebService接口,这个已经是我们的目前规范

       微服务名字获得调用地址

       就是通过接口+注解,获得我们的调用服务。

       适应社区其他程序员提出的,还是统一的面向接口编程的套路。----Feign

 

只需要创建一个接口,然后再上面添加注解即可。

@Mapper
Public interface UserDao{

}

 

Feign能干什么

Feign旨在使编写Java Http客户端变得更容易。

前面在使用Ribbon + Rest Template时,利用Rest Template对HTTP请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现服务接口的定义。在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用spring cloud Ribbon时,自动封装服务调用客户端的开发量。

 

Feign集成了Ribbon

利用Ribbon维护了SpringServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

 

Feign使用步骤

第一步:

新建springcloud-model-consumer-dept-feign  (面向接口开发)

参考springcloud-model-consumer-dept-80  (Ribbon+RestTemplate )

复制主体工程结构

创建/修改主启动类DeptConsumer80_Feign_App

package com.jiangjy.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient  //和eureka整合并使用客户端
@EnableFeignClients(basePackages = {"com.jiangjy.springcloud"}) //扫描Feign服务的包
@ComponentScan(value = "com.jiangjy.springcloud")
public class DeptConsumer80_Feign_App {

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

   springcloud-model-dept-feign 工程pom.xml,添加对feign的支持

<!-- feign相关jar -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

第二步:

修改springcloud-model-aip工程

     pom.xml文件
 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 -->
  <parent>
    <groupId>com.jiangjy.springcloud</groupId>
    <artifactId>springcloud-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <!-- 当前Module我自己叫什么名字 -->
  <artifactId>springcloud-model-api</artifactId>
  
  <!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		
		<!-- feign相关jar -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>	
	</dependencies>
  
</project>

新建DeptClientService接口并新增注解@FeignClient

详情:

package com.jiangjy.springcloud.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jiangjy.springcloud.pojo.Dept;

@FeignClient(value = "SPRINGCLOUD-MODEL-DEPT")  //面向服务中的注解+接口
public interface DeptClientService {

	@RequestMapping(value = "/dept/getAll",method = RequestMethod.GET)
	public List<Dept> getAll();
	
	@RequestMapping(value = "/dept/getById/{id}", method = RequestMethod.GET)
	public Dept getById(@PathVariable("id") Long id);
	
	@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
	public boolean add(Dept dept);
	
}

公共部分:先mvn  clean  ------》 mvn install

springcloud-model-dept-feign 工程 修改/创建controller包中的DeptController_Consumer.java

添加新建的private DeptClientService service; 接口

package com.jiangjy.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.jiangjy.springcloud.pojo.Dept;
import com.jiangjy.springcloud.service.DeptClientService;

@RestController
public class DeptController_Consumer {
	

	@Autowired
	private DeptClientService service;

	@RequestMapping("/consumer/dept/getAll")
	public List<Dept> getAll(){
		return this.service.getAll();
	}
	
	@RequestMapping("/consumer/dept/getById/{id}")
	public Dept getById(@PathVariable("id") Long id) {
		return this.service.getById(id);
	}
	
	@RequestMapping("/consumer/dept/add")
	public Object add(Dept dept) {
		
		return this.service.add(dept);
	}
}

修改/创建主启动类(见第一步)

 

测试:

启动3个eureka集群

启动3个部门微服务8001/8002/8003

启动Feign自己启动

http://localhost/consumer/dept/getAll

Feign自带负载均衡配置项

小总结:Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate)

该请求发送给Eukeka服务器(http://SRPINGCLOUD-MODEL-DEPT/dept/getAll),

通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。

发布了17 篇原创文章 · 获赞 42 · 访问量 9567

猜你喜欢

转载自blog.csdn.net/jiangjiaoyong/article/details/101049882