当前位置:网站首页>Resttemplate and ribbon are easy to use
Resttemplate and ribbon are easy to use
2022-07-25 12:14:00 【dengjili】
RestTemplate
RestTemplate Is access rest Object of the interface , Provide corresponding access methods
Configuration creation RestTemplate
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Create three test accesses 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 call
@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 It's a forward agent , The proxy client sends a request to the server , After acting here , Load balancing function
Introduce dependencies ,Ribbon Need to rely on the registry 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>
Add configuration item , Modify the configuration file application.properties
eureka.client.serviceUrl.defaultZone=http://192.168.0.102:8761/eureka
modify RestTemplate Object creation , adding annotations @LoadBalanced
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
call rest Interface , take ip And port are replaced with application registration name , as follows eureka-provider-app
@GetMapping("/call/loadbalanced")
public String loadBalanced() {
String result = restTemplate.getForObject("http://eureka-provider-app/hello", String.class);
return result;
}

边栏推荐
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
- PHP 上传ftp路径文件到外网服务器上 curl base64图片
- 氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心
- Client open download, welcome to try
- Go 垃圾回收器指南
- Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
- Hardware connection server TCP communication protocol gateway
- Atomic 原子类
- Unexpected rollback exception analysis and transaction propagation strategy for nested transactions
- PHP curl post length required error setting header header
猜你喜欢

【图攻防】《Backdoor Attacks to Graph Neural Networks 》(SACMAT‘21)

Word中的空白页,怎么也删不掉?如何操作?

Multi-Label Image Classification(多标签图像分类)

【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021

Brpc source code analysis (II) -- the processing process of brpc receiving requests

【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021

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

Transformer变体(Sparse Transformer,Longformer,Switch Transformer)

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

那些离开网易的年轻人
随机推荐
NLP knowledge - pytorch, back propagation, some small pieces of notes for predictive tasks
The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
RestTemplate与Ribbon简单使用
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
Mirror Grid
selenium使用———安装、测试
keepalived实现mysql的高可用
[GCN multimodal RS] pre training representations of multi modal multi query e-commerce search KDD 2022
Transformer variants (routing transformer, linformer, big bird)
Zero-Shot Image Retrieval(零样本跨模态检索)
Location analysis of recording an online deadlock
mysql的表分区
【对比学习】Understanding the Behaviour of Contrastive Loss (CVPR‘21)
LeetCode第303场周赛(20220724)
Introduction to redis
【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
The applet image cannot display Base64 pictures. The solution is valid
【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
OSPF comprehensive experiment
Scott+scott law firm plans to file a class action against Yuga labs, or will confirm whether NFT is a securities product