OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,用法其实就是编写一个接口,在接口上添加注解即可。
可以简单理解它是借鉴Ribbon的基础之上,封装的一套服务接口+注解的方式的远程调用器。
它的宗旨是编写Java Http客户端接口的时候变得更加容易,其底层也整合了Ribbon,所以也可以支持负载均衡。
之前我们在使用Ribbon的时候,利用的是RestTemplate对Http请求进行封装处理,但是在实际开发过程中,对服务依赖的调用不可能就一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以OpenFeign在此基础上做了进一步的封装,由它帮我们定义和实现依赖服务接口的定义,我们只需要创建一个接口并使用使用的方式配置它,即可完成对微服务提供方的接口绑定,简化Ribbon的操作。
使用OpenFeign其实就是在消费端去远程调用,就必须要是FeignClient注解,来标注要调用的服务提供者名称,然后再通过一个接口来定义要调用的方法
①Ribbon
②Feign
③OpenFeign
模块结构如下:
创建好maven模块,继承父模块,并导入相关jar包:包括nacos、mybatis、mysql等
①pom.xml:
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.zi</groupId>
<artifactId>SpringCloudAlibabaAll</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zi</groupId>
<artifactId>cloudalibaba-provider-9003</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloudalibaba-provider-9003</name>
<description>cloudalibaba-provider-9003</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
②父模块的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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zi</groupId>
<artifactId>SpringCloudAlibabaAll</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloudAlibabaAll</name>
<packaging>pom</packaging>
<description>SpringCloudAlibabaAll</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba-version>2.2.5.RELEASE</spring-cloud-alibaba-version>
<openfeign-version>2.2.7.RELEASE</openfeign-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${openfeign-version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>cloudAlibaba-Nacos-9001</module>
<module>cloudalibaba-nacos-9002</module>
<module>cloudalibaba-consumer-8083</module>
<module>cloudalibaba-config-3377</module>
<module>cloudalibaba-openFeign-consumer-8888</module>
<module>cloudalibaba-provider-9003</module>
</modules>
</project>
①User.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer uid;
private String uname;
private Integer upwd;
}
②UserMapper.java
@Mapper
public interface UserMapper {
public User getUserById(Integer uid);
}
③UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zi.mapper.UserMapper">
<select id="getUserById" resultType="user">
select * from user where uid = #{uid}
select>
mapper>
④UserService.java
public interface UserService {
public User getUserById(Integer uid);
}
⑤UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer uid) {
return userMapper.getUserById(uid);
}
}
⑥UserController.java
@Controller
@RequestMapping("/zi")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/{id}")
@ResponseBody
public User getUser(@PathVariable("id") Integer uid){
User user = userService.getUserById(uid);
return user;
}
@GetMapping
@ResponseBody
public String test(){
return "this is test";
}
}
⑦启动类【加上@EnableDiscoveryClient注册到nacos上】
@SpringBootApplication
@EnableDiscoveryClient
public class CloudalibabaProvider9003Application {
public static void main(String[] args) {
SpringApplication.run(CloudalibabaProvider9003Application.class, args);
}
}
server:
# 服务端口
port: 9003
spring:
application:
#服务名称
name: feign-provider
cloud:
discovery:
#nacos地址
server-addr: 127.0.0.1:8848
#配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mp?serverTimezone=UTC
username: root
password: 200151
type: com.alibaba.druid.pool.DruidDataSource
management:
endpoint:
web:
exposure:
include: '*'
#配置mybatis
mybatis:
mapper-locations: classpath:*.xml
type-aliases-package: com.zi.pojo
# 日志的方式打印sql
logging:
level:
com.seamax.bdsearch.dao: DEBUG
访问之前需要启动本地nacos【进入到nacosbin目录下执行 startup.cmd -m standalone】
结果如下:
该模块也继承于父工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.zigroupId>
<artifactId>SpringCloudAlibabaAllartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.zigroupId>
<artifactId>cloudalibaba-openFeign-consumer-8888artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>cloudalibaba-openFeign-consumer-8888name>
<description>cloudalibaba-openFeign-consumer-8888description>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
整体模块结构:
User.java
【后期也可以将所有pojo类放在同一个模块下】
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer uid;
private String uname;
private Integer upwd;
}
OpenFeignService.java
需要添加@FeignClient注解并配置远程调用的服务名称
/**
* 此接口就是配合使用OpenFeign的接口,
* 在此接口中添加@FeignClient接口同时标注
* 要调用的服务端名称,同时使用与服务提供者
* 方法签名一致的抽象方法来表示远程调用的
* 具体内容
*/
@Service
@FeignClient("feign-provider")//远程调用的服务名称
public interface OpenFeignService {
/**
* 该方法表示远程调用zi/{id}接口
* @param uid
* @return
*/
@GetMapping("zi/{id}")
public User getUserById(@PathVariable("id") Integer uid);
}
OpenFeignController.java
@RestController
public class OpenFeignController {
@Autowired
private OpenFeignService openFeignService;
@GetMapping("getInfo/{id}")
public User getInfo(@PathVariable("id") Integer uid){
return openFeignService.getUserById(uid);
}
}
启动类:
需要加上@EnableFeignClients注解及@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient//让nacos找到
@EnableFeignClients
public class CloudalibabaOpenFeignConsumer8888Application {
public static void main(String[] args) {
SpringApplication.run(CloudalibabaOpenFeignConsumer8888Application.class, args);
}
}
配置文件application.yml
server:
port: 8888
spring:
application:
name: nacos-consumer-openFeign
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'