Spring Cloud One: Service registration and Discovery (Eureka) "Version Dalston"

Source: Internet
Author: User

About Spring Cloud

Spring Cloud is a spring boot-based cloud application development tool for configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control buses, global locks, decision-making campaigns, Operations such as distributed sessions and cluster state management provide a simple way to develop.

Micro-Service Architecture

So what is a "microservices architecture"? To put it simply, a microservices architecture is to split a complete application from the data store vertically into multiple different services, each of which can be deployed independently, independently maintained, independently scaled, and invoked between services and services through, for example, restful APIs.

Service Governance Spring Cloud Eureka

Spring Cloud Eureka is the Service governance module under the spring Cloud Netflix project. And the spring cloud Netflix project is one of Spring cloud's sub-projects

It mainly provides modules including: Service Discovery (Eureka), Circuit breakers (Hystrix), intelligent routing (Zuul), Client load Balancing (Ribbon), etc.

First, create a "service registration center"

Create a basic spring boot project, namedeureka-server pom.xml

    <parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-sta rter-parent</artifactid> <version>1.5.4.RELEASE</version> <relativepath/> </p arent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid        > <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid&gt            ; <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies&gt    ; <dependencyManagement> <dependencies> <dependency> <groupid>org.s              Pringframework.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.

@EnableEurekaServer @springbootapplication  Public class appleapplication {    publicstaticvoid  main (string[] args) {         New Springapplicationbuilder (appleapplication.  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,

Just add the application.properties following information to the configuration file:

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

After starting the project, visit: HTTP://LOCALHOST:1001/can see the following page, which has not found any services.

Second, create "service provider"

Below we create the client that provides the service and register ourselves with the Service registration center

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-sta rter-parent</artifactid> <version>1.5.4.RELEASE</version> <relativepath/> </p arent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupi d> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <de Pendency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-s tarter-web</artifactid> </dependency> </dependencies> <dependencyManagement> &                Lt;dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.sr1&lt           ;/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.

@RestController  Public class Computecontroller {    @Autowired    discoveryclient discoveryclient;    @GetMapping ("/DC")    public  String DC () {        = "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.

@EnableDiscoveryClient @springbootapplication  Public class appleapplication {    publicstaticvoid  main (string[] args) {         New Springapplicationbuilder (                appleapplication. 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:

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

by spring.application.name using 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.

Once the project is started, visit: http://localhost:1001

Can be as content, our defined services are successfully registered

Of course, we can also get the eureka-client /dc current list of services through direct access to the interfaces provided by the service.

Just need access: HTTP://LOCALHOST:2001/DC, we can get the following output back:

Spring Cloud One: Service registration and Discovery (Eureka) "Version Dalston"

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.