当前位置:网站首页>[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
 Insert picture description here

Eureka Registration introduction

Eureka: Service registry ( It can be ⼀ Clusters ) External exposure ⾃⼰ The address of , External exposure ⾃⼰ Service address of
 Insert picture description here

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

 Insert picture description here

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 :
 Insert picture description here

Fill in the module information :

 Insert picture description here

Then fill in the service information :
 Insert picture description here

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 :

 Insert picture description here

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 .
 Insert picture description here

First , Copy the original user-service A launch configuration :

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

Pictured above , The first is 8081 port , The second is 8082 port .

Start two user-service example :

 Insert picture description here

see eureka-server Manage Pages :

 Insert picture description here

🥤 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
 Insert picture description here

原网站

版权声明
本文为[Xiao Wei wants to learn from all the guys]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120916175722.html