Spring Cloud Learning (i) registration and discovery of services (Eureka)

Source: Internet
Author: User

1 Creating a Service registration center

Here, the component I need to use is spring Cloud Netflix Eureka, Eureka is a service registration and Discovery module.

1.1 Creating the Model project as a service registration center Eureka-server

Project structure

Pom.xml

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.seawaterbt.cloud</groupId> <artifactid>eureka-server</ artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>e Ureka-server</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.13.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &LT;PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-c loud.version>edgware.sr4</spring-cloud.version> </properties> <dependencies> <depende Ncy> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-star ter-eureka-server</artifactid> </dependency> <dependency> <groupid>org.sp Ringframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>            </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> &LT;ARTIFACTID&G                T;spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> &LT;BUILD&G        T                <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

1.2 Start the Service registration center Eureka-server

Springboot Startup class

 PackageCom.seawaterbt.cloud.eurekaserver;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.netflix.eureka.server.enableeurekaserver;@ Springbootapplication@enableeurekaserver Public classeurekaserverapplication { Public Static voidMain (string[] args) {Springapplication.run (eurekaserverapplication.class, args); }}

Application.yml

Server:   8761  Context-path:/eureka-Servereureka:  instance:    hostname:localhost  Client:     false      serviceurl: defaultzone:http://      ${eureka.instance.hostname}:${server.port}/eureka-server/

by Eureka.client.registerWithEureka:false and Fetchregistry:false to show that you are a Eureka server.

Start the project, open the browser access: http://localhost:8761/eureka-server/, the interface is as follows:

No application available no service was found ... ^_^ because there is no registration service, it is not possible to have services found.

2 Creating a service provider Eureka-client1.1 CreateModelEngineering as a service providereureka-client

Project structure

  

Pom.xml

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.seawatebt.cloud</groupId> <artifactid>eureka-client</ artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>e Ureka-client</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.13.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &LT;PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-c loud.version>edgware.sr4</spring-cloud.version> </properties> <dependencies> <depende Ncy> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-star ter-eureka-server</artifactid> </dependency> <dependency> <groupid>org.sp Ringframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <depende Ncy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starte    r-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupid>org                .springframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> &LT;BUILD&G        T                <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
1.2 Creating a simple service interface

Directory structure

Infocontroller.java

 PackageCom.seawatebt.cloud.eurekaclient.controller;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classInfocontroller {@Value ("${server.port}")     PublicString Port; @GetMapping ({"/hi", "/hello"})     PublicString Hi (@RequestParam (value = "Name", defaultvalue = "SEAWATERBT") (String name) {return"Name=" +name+ "-----port=" +Port; }}

1.3Start a service providereureka-client

Configuring the Springboot Startup class

Eurekaclientapplication

 Packagecom.seawatebt.cloud.eurekaclient;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.netflix.eureka.EnableEurekaClient, @SpringBootApplication @enableeurekaclient Public classeurekaclientapplication { Public Static voidMain (string[] args) {Springapplication.run (eurekaclientapplication.class, args); }}

Application.yml

Server:   8762  Context-path:/eureka-client# service name Spring:  application:    name:server-  Hieureka:  client:    serviceurl:      defaultzone:http://localhost:8761/ eureka-server/eureka/

Spring.application.name, this is very important, this in the future between service and service calls are generally based on this name

To start the project, open http://localhost:8761/eureka-server/, which is the Eureka server URL:

You will find a service already registered in the service, the service name is Service-hi, the port is 8762

Visit Http://pc-20180321caba:8762/eureka-client/hi you will see on the browser:

3. References

69696915

Spring Cloud Learning (i) registration and discovery of services (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.