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 introduce the required dependencies in the Pom.xml:

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

Start a service registry with @enableeurekaserver annotations to provide conversations to 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:

@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 attempt to register itself as a client, so we need to disable its client registration behavior by simply 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

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:

  <parent><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-parent</artifactid><version>1.5.4.release</version><relativepath/> <!--lookup parent from repository--></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.

@RestControllerpublic class DcController {@AutowiredDiscoveryClient discoveryClient;@GetMapping("/dc")public String dc() {String services = "Services: " + discoveryClient.getServices();System.out.println(services);return services;}}

Finally, by adding @enablediscoveryclient annotations to the main class, this annotation activates the discoveryclient implementation in the Eureka so that the output of the service information in the 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 for Application.properties, as follows:

spring.application.name=eureka-clientserver.port=2001eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

With the Spring.application.name property, we can specify that the name of the microservices will follow the call and only use that name to access the service. The Eureka.client.serviceUrl.defaultZone property 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 the Server.port property to set different ports.
Among these, the eureka-client in square brackets is the list of all services that are obtained in the implementation of Eureka through the Discoveryclient interface defined by spring cloud. 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.

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.