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

边栏推荐
- Learning to pre train graph neural networks
- PHP one server sends pictures to another. Curl post file_ get_ Contents save pictures
- Mirror Grid
- 【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
- 'C:\xampp\php\ext\php_zip.dll' - %1 不是有效的 Win32 应用程序 解决
- GPT plus money (OpenAI CLIP,DALL-E)
- 容错机制记录
- Introduction to redis
- dirReader.readEntries 兼容性问题 。异常错误DOMException
- Brpc source code analysis (I) -- the main process of RPC service addition and server startup
猜你喜欢

Unexpected rollback exception analysis and transaction propagation strategy for nested transactions

嵌套事务 UnexpectedRollbackException 分析与事务传播策略

从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」

【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21

知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)

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

【GCN多模态RS】《Pre-training Representations of Multi-modal Multi-query E-commerce Search》 KDD 2022

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

【微服务~Sentinel】Sentinel降级、限流、熔断

【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
随机推荐
Zero shot image retrieval (zero sample cross modal retrieval)
I advise those students who have just joined the work: if you want to enter the big factory, you must master these concurrent programming knowledge! Complete learning route!! (recommended Collection)
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
After having a meal with trump, I wrote this article
The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
The applet image cannot display Base64 pictures. The solution is valid
PHP curl post x-www-form-urlencoded
R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
[high concurrency] Why is the simpledateformat class thread safe? (six solutions are attached, which are recommended for collection)
[cloud co creation] what is the role of AI in mathematics? What will be the disruptive impact on the mathematical world in the future?
Zuul网关使用
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
GPT plus money (OpenAI CLIP,DALL-E)
Transformer变体(Routing Transformer,Linformer,Big Bird)
剑指 Offer 22. 链表中倒数第k个节点
Application and innovation of low code technology in logistics management
Atomic atomic class
Introduction to redis
Those young people who left Netease
和特朗普吃了顿饭后写下了这篇文章