当前位置:网站首页>RestTemplate与Ribbon简单使用
RestTemplate与Ribbon简单使用
2022-07-25 11:21:00 【dengjili】
RestTemplate
RestTemplate是访问rest 接口的对象,提供相应的访问方法
配置创建RestTemplate
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
创建三个测试访问的uri
@RestController
public class DemoController {
@GetMapping("/demo/data")
public Foo getData(@RequestParam("name") String name) {
Foo foo = new Foo();
foo.setId(1);
foo.setName(name);
return foo;
}
@GetMapping("/demo/data/{name}")
public String getData2(@PathVariable("name") String name) {
return name + "plus";
}
@PostMapping("/data/save")
public long addData(@RequestBody Foo foo) {
System.out.println(foo.getName());
return 10001;
}
}
RestTemplate调用
@RestController
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/call/data")
public Foo getData(@RequestParam("name") String name) {
Foo result = restTemplate.getForObject("http://localhost:8080/demo/data?name=" + name, Foo.class);
return result;
}
@GetMapping("/call/data/{name}")
public String getData2(@PathVariable("name") String name) {
String result = restTemplate.getForObject("http://localhost:8080/demo/data/{name}", String.class, name);
return result + "call back";
}
@GetMapping("/call/data3")
public Foo getData3(@RequestParam("name") String name) {
ResponseEntity<Foo> entity = restTemplate.getForEntity("http://localhost:8080/demo/data?name=" + name, Foo.class);
System.out.println(entity.getStatusCodeValue());
Foo result = entity.getBody();
return result;
}
@GetMapping("/call/data/save")
public long addDataTest() {
Foo foo = new Foo();
foo.setId(1);
foo.setName("test");
long result = restTemplate.postForObject("http://localhost:8080/data/save", foo, Long.class);
return result;
}
}
Ribbon

Ribbon是正向代理,代理客户端向服务器发起请求,这里代理后,进行负载均衡功能
引入依赖,Ribbon需要依赖于注册中心eureka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
添加配置项,修改配置文件application.properties
eureka.client.serviceUrl.defaultZone=http://192.168.0.102:8761/eureka
修改RestTemplate对象的创建,添加注解@LoadBalanced
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
调用rest接口,将ip和端口换成应用注册名称,如下eureka-provider-app
@GetMapping("/call/loadbalanced")
public String loadBalanced() {
String result = restTemplate.getForObject("http://eureka-provider-app/hello", String.class);
return result;
}

边栏推荐
- 'C:\xampp\php\ext\php_zip.dll' - %1 不是有效的 Win32 应用程序 解决
- dirReader.readEntries 兼容性问题 。异常错误DOMException
- [high concurrency] Why is the simpledateformat class thread safe? (six solutions are attached, which are recommended for collection)
- 对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)
- 硬件连接服务器 tcp通讯协议 gateway
- 【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
- brpc源码解析(二)—— brpc收到请求的处理过程
- Risks in software testing phase
- 【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
- pycharm连接远程服务器ssh -u 报错:No such file or directory
猜你喜欢

【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22

【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)

brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制

'C:\xampp\php\ext\php_zip.dll' - %1 不是有效的 Win32 应用程序 解决

【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020

PHP curl post x-www-form-urlencoded

Application and innovation of low code technology in logistics management

异构图神经网络用于推荐系统问题(ACKRec,HFGN)

Learning to Pre-train Graph Neural Networks(图预训练与微调差异)

30 sets of Chinese style ppt/ creative ppt templates
随机推荐
异构图神经网络用于推荐系统问题(ACKRec,HFGN)
PHP curl post length required error setting header header
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造
Zero-Shot Image Retrieval(零样本跨模态检索)
LeetCode第303场周赛(20220724)
session和cookie有什么区别??小白来告诉你
【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
return 和 finally的执行顺序 ?各位大佬请看过来,
dirReader.readEntries 兼容性问题 。异常错误DOMException
[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置add参数在小提琴内部添加抖动数据点以及均值标准差竖线(jitter and mean_sd)
【AI4Code最终章】AlphaCode:《Competition-Level Code Generation with AlphaCode》(DeepMind)
【高并发】高并发场景下一种比读写锁更快的锁,看完我彻底折服了!!(建议收藏)
基于TCP/IP在同一局域网下的数据传输
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
brpc源码解析(八)—— 基础类EventDispatcher详解
Objects in JS