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 use Spring Cloud to build a service registry

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

Share

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

How to use Spring Cloud to build a service registration center, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Unlike dubbo, Spring Cloud is an one-stop distributed framework. Spring Cloud provides developers with tools to quickly build some common patterns in distributed systems (such as configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control buses). The coordination of distributed systems leads to template patterns, and developers using Spring Cloud can quickly support services and applications that implement these patterns. They will work well in any distributed environment, including developers' own laptops, bare metal data centers, and hosting platforms such as Cloud Foundry. So today I hope to take my friends to know about Spring Cloud through a simple case.

OK, so in this article, I would like to introduce the use of Eureka in Spring Cloud to build a service registry, and then register the service with it. Since Spring Cloud builds distributed services on top of Spring Boot, you need to have a little knowledge of Spring Boot to read this article.

OK, so what is mentioned above is the basic knowledge needed to read this article, based on which let's take a look at how to use Eureka in Spring Cloud to build a service registry.

First of all, I would like to say that Spring Cloud is not a thing, similar to Hadoop, Spring Cloud also contains many sub-projects, the Eureka we are going to look at today is just one of them. The function of Eureka is somewhat similar to the zookeeper we wrote before. It is a service governance component, including service registry, service registration and discovery mechanism. We will not introduce the other components here, but will mention them one by one in the following series of articles.

OK, you've been talking a lot of nonsense. Let's code it.

Create a service registry to create a normal Spring Boot project

First of all, we need to create a normal Spring Boot project, named eureka-server. How common is it? There is no need to add a starter, and only a parent starter is referenced after the creation is successful.

Add Eureka dependency

After the project is successfully created, add the dependency of eureka-server to the pom.xml file. Currently, the stable version of eureka is Dalston.SR3. After adding the dependency, the pom.xml file is as follows:

4.0.0 org.sang eureka-server 0.0.1-SNAPSHOT jar eureka-server Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter Org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Dalston.SR3 Pom import org.springframework.boot spring-boot-maven-plugin

So I mainly refer to the http://projects.spring.io/spring-cloud/ of the Eureka official website for the dependent addition here.

Start a service registry

The simple way to start a service registry is to add a @ EnableEurekaServer annotation to the entry class of Spring Boot, as follows:

@ EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main (String [] args) {SpringApplication.run (EurekaServerApplication.class, args);}} configure the service registry

Finally, we can do a little more simple configuration. The configuration is written in the configuration file application.properties of Spring Boot, as follows:

Server.port=1111eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/

OK, so with regard to these lines of comments, I would like to make the following points:

1.server.port=1111 means to set the port number of the service registry

2.eureka.instance.hostname=localhost means to set the hostname of the service registry

3.eureka.client.register-with-eureka=false, since the application we are currently creating is a service registry, not an ordinary application, by default, the application registers itself with the registry (and itself) and sets it to false to disable this default behavior

4.eureka.client.fetch-registry=false, which means not to retrieve other services, because the service registry itself is responsible for maintaining service instances, and it does not need to retrieve other services.

test

OK, after all this, we can start this Spring Boot service. After the service starts successfully, type: http://localhost:1111 in the browser to see the following page:

OK, after seeing the above page, it means that your service registry has been set up.

Summary

We used to have a blog about how to install zookeeper on Linux [install Zookeeper on Linux and some precautions], but there is no such problem for Eureka, because the service registry in Eureka is actually a Spring Boot project, and we know that the Spring Boot project can be directly typed into a jar package, and then the java-jar command can be run in the same way on both Windows and Linux.

Register a service provider

OK, now that we have a service registry, we can consider registering a service provider with this service registry.

Create a new Spring Boot project

Or create a Spring Boot project. This creation is one more step than the previous creation. Select the starter of web when creating, and let's create a web project. Select web when you create it in IntelliJ IDEA, as follows:

Add Eureka dependency

In the created project, we need to add Eureka dependencies as follows:

4.0.0 org.sang provider 0.0.1-SNAPSHOT jar provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-dependencies Dalston.SR3 Entry for pom import org.springframework.boot spring-boot-maven-plugin to create an application

This is a web project, so we add a Controller and provide an access entry in the Controller, as follows:

@ RestControllerpublic class HelloController {private final Logger logger = Logger.getLogger (getClass ()); @ Autowired private DiscoveryClient client; @ RequestMapping (value = "/ hello", method = RequestMethod.GET) public String index () {List instances = client.getInstances ("hello-service"); for (int I = 0; I < instances.size () Logger.info ("/ hello,host:" + instances.get (I). GetHost () + ", service_id:" + instances.get (I). GetServiceId ());} return "Hello World";}}

After the service is created here, the service-related information is printed out in the log.

Activate DiscoveryClient in Eureka

At the entry function of Spring Boot, activate the DiscoveryClient implementation in Eureka by adding the @ EnableDiscoveryClient annotation (because we injected DiscoveryClient into HelloController).

@ EnableDiscoveryClient@SpringBootApplicationpublic class ProviderApplication {public static void main (String [] args) {SpringApplication.run (ProviderApplication.class, args);}} configure the service name and registry address

Finally, we can configure the service name and registry address in the application.properties file, as follows:

Spring.application.name=hello-serviceeureka.client.service-url.defaultZone= http://localhost:1111/eureka

The meaning of these two lines of code is very simple, so I won't say any more.

test

After we have done all this, we can test it and run the Spring Boot project directly. After running successfully, we refresh the http://localhost:1111 and we can see that a service has been successfully registered. As follows:

At the same time, if we look at the running log of the service provider, we can also see the information of the service, as follows:

OK, after that, one of our service registries was successfully built, and a service provider was successfully registered. But there is a small problem, that is, we are a single-node service registry, once a failure occurs, the whole service will be paralyzed, so in practical application, we need to build a high-availability registry.

After reading the above, do you know how to use Spring Cloud to build a service registry? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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