Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the service registration and discovery of Eureka in Spring Cloud Netflix?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Spring Cloud Netflix Eureka service registration and discovery of what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Introduction to Spring cloud

Spring Cloud provides developers with tools to quickly build distributed systems (such as configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control buses, one-time tokens, global locks, leadership elections, distributed sessions, cluster state, etc.). Developers can use Spring Cloud to quickly build applications with the above functions. They work in any distributed environment, including developers' own laptops, bare metal data centers, and managed platforms such as Cloud Foundry.

Create a service registry

Let's create a project to act as a service registry. Let's talk about the creation process in detail:

Create a new service registry

Create a new project in IDEA-> Select spring initialir as shown below:

Click next- > fill in the project metadata information:

Click next- > Select Spring Cloud Discovery- > select Eureka Server on the right, and then click next to know that the project is created successfully:

After the creation, the internal use of pom.xml is as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE com.noodles.mars eureka-server 0.0.1-SNAPSHOT eureka-server Demo project for Spring Boot 11 Greenwich.SR3 org.springframework.cloud spring-cloud-starter-netflix-eureka-server Org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Org.springframework.boot spring-boot-maven-plugin enables service registry configuration

After relying on the spring-cloud-starter-netflix-eureka-server package, enabling a service registry is easy, as long as you annotate @ EnableEurekaServer on the startup class:

@ EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main (String [] args) {SpringApplication.run (EurekaServerApplication.class, args);}} Service Registry Project configuration

Eureka is a highly available component. There is no backend cache component. After registration, each instance needs to send a heartbeat to the service registry. By default, an Eureka server is also a client, and a server must be specified. Configuration file for the service registry:

Server: port: 9090eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:9090/eureka

Start the registry and open the browser to access: http://localhost:9090, will see the following interface, and the registration list is empty:

Create a new service provider

The project creation procedure is the same as creating a registry, except that you select Eureka Discovery Client when you select Spring Cloud Discovery:

The service provider project pom.xml is as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE com.noodles.mars eureka-client 0.0.1-SNAPSHOT eureka-client Demo project for Spring Boot 11 Greenwich.SR3 org.springframework.cloud spring-cloud-starter-netflix-eureka-client Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} Pom import org.springframework.boot spring-boot-maven-plugin

It is also easy to enable the registration service. First of all, note @ EnableEurekaClient on the Spring boot startup class to indicate that you are EurekaClient:

@ EnableEurekaClient@SpringBootApplicationpublic class EurekaClientApplication {public static void main (String [] args) {SpringApplication.run (EurekaClientApplication.class, args);}}

Then configure the service registry information:

Server: port: 8040eureka: client: service-url: defaultZone: http://localhost:9090/eureka/spring: application: name: hello-erueka-client

Note: the spring.application.name needs to be specified here, and the application name will be used in subsequent inter-service calls.

Provide an interface here, and then test the call:

RestControllerpublic class HelloController {@ Value ("${server.port}") int port; @ GetMapping ("/ hello") public String hello (@ RequestParam ("name") String name) {return String.format ("Hello, My name is% s, I'm from port:% d", name, port);}}

Start the project, open a browser and enter http://localhost:9090, to open the registry URL:

After opening the web page, you will find that your service provider is already registered in the service registry.

Using Postman or accessing the service provider's interface directly in the browser, you will see the response:

Hello, My name is Mars, I'm from port: 8040 is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report