当前位置:网站首页>Ribbon 服务调用
Ribbon 服务调用
2022-04-23 21:58:00 【Leon_Jinhai_Sun】
# 1.项目中引入依赖
- 说明:
1.如果使用的是eureka client 和 consul client,无须引入依赖,因为在eureka,consul中默认集成了ribbon组件
2.如果使用的client中没有ribbon依赖需要显式引入如下依赖
<!--引入ribbon依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
# 2.查看consul client中依赖的ribbon
# 3.使用restTemplate + ribbon进行服务调用
- 使用discovery client 进行客户端调用
- 使用loadBalanceClient 进行客户端调用
- 使用@loadBalanced 进行客户端调用
# 3.1 使用discovery Client形式调用
@Autowired
private DiscoveryClient discoveryClient;
//获取服务列表
List<ServiceInstance> products = discoveryClient.getInstances("服务ID");
for (ServiceInstance product : products) {
log.info("服务主机:[{}]",product.getHost());
log.info("服务端口:[{}]",product.getPort());
log.info("服务地址:[{}]",product.getUri());
log.info("====================================");
}
# 3.2 使用loadBalance Client形式调用
@Autowired
private LoadBalancerClient loadBalancerClient;
//根据负载均衡策略选取某一个服务调用
ServiceInstance product = loadBalancerClient.choose("服务ID");
log.info("服务主机:[{}]",product.getHost());
log.info("服务端口:[{}]",product.getPort());
log.info("服务地址:[{}]",product.getUri());
# 3.3 使用@loadBalanced
//1.整合restTemplate + ribbon
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//2.调用服务位置注入RestTemplate
@Autowired
private RestTemplate restTemplate;
//3.调用
String forObject = restTemplate.getForObject("http://服务ID/hello/hello?name=" + name, String.class);
版权声明
本文为[Leon_Jinhai_Sun]所创,转载请带上原文链接,感谢
https://blog.csdn.net/leon_jinhai_sun/article/details/124224900
边栏推荐
- Pycharm download and installation
- Detectron2 using custom datasets
- IIS cannot load * woff,*. woff2,*. Solution of SVG file
- Detailed explanation of basic assembly instructions of x86 architecture
- openFeign 服务调用
- The DNN of OpenCV is used to read onnx as the prediction end to realize classification
- Is rust more suitable for less experienced programmers?
- Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
- 1.整理华子面经--1
- Arm architecture assembly instructions, registers and some problems
猜你喜欢
Hystrix组件
A method of asynchronous response of application service through load balancing
服务雪崩、服务熔断、服务降级
Database Experiment 8 trigger experiment
Plato Farm元宇宙IEO上线四大,链上交易颇高
This paper solves the cross domain problem of browser
Code cloud download history version process
Display, move, rotate
手撕《Google SRE Book》
C winfrom DataGridView click on the column header can not automatically sort the problem
随机推荐
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
[leetcode sword finger offer 28. Symmetric binary tree (simple)]
21. Basic usage of MariaDB
Resolve the "chromedriver executable needs to be in path" error
C list data paging
South Korea may ban apple and Google from offering commission to developers, the first in the world
Database Experiment 5 Security Language Experiment
Database Experiment four View experiment
ROS learning notes - tutorial on the use of ROS
服务雪崩效应
清华大学 | WebFace260M:百万级深度人脸识别的基准(TPAMI2022)
OpenFeign的参数传递之数组和集合类型
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
ERP function_ Financial management_ The difference between red and blue words in invoices
MVC three-tier architecture (illustration)
OpenFeign超时设置
1.整理华子面经--1
Presto on spark supports 3.1.3 records
服务降级的实现
Preliminary analysis of Airbase