SpringCloud openfeign相互调用实现方法介绍
目录
- 一、准备工作
- 二、新建一个springcloud项目
一、准备工作
1、安装nacos,地址:Releases · alibaba/nacos · github
2、springboot与springcloud之间的版本对应关系
我使用的版本:
springboot:2.4.2
springcloud:2020.0.1
springcloud alibaba:2021.1
二、新建一个springcloud项目
1、首先打开idea,新建一个maven项目,并将src文件夹删除
2、在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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSwww.devze.comHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <Java.version>1.8</java.version> <!--spring cloud 版本--> <spring-cloud.version>2020.0.1</spring-cloud.version> <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version> </properties> <dependencies> <dependency> <gropythonupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> 开发者_JS开发 <version>8.0.29</version> </dependency> <!--引入配置中心阿里巴巴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--引入注册中心阿里巴巴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spri编程ng-cloud-alibaba-dependencies</a编程客栈rtifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>myBATis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> </dependencies> </dependencyManagement> </project>
3、在根目录下分别新建maven子项目provider和consumer
4、修改provider和consumer的pom.xml文件
(1)给consumer添加依赖,openfeign和loadbalancer。注意,服务消费者一定要引入loadbalancer依赖,否则会报错。
<!--OpenFeign为HTTP形式的Rest API提供了非常简洁高效的RPC调用方式--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--loadbalancer是Spring Cloud官方自己提供的客户端负载均衡器,抽象和实现,用来替代Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
(2)provider暂时不用添加依赖
5、为provider和consumer添加配置文件application.yml
# provider
server: port: 8001spring: application: name: provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # nacos服务的端口号为8848# consumerserver: port: 8002spring: application: name: consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848
6、给provider和consumer添加启动类
(1)ProviderApplication.java
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
(2)ConsumerApplication.java
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
consumer是消费者,需要调用provider服务,故需要加上@EnableFeignClients注解
5、编写provider生产者的服务,一个简单功能,返回“hello world”
@RestController @RequestMapping("/provider") public class ProviderController { @GetMapping("/test") public String test() { return "hello world"; } }
6、在consumer中新建client接口,用于调用provider中的方法
@FeignClient("provider") // 括号中为目标服务名 public interface ProviderClient { @GetMapping("provider/test") // 目标方法的url String hello(); }
7、在consumer的controller中调用client接口
@RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired ProviderClient providerClient; @GetMapping("/feignTest") public String test() { return providerClient.hello(); } }
8、启动项目
(1)启动nacos,将nacos下载安装好android以后,进入nacos中的bin文件夹。Windows系统用.cmd文件,linux和MACOS系统用.sh文件
以macOS系统为例,单机启动的命令为
./startup.sh -m standalone
启动成功后在浏览器上登录nacos进行管理,本地链接:http://localhost:8848/nacos/#/login
初始用户名和密码都为nacos
登录进去,此刻并无服务
(2)启动provider和consumer
刷新页面,在nacos上已经能够在服务列表中找到provider和consumer
9、测试效果
(1)测试provider,访问http://localhost:8001/provider/test,可以获得预期返回值“hello world”
(2)测试consumer,访问http://localhost:8002/consumer/feignTest,该接口使用openfeign调用了/provider/test接口,同样获得了“hello world”
到此这篇关于SpringCloud openfeign相互调用实现方法介绍的文章就介绍到这了,更多相关SpringCloud openfeign内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论