当前位置:网站首页>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;
}

边栏推荐
- 【高并发】SimpleDateFormat类到底为啥不是线程安全的?(附六种解决方案,建议收藏)
- pycharm连接远程服务器ssh -u 报错:No such file or directory
- 【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
- 已解决The JSP specification requires that an attribute name is preceded by whitespace
- [untitled]
- Hardware connection server TCP communication protocol gateway
- 银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
- Go garbage collector Guide
- 【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
- [USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)
猜你喜欢

Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database

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

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)

【AI4Code最终章】AlphaCode:《Competition-Level Code Generation with AlphaCode》(DeepMind)

【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》

What is the global event bus?

Brpc source code analysis (VI) -- detailed explanation of basic socket

Learning to pre train graph neural networks

Transformer variants (routing transformer, linformer, big bird)

Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)
随机推荐
brpc源码解析(八)—— 基础类EventDispatcher详解
PHP uploads the FTP path file to the curl Base64 image on the Internet server
[MySQL 17] installation exception: could not open file '/var/log/mysql/mysqld log‘ for error logging: Permission denied
氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心
Zero shot image retrieval (zero sample cross modal retrieval)
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
winddows 计划任务执行bat 执行PHP文件 失败的解决办法
PHP 上传ftp路径文件到外网服务器上 curl base64图片
R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为jpeg格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
[high concurrency] a lock faster than read-write lock in high concurrency scenarios. I'm completely convinced after reading it!! (recommended Collection)
已解决The JSP specification requires that an attribute name is preceded by whitespace
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
Brpc source code analysis (I) -- the main process of RPC service addition and server startup
Return and finally? Everyone, please look over here,
Learning to pre train graph neural networks
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
【GCN多模态RS】《Pre-training Representations of Multi-modal Multi-query E-commerce Search》 KDD 2022
【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
Intelligent information retrieval (overview of intelligent information retrieval)