In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to register and discover Eureka in Spring cloud, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Introduction to Spring Cloud
Spring Cloud is a cloud application development tool based on Spring Boot, which provides a simple development method for configuration management, service discovery, circuit breaker, intelligent routing, micro-agent, control bus, global lock, decision campaign, distributed session and cluster state management involved in JVM-based cloud application development.
Spring Cloud contains multiple subprojects (for different open source products involved in distributed systems), such as: Spring Cloud Config, Spring Cloud Netflix, Spring Cloud0 CloudFoundry, Spring Cloud AWS, Spring Cloud Security, Spring Cloud Commons, Spring Cloud Zookeeper, Spring Cloud CLI and so on.
Spring Cloud Eureka
First, let's try to implement service governance using Spring Cloud Eureka.
Spring Cloud Eureka is a service governance module under the Spring Cloud Netflix project. The Spring Cloud Netflix project is one of the sub-projects of Spring Cloud, which mainly focuses on the packaging of a series of open source products of Netflix, which provides self-configured Netflix OSS integration for Spring Boot applications. With some simple annotations, developers can quickly configure common modules in applications and build huge distributed systems. Its main modules include: service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent routing (Zuul), client load balancing (Ribbon) and so on.
Next, let's take a specific look at how to implement service governance using Spring Cloud Eureka.
Create a Service Registry
Create a basic Spring Boot project, named eureka-server, and introduce the required dependencies in pom.xml:
Org.springframework.bootspring-boot-starter-parent1.5.4.RELEASEorg.springframework.cloudspring-cloud-starter-eureka-server org.springframework.cloudspring-cloud- dependencies Dalston.SR1 pom import
Start a service registry through the @ EnableEurekaServer annotation to provide conversations to other applications. This step is very simple. You only need to add this annotation to a normal Spring Boot application to enable this feature, such as the following example:
@ EnableEurekaServer@SpringBootApplicationpublic class Application {public static void main (String [] args) {new SpringApplicationBuilder (Application.class) .web (true) .run (args);}}
By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by adding the following information to the application.properties configuration file:
Spring.application.name=eureka-serverserver.port=1001eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false
In order to distinguish from the services to be registered later, the port of the service registry is set to 1001 through the server.port property. After starting the project, visit: http://localhost:1001/, you can see the following page, where no services have been found.
Create a Service provider
Let's create a client that provides the service and register ourselves with the service registry. In this article, we mainly introduce the registration and discovery of services, so we might as well try to provide an interface in the service provider to obtain all the current service information.
First, create a basic Spring Boot application. Name it eureka-client, and in pom.xml, add the following configuration:
Org.springframework.bootspring-boot-starter-parent1.5.4.RELEASE org.springframework.cloudspring-cloud-starter-eurekaorg.springframework.bootspring-boot-starter-web org.springframework.cloudspring-cloud- dependencies Dalston.SR1 pom import
Secondly, the / dc request processing interface is implemented to print out the relevant contents of the service instance in the log through the DiscoveryClient object.
@ RestControllerpublic class DcController {@ AutowiredDiscoveryClient discoveryClient;@GetMapping ("/ dc") public String dc () {String services = "Services:" + discoveryClient.getServices (); System.out.println (services); return services;}}
Finally, by adding the @ EnableDiscoveryClient annotation to the application main class, the annotation can activate the DiscoveryClient implementation in Eureka, so that the output of service information in Controller can be realized.
@ EnableDiscoveryClient@SpringBootApplicationpublic class Application {public static void main (String [] args) {new SpringApplicationBuilder (ComputeServiceApplication.class) .web (true) .run (args);}}
After we have completed the implementation of the service content, we will continue to do some configuration work on application.properties, as follows
Spring.application.name=eureka-clientserver.port=2001eureka.client.serviceUrl.defaultZone= http://localhost:1001/eureka/
Through the spring.application.name attribute, we can specify the name of the microservice and then use that name to access the service when invoked. The eureka.client.serviceUrl.defaultZone attribute specifies the location of the service registry, corresponding to the configuration content of the service registry. To test the DiffServ provider and service registry on the local machine, use the server.port property to set different ports.
After starting the project, visit http://localhost:1001/ again. As shown in the figure below, the service we defined has been successfully registered.
Of course, we can also get the current service list by directly accessing the / dc interface provided by the eureka-client service, as long as we visit: http://localhost:2001/dc, we can get the following output:
Services: [eureka-client]
Where eureka-client in square brackets is the list of all services obtained in the implementation of eureka through the DiscoveryClient interface defined by Spring Cloud. Because Spring Cloud does a very good abstraction in the service discovery layer, for the above program, we can seamlessly switch from eureka's service governance system to consul's service governance system.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.