SpringBoot + openFeign实现远程接口调用的过程
作者:12程序猿
现在的微服务项目不少都使用的是springboot+spring cloud构建的项目,微服务之间的调用都离不开feign来进行远程调用,这篇文章主要介绍了SpringBoot + openFeign实现远程接口调用,需要的朋友可以参考下
SpringBoot服务之间通过openFeign实现远程接口调用
现在的微服务项目不少都使用的是springboot+spring cloud构建的项目,微服务之间的调用都离不开feign来进行远程调用。那么我们一个服务需要调用第三方的服务的时候,我们常常可能使用httpclient或者restTemplate等客户端api来实现远程调用,其实我们可以在微服务没有适用spring cloud框架的情况下,想调用第三方服务,也可以通过feign组件实现http的远程调用。
实现过程
1.首先创建服务端项目,提供数据接口
1.1添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>1.2 配置application.yml
application.yml:
server:
port: 8080
spring:
application:
name: serviceDemo1.3 实体类
User:
package com.example.servicedemo.entity;
import lombok.Data;
/**
* 用户信息
* @author qzz
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}1.4 添加控制器方法
UserController:
package com.example.servicedemo.controller;
import com.example.servicedemo.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author qzz
*/
@RestController
public class UserController {
@RequestMapping("/api/user/getUserList")
public List<User> getUserList(){
//模拟数据库请求数据
List<User> list = new ArrayList<>();
User user = new User();
user.setId(1);
user.setName("Jack");
user.setAge(31);
list.add(user);
return list;
}
}1.5 启动类
package com.example.servicedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author qzz
*/
@SpringBootApplication
public class ServiceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceDemoApplication.class, args);
}
}浏览器访问:http://localhost:8080/api/user/getUserList

2.创建客户端项目,调用服务端接口
2.1添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>2.2 配置application.yml
application.yml:
server:
port: 8081
spring:
application:
name: clientName2.3 实体类
User:
package com.example.clientdemo.entity;
import lombok.Data;
/**
* @author qzz
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}2.4 创建OpenFeign接口
注意:@FeignClient的name和value属性必填其一,另外url必填。
package com.example.clientdemo.feign;
import com.example.clientdemo.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* openFeign接口
* URL:就是远程端需要调用接口的服务URL路径,name:就是服务名,value和name一样
* @author qzz
*/
@FeignClient(name = "serviceDemo",url = "http://localhost:8080")
public interface ServiceDemoFeign {
/**
* 获取用户列表
* @return
*/
@RequestMapping("/api/user/getUserList")
List<User> getUserList();
}2.5 添加控制器方法
UserController:
/**
* @author qzz
*/
@RestController
public class UserController {
/**
* 注入OpenFeign接口
*/
@Autowired
private ServiceDemoFeign serviceDemoFeign;
@RequestMapping("/api/client/user/getUserList")
public List<User> getUserList(){
return serviceDemoFeign.getUserList();
}
}2.6 启动类
启动类需要添加@EnableFeignClients注解。加入EnableFeignClients开启Feign注解,使Feign的bean可以被注入
package com.example.clientdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author qzz
*/
@EnableFeignClients
@SpringBootApplication
public class ClientDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ClientDemoApplication.class, args);
}
}
2.7 测试效果
浏览器访问:http://localhost:8081/api/client/user/getUserList

返回结果成功,说明服务调用成功。
完整代码
点击此处进行下载
到此这篇关于SpringBoot + openFeign实现远程接口调用的文章就介绍到这了,更多相关SpringBoot openFeign远程接口调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
