学无止境
欢迎关注我的公众号
接收定期更新文章
【微服务实战】服务间通信:SpringBoot + OpenFeign 调用实战(含超时与重试配置)
2026-01-08 13:11:47
664

 


【微服务实战】服务间通信:Spring Boot + OpenFeign 调用实战(含超时与重试配置)

上个章节已经完成多模块注册[链接],本章节继续探索。
spring Boot 版本 3.3.7
nacos 版本 3.1.1
Spring Cloud 版本 2023.0.3
Spring Cloud Alibaba 版本 2023.0.3.4

  • common 模块:存放公共工具类、实体、常量等

  • home 模块:Web 应用,端口 8081

  • user 模块:Web 应用,端口 80828083

  • 实现目标:通过home服务请求user服务多个实例并实现自动负载均衡


1、home 模块

home/pom.xml

      
1
2
3
4
5
6
7
8
9
10

<!-- OpenFeign -->
<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>

home/src/main/java/com/tsphp/home/HomeApplication.java

      
1
2
3
4
5
6
7
8
9
10
11
12
13

package com.tsphp.home;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
 
@SpringBootApplication
@EnableFeignClients # 激活openFeigen
public class HomeApplication {
    public static void main(String[] args) {
        SpringApplication.run(HomeApplication.class, args);
    }
}

创建 UserClient客户端

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

package com.tsphp.home.client;
 
import com.tsphp.common.units.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
 
@Component
@FeignClient(name = "user-service") // 服务名(注册到 Nacos 的名称)
public interface UserClient {
 
    @GetMapping("/user/info/{id}")
    Result getUserInfoById(@PathVariable("id") Long id);
}

controller测试代码

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

package com.tsphp.home.api;
 
import com.tsphp.common.units.Result;
import com.tsphp.common.units.ResultUnit;
import com.tsphp.home.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/home")
public class Index {
 
    @Autowired
    private UserClient userClient;
 
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id) {
        return userClient.getUserInfoById(id);
    }
 
}

2、user 模块

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

package com.tsphp.user.api;
 
import com.tsphp.common.units.Result;
import com.tsphp.common.units.ResultUnit;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/user")
public class user {
 
    private final Environment env;
 
    public user(Environment env) {
        this.env = env;
    }
 
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id) {
        int port = env.getProperty("server.port", Integer.class, 0);
        return ResultUnit.wrapSuccess("userInfo:"+port+":" + id);
    }
}

3、服务启动

1)启动Nacos服务:

      
1
2

.\bin\startup.cmd -m standalone
#启动结果如下图:

2)启动Home服务
3)启动User服务8082
4)启动User服务8083

  • 上图标识 Nacos 已经注册了一个home服务+两个user服务

  • 测试结果:重复请求接口自动请求不同的user服务。

  • • 第一次请求:curl http://localhost:8081/home/info/20

  • • 返回:{"errcode":0,"errmsg":"","data":"userInfo:8083:20"}

  • • 第二次请求:curl http://localhost:8081/home/info/20

  • • 返回:{"errcode":0,"errmsg":"","data":"userInfo:8082:20"}

gitee源码: https://gitee.com/wangshi90/springboot


【小结】

今天成功打通了微服务之间的“任督二脉”——用 OpenFeign 一声令下,服务 A 就乖乖去喊服务 B:“兄弟,借个数据!”
更妙的是,我还给它配上了简易版“负载均衡”,不再是单点苦力,而是轮着来、不累垮,仿佛给微服务装上了旋转寿司传送带——谁空闲谁上菜!
虽然现在只是“Hello World”级别的调用,但至少它们开始说话了,没打架也没超时(暂时)。
微服务江湖,今日又添一员“嘴替”侠!😎

 


微服务通信
OpenFeign
负载均衡
Spring Boot
欢迎关注我的公众号,获取更多文章