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

边栏推荐
- GPT plus money (OpenAI CLIP,DALL-E)
- Oil monkey script link
- 【高并发】高并发场景下一种比读写锁更快的锁,看完我彻底折服了!!(建议收藏)
- Zero-Shot Image Retrieval(零样本跨模态检索)
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、使用step函数构建前向逐步回归模型筛选预测变量的最佳子集、scope参数指定候选预测变量
- [comparative learning] understanding the behavior of contractual loss (CVPR '21)
- [high concurrency] a lock faster than read-write lock in high concurrency scenarios. I'm completely convinced after reading it!! (recommended Collection)
- Return and finally? Everyone, please look over here,
- 【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
- Brpc source code analysis (VI) -- detailed explanation of basic socket
猜你喜欢

浅谈低代码技术在物流管理中的应用与创新

Knowledge maps are used to recommend system problems (mvin, Ctrl, ckan, Kred, gaeat)

对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)

OSPF综合实验

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

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

Brpc source code analysis (VI) -- detailed explanation of basic socket
![[RS sampling] a gain tuning dynamic negative sampler for recommendation (WWW 2022)](/img/23/0901da44160ca685d2c694ae9a834b.png)
[RS sampling] a gain tuning dynamic negative sampler for recommendation (WWW 2022)

【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
随机推荐
【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22
【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
The JSP specification requires that an attribute name is preceded by whitespace
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些并发编程知识是你必须要掌握的!完整学习路线!!(建议收藏)
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
GPT plus money (OpenAI CLIP,DALL-E)
[high concurrency] I summarized the best learning route of concurrent programming with 10 diagrams!! (recommended Collection)
Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
[RS sampling] a gain tuning dynamic negative sampler for recommendation (WWW 2022)
Pycharm connects to the remote server SSH -u reports an error: no such file or directory
【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)
dirReader. Readentries compatibility issues. Exception error domexception
Classification parameter stack of JS common built-in object data types
Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher
已解决The JSP specification requires that an attribute name is preceded by whitespace
JS process control
Power Bi -- these skills make the report more "compelling"“