您的位置:首页 > 编程语言 > Java开发

SpringCloud系列:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

2019-06-13 21:29 597 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/StriveBing/article/details/91897696

2.1、Feign 基本使用

为了方便起见现在将“microcloud-consumer-80”模块复制为了“microcloud-consumer-feign”模块。

1、 【microcloud-consumer-feign】为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

feign 包含了 Ribbon 支持,所以导入了以上的依赖包之后就表示项目之中已经存在有了 ribbon 相关支持库。

2、 【microcloud-service】建立一个新的模块,这个模块专门负责客户端接口的定义;

3、 【microcloud-service】修改 pom.xml 配置文件,引用“microcloud-api”模块,这样就可以使用到 VO 类了;

<dependency>
<groupId>cn.study</groupId>
<artifactId>microcloud-api</artifactId>
</dependency>

4、 【microcloud-service】此时如果要通过 Feign 进行远程 Rest 调用,那么必须要考虑服务的认证问题。

· 此时可以删除原始的 RestConfig 进行的配置处理,然后添加feign的认证配置类

package cn.study.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("studyjava", "hello");
}
}

5、 【microcloud-service】建立一个 IDeptClientService 接口;

package cn.study.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 cn.study.commons.config.FeignClientConfig;
import cn.study.vo.Dept;
/**
* 通过注解@FeignClient添加接口对应的远程微服务名称value="MICROCLOUD-PROVIDER-DEPT"和
* 服务的认证configuration=FeignClientConfig.class
*
*/
@FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class)
public interface IDeptClientService {
@RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}")
public Dept get(@PathVariable("id") long id) ;
@RequestMapping(method=RequestMethod.GET,value="/dept/list")
public List<Dept> list() ;
@RequestMapping(method=RequestMethod.POST,value="/dept/add")
public boolean add(Dept dept) ;
}

6、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 开发包:

<dependency>
<groupId>cn.study</groupId>
<artifactId>microcloud-service</artifactId>
</dependency>

7、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序类;

package cn.study.microcloud.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;

@RestController
public class ConsumerDeptController {
@Resource
private IDeptClientService deptService ;
@RequestMapping(value = "/consumer/dept/get")
public Object getDept(long id) {
return this.deptService.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public Object listDept() {
return this.deptService.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object addDept(Dept dept) throws Exception {
return this.deptService.add(dept);
}
}

8、 【microcloud-consumer-feign】修改程序启动主类,追加操作处理。

package cn.study.microcloud;

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;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"cn.study.service"})//进行接口IDeptClientService的扫描生成使得可以注入到ConsumerDeptController里面
public class Consumer_80_StartSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(Consumer_80_StartSpringCloudApplication.class,
args);
}
}

9、 启动测试:http://client.com/consumer/dept/get?id=1

· 可以发现 Feign 在处理的时候自带有负载均衡的配置项

2.2、Feign 相关配置

1、 【microcloud-consumer-feign】Feign 之中最为核心的作用就是将 Rest 服务的信息转换为接口,但是在实际的使用之中也需要考虑到一些配置情况,例如:数据压缩,Rest 的核心本质在于:JSON 数据传输(XML、文本),于是就必须思考一种情况,如果用户发送的数据很大,这个时候可以考虑修改 application.yml 配置文件对传输数据进行压缩;

feign:
compression:
request:
mime-types:       # 可以被压缩的类型
- text/xml
- application/xml
- application/json
min-request-size: 2048 # 超过2048的字节进行压缩

2、 如果有需要则可以在项目之中开启 feign 的相关日志信息(默认不开启):

· 【microcloud-consumer-feign】修改 application.yml 配置文件,追加日志追踪:

logging:
level:
cn.study.service: DEBUG

· 【microcloud-service】修改 FeignClientConfig,开启日志的输出:

package cn.study.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.Logger;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
@Bean
public Logger.Level getFeignLoggerLevel() {
return feign.Logger.Level.FULL ;
}
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("studyjava", "hello");
}
}

再次运行程序现在可以观察到如下的流程:

· 当使用 Feign 要通过接口的方法访问 Rest 服务的时候会根据设置的服务类型发出请求,这个请求是发送给 Eureka(地址: “http://MICROCLOUD-PROVIDER-DEPT/dept/list”);

· 随后由于配置了授权处理,所以继续发送授权信息(“Authorization”);

· 在进行服务调用的时候 Feign 融合了 Ribbon 技术,所以也支持有负载均衡的处理;

**总结:**Feign = RestTempate + HttpHeader + Ribbon + Eureka 综合体 = 业务接口的自动实例化

3ff7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