Home >Java >javaTutorial >Microservice service registration center and service discovery tool written in Java
Microservice service registration center and service discovery tool written in Java
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.
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.
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:
<dependencies> <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-server</artifactId> </dependency> </dependencies>
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
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); } }
Now, start the application and the Eureka server will be running on http://localhost:8761
.
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.
Similarly, create a project based on Spring Boot and add the following dependencies to the pom.xml
file:
<dependencies> <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</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>
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/
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<ServiceInstance> 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."; } }
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
.
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!