Home > Java > Java Tutorial > body text

Microservice service registration center and service discovery tool written in Java

王林
Release: 2023-08-09 11:12:21
Original
1144 people have browsed it

Microservice service registration center and service discovery tool written in Java

Microservice service registration center and service discovery tool written in Java

Introduction

With the popularity of microservice architecture, service registration and discovery became an important component. In the microservice architecture, services actively register with the registration center and discover and connect services through the registration center. This article will introduce how to use Java to write a simple microservice service registration center and service discovery tool.

1. Microservice Service Registration Center

The Microservice Service Registration Center is a centralized component used to manage the registration and discovery of each service. In this article, we will use Spring Boot and Netflix Eureka to implement a service registry.

1.1 Create a Spring Boot project

First, we need to create a project based on Spring Boot. You can use common Java development tools such as Eclipse and IntelliJ IDEA to create projects, and add the following dependencies to the pom.xml file:


    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
Copy after login

1.2 Configure the Eureka server

In the application.properties file, add the following configuration:

spring.application.name=eureka-server
server.port=8761

eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=60000
Copy after login

1.3 Create the Eureka server

Next, create a EurekaServerApplication class, and Use the @EnableEurekaServer annotation to enable the Eureka server.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Copy after login

Now, start the application and the Eureka server will be running on http://localhost:8761.

2. Microservice service discovery tool

Microservice service discovery tool is used to discover available service instances from the service registry. In this article, we will use Netflix Ribbon to implement service discovery.

2.1 Create a Spring Boot project

Similarly, create a project based on Spring Boot and add the following dependencies to the pom.xml file:


    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka
    
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
    
Copy after login

2.2 Configure Eureka client

In the application.properties file, add the following configuration:

spring.application.name=service-discovery-client
server.port=8080

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Copy after login

2.3 Create a service consumer

Next, create a HelloController class and use the Netflix Ribbon to consume the service.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/hello")
    public String hello() {
        List instances = discoveryClient.getInstances("hello-service");
        if (instances != null && !instances.isEmpty()) {
            ServiceInstance serviceInstance = instances.get(0);
            String url = serviceInstance.getUri().toString();
            RestTemplate restTemplate = new RestTemplate();
            return restTemplate.getForObject(url + "/hello", String.class);
        }
        return "No service available.";
    }
}
Copy after login

2.4 Running the service consumer

Finally, start the application and the service consumer will run on http://localhost:8080. By accessing http://localhost:8080/hello, it will consume the /hello interface of the microservice named hello-service.

Conclusion

This article introduces how to use Java to write a microservice service registration center and service discovery tool based on Spring Boot and Netflix Eureka. Using these tools, we can easily implement service registration and discovery in microservice architecture. Hope this article can be helpful to you!

The above is the detailed content of Microservice service registration center and service discovery tool written in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!