spring-cloud微服务部署-feign服务间调用

1 准备工作

需要安装并启动nacos,作为服务注册中心。地址:https://nacos.io/

2 项目结构

parent的pom.xml声明依赖:

xml 复制代码
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.2.12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2023.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

2.1 服务service-a

在pom.xml中引入依赖:

xml 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.smant.xhappy</groupId>
            <artifactId>service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
</dependencies>

在service-a中通过feign调用service-b的hello方法:

在controller中定义调用service-b中hello方法的接口:

在启动类上开启feign接口扫描:service-a需要调用service-b的接口,要在service-a的启动类上加@EnableFeignClients 注解,也就是服务调用方要加上@EnableFeignClients注解。

在application.yml中配置nacos地址:

bash 复制代码
server:
  port: 7515
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: service-a

2.2 服务service-b

和service-a的结构代码都差不多,不在赘述。

2.2 feign接口

在pom.xml引入依赖:

xml 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
</dependencies>

声明service-b的hello方法,给别的服务调用。

3 启动测试

启动service-a和service-b,然后请求地址:

http://localhost:7515/api/a/hello-a?msg=world

响应:A, hello world

http://localhost:7515/api/a/hello-b?msg=world

响应:B, hello world

这样一个简单的通过feign进行服务间调用的流程的就通了!!!

相关推荐
FQNmxDG4S13 小时前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
码点滴13 小时前
什么时候用 DeepSeek V4,而不是 GPT-5/Claude/Gemini?
人工智能·gpt·架构·大模型·deepseek
heimeiyingwang13 小时前
【架构实战】状态机架构:订单/工单状态流转设计
观察者模式·架构·wpf
虹科网络安全13 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
axng pmje14 小时前
Java语法进阶
java·开发语言·jvm
HackTorjan14 小时前
深度神经网络的反向传播与梯度优化原理
人工智能·spring boot·神经网络·机器学习·dnn
rKWP8gKv714 小时前
Java微服务性能监控:Prometheus与Grafana集成方案
java·微服务·prometheus
老前端的功夫14 小时前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python
qq_4352879214 小时前
第9章 夸父逐日与后羿射日:死循环与进程终止?十个太阳同时值班的并行冲突
java·开发语言·git·死循环·进程终止·并行冲突·夸父逐日
小江的记录本14 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka