当前位置:网站首页>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
边栏推荐
- [MySQL] - index principle and use
- 2022 latest Tibet Construction Safety Officer simulation question bank and answers
- Five years after graduation, I changed from information management to software testing engineer, and my monthly salary finally exceeded 12K
- 【HCIP】OSPF 关系建立
- sql多表查询的练习
- 三星Galaxy Z可折叠产品的预告片泄露:'柔性好于平面'
- Promote the replacement of X86 with arm server chips, and Huawei and Feiteng carry the banner of localization!
- Development status of Dao
- Calendar documents implemented by several qwidgets
- Counter attack dark horse: devdbops training, give you the best courses!
猜你喜欢

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

nvidia-smi报错:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver 完整记录

Embedded sig | distributed soft bus

Incremental secure file system SFS based on C language design
![[hcip] OSPF relationship establishment](/img/19/e03fea44f2908c7b585e7a1f87c075.png)
[hcip] OSPF relationship establishment

比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算

Implementation principle of semaphore in golang

How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged

APP信息侦察&夜神模拟器Burp抓包配置
![[hcip] OSPF route calculation](/img/1c/ee9eee2e723b850c401f7cddda1b27.png)
[hcip] OSPF route calculation
随机推荐
KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
Arduino experiment I: two color lamp experiment
What if redis memory is full? This is the right way to deal with it
8-其他编程语言--记录
国产DRAM年底将量产,但前路依旧漫漫!
Let the program do one thing in one or more seconds
Interview Essentials
纷享销客罗旭对话西门子王海滨:先进制造,要向数字化要效益
IDEA的那些环境配置及插件
Mate30 series release: how far can Huawei go in reconstructing images?
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ )
NVIDIA SMI error: NVIDIA SMI has failed because it could't communicate with the NVIDIA driver complete record
Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
Day07 MySql知识点再总结与多表查询
云原生微服务第一章之服务器环境说明
基于C语言的页式文件系统
2022 latest Tibet Construction Safety Officer simulation question bank and answers
【HCIP】OSPF 外部路由引入
Docker uses mysql:5.6 and owncloud image to build a personal network disk, install and build a private warehouse harbor
总投资100亿美元,华虹无锡12吋晶圆厂正式投产