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

How to realize the registration and discovery of spring cloud service

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

Share

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

This article focuses on "how to realize the registration and discovery of spring cloud services". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the registration and discovery of spring cloud services.

Introduction to spring cloud

Spring cloud provides developers with some tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision campaigns, distributed sessions, and so on. It runs in a simple environment and can run on developers' computers. In addition, it shows that spring cloud is based on springboot, so you need to have some understanding of springboot in development. In addition, if you do not understand the "micro-service architecture", you can search for the "micro-service architecture" through the search engine.

Create a service registry

Here, we need to use the component on Spring Cloud Netflix Eureka, eureka is a service registration and discovery module.

2.1First, create a maven main project.

2.2 then create two model projects: one model project as the service registry, Eureka Server, and the other as Eureka Client.

Taking the creation of server as an example, the creation process is described in detail:

Right click Project-> create model- > Select spring initialir

Next-> Select cloud discovery- > eureka server, and then go on to the next step.

The pom.xml file of the created project is as follows:

2.3 to start a service registry, you only need an annotation @ EnableEurekaServer, which needs to be added to the startup application class of the springboot project:

* * 2.4* * eureka is a highly available component. It has no backend cache. After registration, each instance needs to send a heartbeat to the registry (so it can be done in memory). By default, erureka server is also an eureka client, and a server must be specified. Configuration file appication.yml for eureka server:

Show that you are an eureka server through eureka.client.registerWithEureka:false and fetchRegistry:false.

2.5 eureka server has an interface, start the project and open a browser to access:

Http://localhost:8761. The interface is as follows:

No No application available service was found... ^ _ ^

Because there is no registration service, of course, no service can be found.

Create a service provider (eureka client)

When client registers with server, it provides some metadata, such as host and port, URL, home page, etc. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registered server.

The creation process is similar to server. The pom.xml is created as follows:

Indicate that you are an eurekaclient by annotating @ EnableEurekaClient.

@ EnableEurekaClient is not enough. You also need to indicate the address of your service registry in the configuration file. The application.yml configuration file is as follows:

It is important to specify the spring.application.name, which is generally based on this name for future service-to-service calls.

Start the project and open http://localhost:8761, the URL of eureka server:

You will find that a service is already registered with the service. The service name is SERVICE-HI and the port is 7862.

When you open http://localhost:8762/hi?name=forezp, you will see on the browser:

Hi forezp,i am from port:8762

At this point, I believe you have a deeper understanding of "how to realize the registration and discovery of spring cloud services". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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