SpringCloud Feign远程调用公共类抽取 【SpringCloud系列4】
SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发
Nacos 官网 https://nacos.io/zh-cn/
使用 nacos
Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign
本文章是系列文章中的一篇
- 1、SpringCloud 项目基础工程搭建 【SpringCloud系列1】
- 2、SpringCloud 集成Nacos注册中心 【SpringCloud系列2】
- 3、SpringCloud Feign远程调用 【SpringCloud系列3】
1 创建 feign-api
首先创建一个module,命名为feign-api
填写基本信息如下
然后删除 自动生成的多余的部分,留下空的项目
然后在 order-service 与 user-service 中添加 feign-api 的依赖如下
<dependency>
<groupId>com.biglead</groupId>
<artifactId>feign-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后在 feign-api 中添加 openfeign 的依赖如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--httpClient连接池的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
然后把 FeignUserClient 相关内容移动到 feign-api 中
然后在 order-service 的启动类中 指定Feign应该扫描的包:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients(basePackages = "com.biglead.feign.clients")
@SpringBootApplication
@MapperScan(basePackages = "com.biglead.orderservice.mapper")
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
然后启动服务测试
本文章是系列文章,代码过多,可以查看源码如下:
https://gitee.com/android.long/spring-cloud-biglead/tree/master/biglead-api-04-feign
有兴趣可以关注一下 公众号 biglead
还没有评论,来说两句吧...