[Spring Cloud combat: Eureka] service registration and discovery

Spring Cloud Eureka: service registration and discovery

Eureka is an open source Netflix product that provides service registration and discovery. It provides a complete implementation of Service Registry and Service Discovery. It is also one of the most important and core components in the SpringCloud system.

Background introduction

Microservices are deployed independently and have clear boundaries, and complex business functions are built through remote calls between services.
Then why do you need to refer to service registration and discovery? What are the specific problems to be solved for service registration and discovery?

Service registration and discovery mainly solve the following two important problems:

  • 1) Shielding and decoupling the details of interdependence between services:
    • We know that remote calls between services must know each other's IP and port information. We can directly configure the callee's IP and port on the caller. This way the caller directly relies on the IP and port has obvious problems. For example, after the called IP and port change, the calling method must be modified synchronously.
    • Through service discovery, the dependency of IP and port between services is transformed into dependency of service name. The service name can be identified according to the microservice business. Therefore, the dependency details between shielding and decoupling services are service discovery and registration resolution. The first question.
  • 2) Dynamic management of microservices:
    • In the microservice architecture, there are many services, and the interdependence between services is also intricate. Whether the service stops actively, unexpectedly hangs, or expands the service implementation due to increased traffic, dynamic changes in the service data or status are required. The callee is notified as soon as possible, and then the callee takes corresponding measures. Therefore, for service registration and discovery, the data and status of the service should be managed in real time, including service registration online, service active offline, and abnormal service removal.

Introduction to Eureka

Spring Cloud encapsulates the Eureka module developed by Netflix to implement service registration and discovery. Eureka adopts the design architecture of CS.

  • Eureka Server serves as the server for the service registration function, and it is the service registration center. Other microservices in the system use Eureka's client to connect to the Eureka Server and maintain a heartbeat connection.
  • Maintenance personnel can use Eureka Server to monitor whether each microservice in the system is operating normally.
  • Some other modules of Spring Cloud (such as Zuul) can use Eureka Server to discover other microservices in the system and execute related logic.
  • Eureka consists of two components: Eureka server and Eureka client. The Eureka server is used as a service registration server.
  • The basic structure of Eureka consists of 3 roles:
    • 1. Eureka Server: Provide service registration and discovery
    • 2. Service Provider: The service provider registers its own service in Eureka so that other services can be found.
    • 3. Service Consumer: The service consumer obtains a list of registered services from Eureka, so that the service can be consumed.

Build Eureka registration center

Let's create and run the Eureka registry to see the correct posture for creating and running SpringCloud applications in IDEA.

  • Development tool idea
  • Java version 1.8
  • SpringBoot version 2.3.1.RELEASE
  • SpringCloud version Hoxton.SR6

Use IDEA to create SpringCloud applications

  • It is convenient for learning to create a cloud-learn project, and the source code will be placed on GitHub in the future.
  • The process is shown in the figure:
    Create flowchart
  • In the project just created, create an eureka-learn module, and use Spring Initializer to initialize a SpringBoot project
  • The process is shown in the figure:
    Create flowchart
  • Open the pom.xml file of the eureka-learn module, you will find that there is an additional eureka-server dependency
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
  • Just add the @EnableEurekaServer annotation to the startup class to enable the function of the eureka registry
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServiceApplication {
          
          
    
        public static void main(String[] args) {
          
          
            SpringApplication.run(EurekaServiceApplication.class, args);
        }
    }
    
  • Modify the configuration file application.yml, add the relevant configuration of the eureka registry
    server:
      port: 8001 #指定运行端口
    spring:
      application:
        name: eureka-server #指定服务名称
    eureka:
      instance:
        hostname: localhost #指定主机地址
      client:
        fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
        register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
      server:
        enable-self-preservation: false #关闭保护模式
    
  • After the operation is completed, visit the address http://localhost:8001/ to see the interface of the Eureka registry
  • as the picture shows:
    Page map

Build Eureka client service

  • Create a new eureka-client module in the project and add the following dependencies in pom.xml
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  • Adding the @EnableDiscoveryClient annotation to the startup class indicates that it is an Eureka client
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaServiceClientApplication {
          
          
    
        public static void main(String[] args) {
          
          
            SpringApplication.run(EurekaServiceClientApplication.class, args);
        }
    }
    
  • Add the relevant configuration of the Eureka client to the configuration file application.yml
    server:
      port: 8101 #运行端口号
    spring:
      application:
        name: eureka-client #服务名称
    eureka:
      client:
        register-with-eureka: true #注册到Eureka的注册中心
        fetch-registry: true #获取注册实例列表
        service-url:
          defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
    
  • Check the registration center http://localhost:8001/ and find that the Eureka client has been successfully registered
  • as the picture shows:
    Registered renderings

Build an Eureka registry with certification

  • Create an eureka-security-server module and add the following dependencies in pom.xml
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  • Modify the application.yml configuration file
    server:
      port: 8004
    spring:
      application:
        name: eureka-security-server
      security: #配置SpringSecurity登录用户名和密码
        user:
          name: peter
          password: 20200314
    eureka:
      instance:
        hostname: localhost
      client:
        fetch-registry: false
        register-with-eureka: false
    
  • Because of the introduction of spring-security dependency, you need to add Java configuration WebSecurityConfig
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
          
          
        @Override
        protected void configure(HttpSecurity http) throws Exception {
          
          
            http.csrf().ignoringAntMatchers("/eureka/**");
            super.configure(http);
        }
    }
    
  • Run eureka-security-server, visit http://localhost:8004 and find that login authentication is required
  • as the picture shows
    Image that requires login authentication

Eureka-client registers to the registration center with login authentication

  • The address format of the registry needs to be modified in the configuration file
    http://${
          
          username}:${
          
          password}@${
          
          hostname}:${
          
          port}/eureka/
    
  • Add the application-security.yml configuration file and modify the user name and password according to the format
    server:
      port: 8103
    spring:
      application:
        name: eureka-client
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://peter:20200314@localhost:8004/eureka/
    

About Eureka common configuration

  • The configuration is as follows:
    eureka:
      client:  #eureka客户端配置
        register-with-eureka: true  #是否将自己注册到eureka服务端上去
        fetch-registry: true #是否获取eureka服务端上注册的服务列表
        service-url:
          defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
        enabled: true # 启用eureka客户端
        registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
      instance: #eureka客户端实例配置
        lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
        lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
        metadata-map:
          zone: jiangsu #所在区域
        hostname: localhost #服务主机名称
        prefer-ip-address: false #是否优先使用ip来作为主机名
      server: #eureka服务端配置
        enable-self-preservation: false #关闭eureka服务端的保护机制
    

review

expect

Project source address

Guess you like

Origin blog.csdn.net/Strive_Peter/article/details/113949635