当前位置:网站首页>RestTemplate 服务调用
RestTemplate 服务调用
2022-04-23 21:58:00 【Leon_Jinhai_Sun】
# 1.创建两个服务并注册到consul注册中心中
- users 代表用户服务 端口为 9999
- products 代表商品服务 端口为 9998
`注意:这里服务仅仅用来测试,没有实际业务意义
# 2.在商品服务中提供服务方法
@RestController
@Slf4j
public class ProductController {
@Value("${server.port}")
private int port;
@GetMapping("/product/findAll")
public Map<String,Object> findAll(){
log.info("商品服务查询所有调用成功,当前服务端口:[{}]",port);
Map<String, Object> map = new HashMap<String,Object>();
map.put("msg","服务调用成功,服务提供端口为: "+port);
map.put("status",true);
return map;
}
}
# 3.在用户服务中使用restTemplate进行调用
@RestController
@Slf4j
public class UserController {
@GetMapping("/user/findAll")
public String findAll(){
log.info("调用用户服务...");
//1.使用restTemplate调用商品服务
RestTemplate restTemplate = new RestTemplate();
String forObject = restTemplate.getForObject("http://localhost:9998/product/findAll",
String.class);
return forObject;
}
}
# 4.启动服务
# 5.测试服务调用
- 浏览器访问用户服务 http://localhost:9999/user/findAll
# 6.总结
- rest Template是直接基于服务地址调用没有在服务注册中心获取服务,也没有办法完成服务的负载均衡如果需要实现服务的负载均衡需要自己书写服务负载均衡策略。
版权声明
本文为[Leon_Jinhai_Sun]所创,转载请带上原文链接,感谢
https://blog.csdn.net/leon_jinhai_sun/article/details/124210700
边栏推荐
猜你喜欢
hystrix dashboard的使用
NVM introduction, NVM download, installation and use (node version management)
Cvpr2022 | efficient pre training based on knowledge distillation
分布式系统服务熔断
Pycharm Chinese plug-in
Online Excel to CSV tool
Online timing flow chart making tool
Oracle updates the data of different table structures and fields to another table, and then inserts it into the new table
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
C language programming based on loop structure (PTA)
随机推荐
OpenFeign组件的简介和使用
从严从重从快 上海全面加强疫情期间食品安全监管
Introduce structured concurrency and release swift 5.5!
MySQL back to table
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Devops and cloud computing
Yolov5 NMS source code understanding
[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]
Pycharm download and installation
1. Finishing huazi Mianjing -- 1
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
Detectron2 usage model
Sklearn function: Train_ Test_ Split (split training set and test set)
Detailed explanation of basic assembly instructions of x86 architecture
Swift import third-party library reports an error no such module““
Database Experiment four View experiment
Deep understanding of modern mobile GPU (continuously updating)
C# ftpHelper
C reads excel specific data into specific columns of DataGridView