About Spring Cloud Eureka

Source: Internet
Author: User

First, let's try using spring Cloud Eureka to implement service governance.

Spring Cloud Eureka is the Service governance module under the spring Cloud Netflix project. The spring cloud Netflix project is one of Spring cloud's sub-projects, primarily packaged with Netflix's range of open source products, which provides a self-configuring Netflix OSS integration for spring boot applications. With some simple annotations, developers can quickly configure common modules in the application and build large, distributed systems. It mainly provides modules including: Service Discovery (Eureka), Circuit breakers (Hystrix), intelligent routing (Zuul), Client load Balancing (Ribbon), etc.

Here's a look at how to implement service governance with spring Cloud Eureka.

Create a Service registration center

Create a basic spring boot project, named eureka-server , and pom.xml introduce the required dependencies in:

12345678910111213141516171819202122232425 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.4.release</version> <relativepath/> </parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.sr1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>

@EnableEurekaServerStart a service registry with annotations to provide conversations for other apps. This step is very simple, just add this annotation to a normal spring boot application to enable this feature, such as the following example:

123456789 @EnableEurekaServer @SpringBootApplication public class application { public static void main(string[] args) { /c13>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 simply application.properties adding the following information to the configuration file:

123456 spring.application.name=eureka-serverserver.port=1001 eureka.instance.hostname= Localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false

To differentiate the service from subsequent registrations, the port for the service registry is set to 1001 via the Server.port property. After starting the project, visit: http://localhost:1001/, you can see the following page, which has not found any services.

Create a "service provider"

Below we create the client that provides the service, and register ourselves with the Service registration center. In this article we mainly introduce the registration and discovery of services, so we may try to provide an interface in the service provider to obtain all the current service information.

First, create a basic spring boot app. Named eureka-client , in pom.xml , add the following configuration:

1234567891011121314151617181920212223242526272829 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.4.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.sr1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>

Second, the implementation of the/DC request processing interface, through the Discoveryclient object, in the log to print out the relevant content of the service instance.

1234567891011121314 @RestController public class Dccontroller { @Autowireddiscoveryclient discoveryclient; @GetMapping ("/DC") Public String DC() {string services = "Services:" + discoveryclient.getservices (); System.out.println (services); return services;} }

Finally, by adding annotations to the main class @EnableDiscoveryClient , this annotation activates the discoveryclient implementation in the Eureka so that the output of the service information in the controller can be realized.

123456789 @EnableDiscoveryClient @SpringBootApplication public 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 application.properties do some configuration work, as follows:

123 spring.application.name=eureka-clientserver.port=2001eureka.client.serviceurl.defaultzone=http://localhost:1001/eureka/

By using spring.application.name properties, we can specify that the name of the microservices will follow the call and only use that name to access the service. eureka.client.serviceUrl.defaultZoneproperty corresponds to the configuration content of the service registry, specifying the location of the service registry. In order to test differentiated service providers and service registries on this computer, use server.port the properties to set different ports.

After starting the project, visit again: http://localhost:1001/. As content, the services we define are successfully registered.

Of course, we can also get the eureka-client current service manifest by directly accessing the interface provided by the service /dc , only need to access: HTTP://LOCALHOST:2001/DC, we can get the following output to return:

1 Services: [Eureka-client]

Among these, the square brackets are all the service manifests that are obtained in the implementation of the eureka-client Eureka through the interfaces defined by spring cloud DiscoveryClient . Since spring cloud has done a very good job of 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 Central.

From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. Source Source

About Spring Cloud Eureka

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.