SpringCloud系列文章同步网址点击阅读原文直达,欢迎交流探讨。
Feign是什么?
Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。
Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
总结:Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,只需要创建一个接口,然后在上面添加注解即可。
Feign能干什么?
Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。
但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。
所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。
Feign之工程构建
1.参考microservicecloud-consumer-dept-80 来新建microservicecloud-consumer-dept-feign工程
1).将microservicecloud-consumer-dept-80工程下的java包下的所有类以及配置文件复制一份到microservicecloud-consumer-dept-feign
2).在microservicecloud-consumer-dept-feign工程下修改主启动类名字:DeptConsumer80_Feign_App
package hhf.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
2.microservicecloud-consumer-dept-feign工程pom.xml修改,主要添加对feign的支持
1).添加microservicecloud-consumer-dept-feign工程下pomjar包:
?xml version="1.0" encoding="UTF-8"?
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"
parent
artifactIdmicroservicecloud/artifactId
groupIdcom.hhf.springcloud/groupId
version0.0.1-SNAPSHOT/version
/parent
modelVersion4.0.0/modelVersion
artifactIdmicroservicecloud-consumer-dept-feign/artifactId
dependencies
dependency!-- 自己定义的api --
groupIdcom.hhf.springcloud/groupId
artifactIdmicroservicecloud-api/artifactId
version${project.version}/version
/dependency
!-- feign相关 --
dependency
groupIdorg.springframework.cloud/groupId
artifactIdspring-cloud-starter-feign/artifactId
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-web/artifactId
/dependency
!-- 修改后立即生效,热部署 --
dependency
groupIdorg.springframework/groupId
artifactIdspringloaded/artifactId
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-devtools/artifactId
/dependency
!-- Ribbon相关 --
dependency
groupIdorg.springframework.cloud/groupId
artifactIdspring-cloud-starter-eureka/artifactId
/dependency
dependency
groupIdorg.springframework.cloud/groupId
artifactIdspring-cloud-starter-ribbon/artifactId
/dependency
dependency
groupIdorg.springframework.cloud/groupId
artifactIdspring-cloud-starter-config/artifactId
/dependency
/dependencies
/project
3.修改microservicecloud-api工程
1).在microservicecloud-api工程下添加pom文件的jar包(其他jar包不变):
dependency
groupIdorg.springframework.cloud/groupId
artifactIdspring-cloud-starter-feign/artifactId
/dependency
2).在microservicecloud-api工程下新建DeptClientService接口并新增注解@FeignClient
package com.hhf.springcloud.service;
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.hhf.springcloud.entities.Dept;
import java.util.List;
@FeignClient(value = "MICROSERVICECLOUD-DEPT") //向哪个微服务进行面向接口feign的编码
public interface DeptClientService {
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public ListDept list();
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(Dept dept);
}
4.在microservicecloud-consumer-dept-feign工程下修改Controller的DeptController_Consumer类,添加上一步新建的DeptClientService接口。
1).修改Controller的DeptController_Consumer类,并将原先的代码删除,添加以下内容:
package hhf.springcloud.controller;
import com.hhf.springcloud.entities.Dept;
import com.hhf.springcloud.service.DeptClientService;
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 java.util.List;
@RestController
public class DeptController_Consumer {
@Autowired
private DeptClientService service;
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return this.service.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public ListDept list()
{
return this.service.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object add(Dept dept)
{
return this.service.add(dept);
}
}
5.在microservicecloud-consumer-dept-feign工程下修改DeptConsumer80_Feign_App主启动类:
1).在DeptConsumer80_Feign_App主启动类添加以下内容:
package hhf.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
@EnableFeignClients(basePackages= {"com.hhf.springcloud"})
@ComponentScan("com.hhf.springcloud")
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
6.终于到测试阶段了:
1).启动3个eureka集群配置
(microservicecloud-eureka-7001系列)。
2).启动3个Dept微服务并各自测试通过(microservicecloud-provider-dept-8001系列)。
3).启动microservicecloud-consumer-dept-feign工程。
请在浏览器输入:
http://localhost/consumer/dept/list
推荐大而全的【后端技术精选】