[spring cloud series] first experience of Eureka Registration Center


[spring cloud series] first experience of Eureka Registration Center

[spring cloud series] first experience of Eureka Registration Center

In the spring cloud microservice system, there are several important components, such as registration center, configuration center, gateway, security, load balancing, monitoring and so on. Next, let’s take a look at what these commonly used components are and how to use them under the microservice architecture.

This article is the first part, the use of Eureka registry

<!– more –>

1. Basic introduction

1. Registration Center

Registration Center, the main core point is the registration and discovery of services.

In short, all our services will identify themselves on the registry. The registry uniformly manages the mapping relationship between all service names and specific applications. In this way, the access between microservices can communicate with each other directly through service names, compared with directly through IP The advantage of port access is that when a service is offline, added or changed, the caller only needs to maintain an up-to-date mapping table of the registry without any other logic changes.

The registries we usually use areEureka, Consul, Zookeeper, nacosWe will introduce them one by one in our subsequent tutorials

Eureka2. X closed source, 1. X is available, but it is not recommended for new projects, such asConsul, nacosAll good choices

If it is for the purpose of learning, or for historical reasons (such as me), it is not harmful to learn about Eureka

2. Eureka

Eureka is an open source service discovery component of Netflix. It is a rest based service, usually including server and client

The principle is shown in the figure below

[spring cloud series] first experience of Eureka Registration Center

  • Server: provides service registration and stores the information of all available service nodes in the service registry
  • Client: simplifies the interaction with the server, such as encapsulating the basic operations such as sending heartbeat and obtaining registration information table

2. Example demonstration

1. Version Description

In the following demonstration projects, our environment and version information are as follows

  • Development environment: idea + maven
  • SpringBoot: 2.2.1.RELEASE
  • SpringCloud: Hoxton.M2

2. Eureka server

Eureka distinguishes between the server and the client, that is, we have an independent registry service, and other microservices are used as the client side

Server side core dependencies are as follows



Then, in the configuration file, add some basic information

  Port: 8081 service registry port number
    Hostname: ᦇ service registry IP address
    Registerwitheureka: false ා do you want to register yourself with the service registry
    Fetchregistry: false ා do you want to retrieve the service
    Serviceurl: # the configuration content of the service registry, which specifies the location of the service registry
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Notice, aboveregisterWithEurekaIf this configuration is set to false, it is not like registering services by yourself (how to configure multiple Eureka instances will be introduced later)

Then start the class and add comments@EnableEurekaServerTo claim Eureka service

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

At this point, a eureka server has been completed, and we can directly access ithttp://localhost:8081You will see a self-contained console that provides some basic information

3. Eureka client

Here we design two clients, one providing service and the other calling, to demonstrate the basic functions of Eureka

a. Client Eureka service provider

The client needs to add the following key dependencies to the POM file


Next, you need to specify the address of the registry and the service name in the configuration file (please note that this service name is an important clue, which will be used later)

  Port: 8082 service port number
    Serviceurl: # registered address of registry
    Name: Eureka service provider # service name -- the method to call the service according to the name when calling

Similarly, on the startup class, the@EnableEurekaClientTo label the client

public class Application {

    public static void main(String[] args) {

In this project, we write a basic rest interface for subsequent services to call

@RequestMapping(path = "userService")
public class UserService {
    @RequestMapping(path = "/getUserById")
    public UserDTO getUserById(@RequestParam long userId) {
        UserDTO userDTO = new UserDTO();
        userDTO.setNickname (a gray blog);
        return userDTO;

If you take a look at the above implementation, you will find that there is no difference between the controller and the controller

Here, the first Eureka client has been completed and a rest interface has been provided. Next, we will write a second Eureka client to access the rest service above

b. Client Eureka service consumer

There is no difference between the basic process and the above, just change the configuration file slightly

  Port: 8083 service port number
    Serviceurl: # registered address of registry
    Name: Eureka service consumer # service name -- the method to call the service according to the name when calling

So in this service, how to accesseureka-service-providerWhat about the services provided?

  • adoptRestTemplateTo achieve

Please note that this resttemplate is similar to our normalnew RestTemplate()The creation is not the same. We get the instance in the following way

public RestTemplate rest() {
    return new RestTemplate();

Focus on methodological@LoadBalancedAnnotations, which will be covered in more detail in the next section of the ribbon, are only required to know what is generated through itRestTemplateWhen the access is initiated, the service name will be translated into the corresponding one with the help of Eureka’s registration information tableIP + port number

The following is our visit, which is written as follows

private RestTemplate restTemplate;

@GetMapping(path = "uid")
public String getUser(int userId) {
    UserDTO dto = restTemplate
            .getForObject("http://eureka-service-provider/userService/getUserById?userId=" + userId, UserDTO.class);

    return userId + "'s info: " + dto;

Please focus on the URL to visit:"http://eureka-service-provider/userService/getUserById?userId=" + userIdThere is no domain name or IP address here. You can access it directly through the service name

4. Test and summary

We start the above server and two clients in turn, and then access thehttp://localhost:8081To view the Eureka console, you can see the following interface. Both clients have been registered

[spring cloud series] first experience of Eureka Registration Center

Then test the service of provider through consumer

[spring cloud series] first experience of Eureka Registration Center

By now, Eureka’s core functions have been demonstrated. Of course, if only this is the case, the learning cost seems to be very low. As a young man with thinking, he will naturally have a few questions after reading the above process

  • safety problem

    • Registry console direct access, this if exposed
    • An example of Eureka, how to solve the single point fault
  • How long does the service registration take effect? How long will the service be removed from the registration information form? What is the service survival judgment?
  • adoptRestTemplateIs there any way similar to RMI to call by class?

2. Others

0. Project

  • Project: https://github.com/liuyueyi/spring-boot-demo
  • Source code:- https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-cloud/eurka-server – https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-cloud/eurka-service-consumer – https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-cloud/eurka-service-provider

1. A gray blog

It’s not as good as a letter. The above contents are all from one family. Due to limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct, and thank you

The following is a gray personal blog, recording all the blog articles in study and work. Welcome to visit

  • Personal blog https://blog.hhui.top
  • A grey blog spring blog http://spring.hhui.top

[spring cloud series] first experience of Eureka Registration Center