openfeign作为springcloud框架子项目之一,是一种声明式、模板化的HTTP客户端,openfeign为为服务架构中服务间的调用提供了解决方案。
openfeign是如何工作的呢?
Feign通过将注释处理为模板化请求来工作。在输出之前,参数以简单的方式应用于这些模板。尽管Feign仅限于支持基于文本的API,但它极大地简化了系统方面,例如重播请求。
官方解释:
How does Feign work?
Feign works by processing annotations into a templatized request. Arguments are applied to these templates in a straightforward fashion before output. Although Feign is limited to supporting text-based APIs, it dramatically simplifies system aspects such as replaying requests. Furthermore, Feign makes it easy to unit test your conversions knowing this.
话不多说,上代码。
1.搭建openfeign-consumer模块,pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.feng</groupId>
<artifactId>cloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>openfeign-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>openfeign-consumer</name>
<description>cloud study for two month</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml配置文件修改,增加feign配置
spring:
application:
name: openfeign-consumer
server:
port: 8603
servlet:
context-path: /openfeign
eureka:
instance:
instanceId: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8801/register/eureka/
feign:
httpclient:
enabled: true #使用httpclient
okhttp:
enabled: false #不启用okhttp
compression:
request:
enabled: true #启用请求压缩
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true #启用响应压缩
3.新建MovieClient.java,使用@FeignClient注解代表一个openfeign客户端,
package com.feng.cloud.study.feignApi;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author: fengyantao
* @date: 2019/11/11 下午3:09
* @version: V1.0
* @review: fengyantao/2019/11/11 下午3:09
*/
@FeignClient(value = "eureka-producer")
public interface MovieClient {
@GetMapping("/producer/movie/getAllMovies")
String getAllMovies();
}
4.修改controller测试类,使用MovieClien调用生产者eureka-producer,可以用openfeign开发一个openfeign-producer生产者。
package com.feng.cloud.study.controller;
import com.feng.cloud.study.feignApi.MovieClient;
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;
/**
* @author: fengyantao
* @date: 2019/11/6 下午4:26
* @version: V1.0
* @review: fengyantao/2019/11/6 下午4:26
*/
@RestController
@RequestMapping("/user")
public class OpenFeignUserController {
@Autowired
private MovieClient movieClient;
@GetMapping("/findMovies")
public String findMovies() {
return movieClient.getAllMovies();
}
}
5.启动类上添加@EnableFeignClients(basePackages = { "com.feng.cloud.study.feignApi" }),并配置扫描openfeign客户端。
package com.feng.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.feng.cloud.study.feignApi" })
public class OpenFeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(OpenFeignConsumerApplication.class, args);
}
}
6.先后启动eureka注册中心模块,eureka生产者模块,openfeign消费者模块,访问http://localhost:8603/openfeign/user/findMovies ,可以看到生产者提供的信息
到此为止,openfeign+eureka模块已经搭建完成,代码git地址:https://github.com/fengyantao/cloud-study/tree/master
大家如果有疑问,欢迎下方留言沟通!
扩展资料:
两个月学习springcloud(1):搭建eureka注册中心
两个月学习springcloud(2):搭建eureka服务提供者