当前位置:网站首页>Use of resttemplate and Eureka
Use of resttemplate and Eureka
2022-07-29 01:29:00 【Blue code man】
RestTemplate Use
Write
Let this remote call template register to Spring Inside the container
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}Business level write
@Autowired
private RestTemplate restTemplate;You can call get perhaps post The method
get Namely getforboj that post Namely postobj
Test cases
Before use user Inside is null, Use RestTemplate Remote call query , Return the data
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1. Query order
Order order = orderMapper.findById(orderId);
// 2. create http request url
String url = "http://localhost:8081/user/"+order.getUserId();
// 3. Send the request and convert the format
User user = restTemplate.getForObject(url, User.class);
// 4. return
order.setUser(user);
return order;
}

Eureka What is a registry
What is a service provider and what is a consumer
Service providers : In a business , Services invoked by other microservices .( Provide interfaces to other microservices )
Serving consumers : In a business , Call the services of other microservices .( Call interfaces provided by other microservices )
Consumers can also be providers , Providers will also be consumers .
above RestTemplate What happened
How can service consumers get the address information of service providers ?
If there are multiple service providers , How consumers choose ?
How consumers know the health status of service providers ?
Then use the image Eureka Registration center such a registration center solves

Eureka The role of the registry
In fact, it is to help manage the service interface , Monitor whether the interface is healthy
Official words :
Eureka Use
Set up a registration center Eureka Registry Center
New module
because Eureka You need a module of your own to run
So before you cloud Create a new sub project under the project
Import dependencies in subprojects
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Start class
add @EnableEurekaServer open Eureka
The configuration file
server:
port: 10086
spring:
application:
name: lanlandaimaren # The service name Name yourself as you like
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/ #eureka Service address , The port is set above Start project

Direct access to your port :http://localhost:10086/

register user-service Registration service
Add dependencies under the service module you want to add
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
The configuration file
spring:
application:
name:userservice # Service name ditto
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/ # Same address as above

In this way, the service is registered
Service discovery
The previous method

This is the method used above, so what should we do now , speak localhost Replace with a service name such as :
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1. Query order
Order order = orderMapper.findById(orderId);
// 2. create http request url
String url = "http://userservice/user/" +order.getUserId();
// 3. Send the request and convert the format
User user = restTemplate.getForObject(url, User.class);
// 4. return
order.setUser(user);
return order;
}The effect is the same
Ribbon Load balancing

Thanks for the open source materials of dark horse , Provide learning
such RestTemplate and Eureka The use of is over .
边栏推荐
- Writing assignment 1
- Hilbert transform and instantaneous frequency
- mysql 创建索引的三种方式
- Visual full link log tracking
- PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
- Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
- Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device
- 什么是原码、反码和补码
- 教你一文解决 js 数字精度丢失问题
- Docker-compose安装mysql
猜你喜欢

Flink SQL Hudi actual combat

How to deal with the time, scope and cost constraints in the project?

什么是原码、反码和补码

Summary of process and thread knowledge points 1

Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)

20220728-不纯为数字的字符串排序

面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

nep 2022 cat

【mysql】多指标历史累计去重问题
随机推荐
【mysql】字符串转int
js判断 数组/对象数组 1 是否包含数组/对象数组 2
Intel introduces you to visual recognition -- openvino
How to smoothly go online after MySQL table splitting?
表达式求值
This article enables you to understand the underlying principle of MySQL- Internal structure, index, lock, cluster
System Verilog common syntax
Principle and usage setting of large page memory
How to implement the time impact analysis of the construction project?
PlatoFarm社区生态福音,用户可借助Elephant Swap获得溢价收益
Error reporting: SQL syntax error in flask. Fields in SQL statements need quotation marks when formatting
SDRAM Controller Design (two design methods of digital controller)
C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
Third party login process of flask Weibo
图扑软件亮相 2022 福州数博会,携手共创数字新时代
IT硬件故障的主要原因和预防的最佳实践
log4j动态加载配置文件
【SQL之降龙十八掌】01——亢龙有悔:入门10题
Naver three party login
Flash reports an error: type object 'news' has no attribute' query 'the view name is duplicate with the model name
