当前位置:网站首页>[cloud native] establishment of Eureka service registration
[cloud native] establishment of Eureka service registration
2022-06-12 09:25:00 【Xiao Wei wants to learn from all the guys】
Preface :
This article mainly introduces the service registry Eureka And some important parts of it . Read some information , Summarized below . I hope I can deepen my impression and help you guys
If the article needs any improvement, please give me more advice
Xiao Wei, first of all, thank you all 
List of articles
Eureka Registration introduction
Eureka: Service registry ( It can be ⼀ Clusters ) External exposure ⾃⼰ The address of , External exposure ⾃⼰ Service address of 
Service providers : Starts to Eureka register ⾃⼰ Information ( Address , What services are provided )
consumer : towards Eureka A subscription service ,Eureka A list of all provider addresses for the corresponding service will be sent to the consumer , And regularly updated ⼼ jump ( The contract ): Provider passes on a regular basis http⽅ Direction Eureka Refresh your status
Erueka-Server: For discovering and registering application services
Application-Service: Service provider , nesting Eureka-Client, towards Eureka-Server Register your own service
Application-Client: Consumers of service , nesting Eureka-Client, from Eureka-Server Get a list of services
Make Remote Call: Represents a remote call

build eureka-server
The registry is the address book of the microservice architecture , Record the mapping relationship between service and service address . In a distributed architecture , The service will register here , When a service needs to call another service , Just find the address of the service here , To call .
First of all, let's register the service side of the center :eureka-server, This must be an independent microservice
The first step is to create eureka-server service
stay cloud-demo Under the parent project , Create a sub module :
Fill in the module information :

Then fill in the service information :
introduce SpringCloud by eureka Provided starter rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Write the startup class
to eureka-server Write a startup class for the service , Be sure to add a @EnableEurekaServer annotation , Turn on eureka The registry function of :
package cn.itcast.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
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
Start the service
Start microservices , Then visit the :http://127.0.0.1:10086
The following results will appear :

Service registration
We need to user-service Sign up to eureka-server In the middle .
The first step is still to 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>
The second step requires a 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
Because you want to cluster , Load balancing , So start multiple user-service example
To demonstrate a scenario where a service has multiple instances , Let's add a SpringBoot Start configuration of , Start one more user-service.
build Eureka colony
eureka Service cluster , Mutual advance ⾏ register , And on the client side, each eureka The service address is configured in the client configuration ⽂ In the piece .
Eureka Server That is, the service is being registered ⼼, In the case just now , We only have ⼀ individual EurekaServer, in fact EurekaServer It can also be ⼀ Clusters , Form highly available Eureka in ⼼.
Service synchronization :
Multiple Eureka Server They also register with each other as services , When the service provider registers with Eureka Server When a node in the cluster , This node synchronizes the service information to each node in the cluster , from ⽽ Data synchronization . therefore ,⽆ On client access to Eureka Server Any in the cluster ⼀ Nodes , Can get complete service list information .
First , Copy the original user-service A launch configuration :

then , In the pop-up window , Fill in information :

here ,SpringBoot Two windows appear user-service A launch configuration :

Pictured above , The first is 8081 port , The second is 8082 port .
Start two user-service example :

see eureka-server Manage Pages :

🥤 Service discovery
We will order-service Logic modification of : towards eureka-server Pull user-service Information about , Implementing service discovery .
Introduce dependencies
I said before , 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>
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
Service pull and load balancing
Last , We're going 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 @LoadBalanced annotation :
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/** * establish RestTemplate And injection Spring Containers */
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
/* @Bean public IRule randomRule() { return new RandomRule(); }*/
}
modify order-service In service cn.itcast.order.service Under bag OrderService Class queryOrderById Method . Modify access url route , Use the service name instead of ip、 port :
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1. Query order
Order order = orderMapper.findById(orderId);
// 2. utilize RestTemplate launch http request , Query the user
// 2.1.url route
String url = "http://userservice/user/" + order.getUserId();
// 2.2. send out http request , Implement remote call
User user = restTemplate.getForObject(url, User.class);
// 3. encapsulation user To Order
order.setUser(user);
// 4. return
return order;
}
spring Will automatically help us from eureka-server End , according to userservice This service name , Get instance list , Then load balancing is completed . In the next chapter we will talk about Ribbon Knowledge of load balancing , So that we can be optimized in the project .
This is the end of the article , Thank you for your support , And hope to help you
If there is anything to be improved, please give me more advice
Xiaowei thanks you again for your support 
边栏推荐
猜你喜欢

More than 90% of the software companies will ask the software test interview questions. Hurry to recite them

Distributed transaction solution 2: message queue to achieve final consistency

MySQL-MVCC

Description of string

Implementation of hotspot reference

科创人·世界500强集团CIO李洋:数字化转型成事在人,决策者应时刻聚焦于「柴」

测试用例如何编写?

【极术公开课预告】Arm最强MCU内核Cortex-M85处理器,全方位助力物联网创新(有抽奖)

Mysql5.7 partition table
Common technical questions in functional test interview. Would you like to summarize them?
随机推荐
Subtractive integer (number theory)
Auto.js学习笔记10:实例化自定义对象,在子线程使用JSON.stringify()方法导致报错(已解决)
Autojs微信研究:微信不同的版本或模拟器上的微信里的控件ip是不同的。
How to write test cases?
Auto.js学习笔记5:autojs的UI界面基础篇1
使用Visual Studio 2017创建简单的窗口程序
Mysql database ignores case
Complete knapsack problem 1
Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
Financial test interview questions to help you get the offer
2026年中国软件定义存储市场容量将接近45.1亿美元
Quick sort
Auto.js学习笔记8:常用且重要的一些API
Auto.js调试:使用雷电模拟器的网络模式进行调试
【云原生】Eureka服务注册的搭建
Basic exercise decomposing prime factors
SQL basic syntax II
Autojs学习笔记6:text(txt).findOne()切换app时会报错,最后解决实现效果,切换任何app直到脚本找到指定的txt文字的控件进行点击。
Is it necessary to separate databases and tables for MySQL single table data of 5million?
Full alphabetic arrangement (pure alphabetic password dictionary)