当前位置:网站首页>Eureka basic use
Eureka basic use
2022-07-26 22:53:00 【Charming】
List of articles
1. Service split
1、 Single responsibility : Different microservices , Don't develop the same business repeatedly
2、 Data independence : Do not access databases of other microservices
3、 Service oriented : Expose your business as an interface , For other microservices to call
1.1 According to the order id Order query function
demand : According to the order
idWhile querying the order , Return the user information of the order together
Remote call analysis mode
1、 user The module is exposed to the outside world restful The interface of , As long as you input the corresponding request address in the browser, you can get the corresponding request information .
2、user After receiving the request information, the module will go to the database to query the requested information , Then return the corresponding information to the browser
3、 If order Modules can also initiate http Request ,user The module will also return the corresponding information to order modular
4、order The module combines the local database information , You can query the complete order information
How to be in Java In the code HTTP request
Need to be in order-service in towards user-service To launch a http Request , call
http://localhost:8081/user/{userId}This interface .
1.2 Realization way
Microservice remote call - Query order
1、 Sign up for a RestTemplate Instance to Spring Containers
stay order-service Of OrderApplication Register in RestTemplate
@MapperScan("cn.xdr630.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/** * establish RestTemplate And injected into the Spring In the container * @return */
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
2、 modify order-service In service OrderService Class queryOrderById Method , according to Order Object userId Inquire about User
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1. Query order
Order order = orderMapper.findById(orderId);
// 2. utilize RestTemplate And initiate http request , Query the user
// 2.1 url route
String url = "http://localhost:8081/user/" + order.getUserId();
// 2.2 send out http request , Implement remote call
User user = restTemplate.getForObject(url, User.class);
// 3. encapsulation user To order in
order.setUser(user);
// 4. return
return order;
}
}

1.3 Summary
Microservice invocation mode
be based on RestTemplate Sponsored http Request to implement remote call
http Requesting a remote call is a language independent call , Just know each other's ip、 port 、 Interface path 、 Request parameters .
Realize remote invocation across services , In fact, it is realization http request
2. Providers and consumers
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 )

service A Call the service B, service B Call the service C, So service B What role is it ?
answer :B Both providers and consumers . A service can be either a provider , It can also be a consumer .Service call relationship
1、 Service providers : Expose interfaces to other microservice calls
2、 Serving consumers : Call interfaces provided by other microservices
3、 The roles of provider and consumer are actually relative
4、 A service can be both a service provider and a service consumer
3. Eureka Registry Center
3.1 Problems with service invocation
- Suppose the service provider user-service Multiple instances deployed , Pictured :

- order-service When initiating a remote call , How to know user-service Example of ip Address and port ?
- There are many. user-service Address of the instance ,order-service How to choose when calling ?
- order-service How to learn about a user-service Whether the instance is still healthy , Is it down ?
3.2 Eureka The structure and function of

problem 1:order-service How to know user-service Address of the instance ?
The process of obtaining address information is as follows :
- user-service After the service instance starts , Register your information with eureka-server(Eureka Server side ). This is called service registration
- eureka-server Save the mapping relationship between service name and service instance address list
- order-service According to service name , Pull the instance address list . This is called service discovery or service pull
problem 2:order-service How to start from multiple user-service Select a specific instance from the instance ?
- order-service Select an instance address from the instance list using the load balancing algorithm
- Make a remote call to the instance address
problem 3:order-service How to learn about a user-service Whether the instance is still healthy , Is it down ?
- user-service Every once in a while ( Default 30 second ) towards eureka-server Initiate request , Report your status , It's called heartbeat
- When no heartbeat is sent over a certain period of time ,eureka-server It will be considered that the microservice instance has failed , Remove the instance from the service list
- order-service When pulling Services , You can eliminate the fault instance
Be careful : A microservice , It can be either a service provider , It can also serve consumers , therefore eureka Register the service 、 Service discovery and other functions are uniformly encapsulated in eureka-client End
3.3 Summary
stay Eureka Architecture , There are two types of microservice roles :
1、EurekaServer: Server side , Registry Center
① Record service information
② Heartbeat monitoring
2、EurekaClient: client
- Provider: Service providers , For example, in the case of user-service
- Register your information to EurekaServer
- every other 30 Second direction EurekaServer Send a heartbeat
- consumer: Serving consumers , For example, in the case of order-service
- According to the service name from EurekaServer Pull the service list
- Load balancing based on service list , Select a microservice and initiate a remote call
4. build eureka-server
Registration center server :eureka-server, It must be an independent micro service
4.1 establish eureka-server service
1、 stay cloud-demo Under the parent project , Create a sub module
2、 introduce eureka rely on , introduce SpringCloud by eureka Provided starter rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3、 Write the startup class
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4、 Writing configuration files
Write a application.yml file , The contents are as follows :
server:
port: 10086
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
5、 Start the service
Start microservices , Then visit the :http://127.0.0.1:10086
4.2 Summary
- build EurekaServer
① introduce eureka-server rely on
② add to @EnableEurekaServer annotation
③ stay application.yml Middle configuration eureka Address
5. Service registration
- take user-service Sign up to eureka-server in
1、 Introduce dependencies
stay user-service Of pom In file , Introduce the following eureka-client rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、 The configuration file
stay user-service in , modify application.yml file , Add service name 、eureka Address :
spring:
application:
name: userservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
- orderservice Registration as above
3、 Start multiple user-service example
To demonstrate a scenario where a service has multiple instances , Add one SpringBoot Start configuration of , Simulate multi instance deployment , Start one more user-service.
First , Copy the original user-service A launch configuration :

