The construction of the client under the Spring Cloud microservice single deployment Eureka server

eureka-client

When the service discovery component is built, how does the microservice register with the service discovery component? When building an eureka cluster, how to keep the registry consistent between eureka-server? This all involves eureka-client (eureka client). The role of eureka-client is to register information with eureka-server, obtain registry information from eureka-server, etc.
If this is a little confusing, we can say that. When talking about the single deployment of Eureka services earlier, it was said that eureka-server is a news monger. Then eureka-client is a mobile phone. The microservice has a mobile phone, it tells the messenger its name and contact information, and tells eureka-server, if someone comes to it (eureka-server) with its (eureka-client) name, then put it (eureka- client) position. This is registration. Of course, the microservice can also obtain the contact information of other microservices from the eureka-server by phone. What’s more, this mobile phone (eureka-client) also has the function of saving the contact information of other microservices obtained from the message dealer. In this way, if you want to find that microservice again next time, you don’t have to ask the news dealer all the time. Well, after all, phone bills cost money.
In this way, the mobile phone (eureka-client) is very important. Without the mobile phone, there is no microservice that can contact the message vendor (eureka-server), and of course the message vendor cannot provide any information.
So how does a microservice buy a mobile phone that can communicate with a news dealer?

Implement the eureka client

  1. Add eureka-client dependency

    <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>
  2. Add annotation @EnableEurekaClient to EurekaClientApplication

    package com.wangguitang.freedom.study.spring.cloud.eureka.client;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }
  3. Add configuration in the configuration file application.yml

    server:
      port: 8080
    
    spring:
      application:
        name: eureka-client
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka

Start Eureka-client

  1. Right-click the class EurekaClientApplication and select Run As–>Spring Boot App

github source code

https://github.com/wang465745776/freedom-study-spring-cloud-parent/tree/master/freedom-study-spring-cloud-eureka-client

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685187&siteId=291194637