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

Example Analysis of Service Registration and Discovery Eureka in Spring Cloud

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

Share

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

This article will explain in detail the example analysis of service registration and discovery Eureka in Spring Cloud. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

A brief 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. If you don't know it, you can read this article: learn springboot in 2 hours. In addition, if you do not understand the "micro-service architecture", you can search for the "micro-service architecture" through the search engine.

Second, 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 as shown below:

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:

4.0.0 com.forezp eurekaserver 0.0.1-SNAPSHOT jar eurekaserver Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF -8 1.8 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot Spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Dalston.RC1 Pom import org.springframework.boot spring-boot-maven-plugin Spring-milestones Spring Milestones https://repo.spring.io/milestone false

2.3 start a service registry

All you need is an annotation @ EnableEurekaServer, which needs to be added to the startup application class of the springboot project:

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

2.4 eureka is a highly available component

After each instance is registered, a heartbeat needs to be sent 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:

Server: port: 8761eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

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 the browser to visit: http://localhost:8761. The interface is as follows:

No No application available service was found... Of course, no service can be found because there is no registered service.

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:

4.0.0 com.forezp service-hi 0.0.1-SNAPSHOT jar service-hi Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-test test org.springframework.cloud Spring-cloud-dependencies Dalston.RC1 pom import Org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone False

Indicate that you are an eurekaclient by annotating @ EnableEurekaClient.

SpringBootApplication@EnableEurekaClient@RestControllerpublic class ServiceHiApplication {public static void main (String [] args) {SpringApplication.run (ServiceHiApplication.class, args);} @ Value ("${server.port}") String port; @ RequestMapping ("/ hi") public String home (@ RequestParam String name) {return "hi" + name+ ", i am from port:" + port;}}

@ 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:

Eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8762spring: application: name: service-hi

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

This is the end of the article on "sample Analysis of Service Registration and Discovery Eureka in Spring Cloud". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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