To avoid port conflicts , Port settings need to be modified :


see eureka-server Manage Pages :

6. Service discovery
- take order-service Logic modification of : towards eureka-server Pull user-service Information about , Implementing service discovery .
1、 Introduce dependencies
Service discovery 、 Service registration is uniformly encapsulated in eureka-client rely on , Therefore, this step is consistent with the service registration .
stay order-service Of pom In file , Introduce the following eureka-client rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、 The configuration file
Service discovery also needs to know eureka Address , Therefore, the second step is consistent with the service registration , It's all configuration eureka Information :
stay order-service in , modify application.yml file , Add service name 、eureka Address
spring:
application:
name: orderservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
3、 Service pull and load balancing
eureka-server Middle pull user-service List of instances of the service , And load balancing .
But we don't have to do these moves , Just add some comments .
- stay order-service Of OrderApplication in , to RestTemplate This Bean Add one
@LoadBalancedannotation :
@MapperScan("cn.xdr630.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/** * establish RestTemplate And injected into the Spring In the container * @return */
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
modify order-service In service service Under bag OrderService Class queryOrderById Method . Modify access url route , use service name Instead of ip、 port :

spring Automatically from eureka-server End , according to
userserviceThis service name , Get instance list , Then load balancing is completed .test : visit orderservice The service call userservice User information for , visit 6 Next time , You can see on the console that the request is userservice:8081 or userservice:8082 , It can be observed that orderservice It's random access userservice 8081 or 8082 Of , It shows that load balancing is realized .
7. Summary
1、 build EurekaServer
① introduce eureka-server rely on
② add to @EnableEurekaServer annotation
③ stay application.yml Middle configuration eureka Address
2、 Service registration
① introduce eureka-client rely on
② stay application.yml Middle configuration eureka Address
③ Service discovery
3、 introduce eureka-client rely on
① stay application.yml Middle configuration eureka Address
② to RestTemplate add to @LoadBalanced annotation
③ Call... Remotely with the service name of the service provider
边栏推荐
- [hcip] OSPF route calculation
- 【HCIP】OSPF 特殊区域、汇总、认证
- A13 processor has become the biggest highlight of iphone11 series: its performance is twice that of Kirin 980!
- Parameter analysis and stone jumping board
- Mate30系列发布:华为的重构影像之路还能走多远?
- 7.27 watch first | openeuler is ambitious, open source Huizhi creates the future - the most detailed agenda of the Euler sub forum of the open atom global open source summit is released
- Embedded SIG | 分布式软总线
- 测试开发是开发吗?
- 动态规划之线性DP
- Introduction to MySQL database
猜你喜欢

Embedded SIG | 分布式软总线

Embedded sig | distributed soft bus

数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款

Parameter analysis and stone jumping board

2022 latest Tibet Construction Safety Officer simulation question bank and answers

Summary of shell related commands

Those environment configurations and plug-ins of idea

Interview: your most impressive bug, for example

Apifox--比 Postman 还好用的 API 测试工具

基于信心上界蒙特卡洛搜索树(UCT)实现四子棋
随机推荐
Apifox -- a better API testing tool than postman
华为密谋收购巴西运营商?
Use ECs and OSS to set up personal network disk
Makefile related syntax summary (openc910)
what is qrc in qt
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
what crlf mean
华为Atlas900揭秘:集成数千颗昇腾910芯片,算力堪比50万台PC!
A13 processor has become the biggest highlight of iphone11 series: its performance is twice that of Kirin 980!
【HCIP】OSPF 特殊区域、汇总、认证
Eureka基本使用
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ )
Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ )
Mate30系列发布:华为的重构影像之路还能走多远?
Calendar documents implemented by several qwidgets
Interview: your most impressive bug, for example
数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款