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: serviceDemo
1.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: clientName
2.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远程接口调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java的Comparable,Comparator和Cloneable三大接口详解
这篇文章主要为大家详细介绍了Java的Comparable,Comparator和Cloneable的接口,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助2022-03-03SpringMVC MethodArgumentResolver的作用与实现
这篇文章主要介绍了SpringMVC MethodArgumentResolver的作用与实现,MethodArgumentResolver采用一种策略模式,在Handler的方法被调用前,Spring MVC会自动将HTTP请求中的参数转换成方法参数2023-04-04Java中BigDecimal,DateFormatter 和迭代器的"陷阱"
这篇文章主要介绍了Java中BigDecimal,DateFormatter 和迭代器的"陷阱",文章围绕主题展开详细的内容介绍,感兴趣的小伙伴可以参考一下2022-06-06
最新评论