Eureka registry & Service Registration & Service Discovery

Eureka is divided into Eureka server and other services become Eureka client

Eureka basic principle:

Eureka is actually a registration center, equivalent to the matchmaker in our life.

Eureka actual operation mechanism

  • If we want to start three login services.
  • Each of our login services will send registration information to eureka
  • Eureka server will record these ip and port information.
  • If a login service is requested, one of the three healthy login services will be randomly selected to process the login service.
  • At the same time, each Eureka client will send heartbeat packets to Eureka server every 30 seconds. If it fails to do so for more than 30 seconds, it will be rejected by Eureka server

By integrating the above mechanisms, the function of the registration center is realized.

code implementation

Server

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
copy

configuration file

# Eureka server configuration information
server.port=8090
spring.application.name=Eureka-Server
# Register yourself with Eureka. You are both a server and a client. When you are a client to Eureka, you should use commas to separate them for the convenience of Eureka server cluster
eureka.client.service-url.defaultZone=http://127.0.0.1:8090/eureka
copy

Startup class

@SpringBootApplication
@EnableEurekaServer // Specified as Eureka server
@Slf4j
public class XxEurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(XxEurekaServerApplication.class, args);
        System.out.println("After the project is started successfully, go to the service http://localhost:8090 / will display Eureka's dashboard ");
    }

}
copy

Start the project, wait for 30 seconds, access: 127.0.0.1:8090

Client use

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
copy
# Eureka client configuration information
server.port=8091
spring.application.name=Eureka-Client
# Register yourself with Eureka
eureka.client.service-url.defaultZone=http://127.0.0.1:8090/eureka
copy

Startup class

@SpringBootApplication
@EnableEurekaClient // Specified as Eureka client
@Slf4j
public class XxEurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(XxEurekaClientApplication.class, args);
    }

    @Bean
    @LoadBalanced // The default is load balancing
    public RestTemplate initRest() {
        return new RestTemplate();
    }

}
copy

Any Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class DiyController {

    @RequestMapping("/abc")
    public String hello() {
        System.out.println("The request came in");
        return "OK";
    }

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/go")
    public String Simulate load balancing requests() {
        // Test remote call
        ResponseEntity<Object> forEntity = restTemplate.getForEntity("http://EUREKA-CLIENT/abc", null);
        System.out.println("The request came in");
        return "OK";
    }
}
copy

Start the project, wait for 30 seconds, visit: 127.0.0.1:8090 Eureka, register its center, and there will be a following

This completes the registration of the service.

How can service discovery be reflected?

We then directly test, http://127.0.0.1:8091/go It uses RestTemplate to send a GET request, and the address pointed to is not fixed. Instead, it sends the request through the target service address obtained from Eureka. The request EUREKA-CLIENT represents 192.168.0.106: 8091 or 8092 in the above figure.

Registration and discovery from Eureka is complete!

But Eureka only has service registration and discovery, and the load balancing is the Ribbon (you can click the @ Loadbalance annotation to see): https://www.zanglikun.com/13046.html

Special note: the above articles are all my actual operation, and I will not steal other people's articles! Please do not directly embezzle! Reprint remember to mark the source!

Posted by benjaminbeazy on Wed, 24 Aug 2022 12:20:02 +0530