Springcloud First: Registration and discovery of services (Eureka)

Source: Internet
Author: User

Objective:

Must learn to springboot basic knowledge

Brief introduction:

Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-proxies, event busses, global locks, decision-making campaigns, distributed sessions, and more. It runs in a simple environment and can run on a developer's computer.

Tools:

JDK8

apache-maven-3.5.2

IntelliJ Idea 2017.3 x64

First, the Service Registration center 1. 0 Starting a service registry requires only one note @enableeurekaserver
Package Com.lwc;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;/** * @author Eddie */@ Enableeurekaserver@springbootapplicationpublic class Eurekaserverapplication {public    static void Main (string[] args) {        springapplication.run (eurekaserverapplication.class, args);}    }
1.1 Eureka is a highly available component that does not have a back-end cache, each instance is registered to send a heartbeat to the registry (so it can be done in memory), and by default Erureka server is also a Eureka client, and you must specify a server. Eureka Server configuration file Appication.yml:
Spring:  application:      name:eureka-server  Profiles:    active: ${@[email protected]:d EV}
1.2 Application-dev.yml
Server:  Port:8761eureka:  instance:    hostname:localhost  client:    registerwitheureka:false    fetchregistry:false    serviceurl:      defaultzone:http://${eureka.instance.hostname}:${server.port}/ eureka/
1.3 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 "&GT;&LT;MODELVERSION&GT;4.0.0&LT;/MODELVERSION&GT;&LT;GROUPID&GT;COM.LWC </groupid><artifactid>eureka-server</artifactid><version>0.0.1-snapshot</version ><packaging>jar</packaging><name>eureka-server</name><description>demo Project For Spring boot</description><parent><groupid>org.springframework.boot</groupid>< artifactid>spring-boot-starter-parent</artifactid><version>1.5.10.release</version>< Relativepath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr2</ Spring-cloud.version></properties><dependencies><dependency><groupid> Org.springframework.cloud</groupid><artifactid>spring-cloud-starter-eureka-server</artifactid ></dependency><dependency><groupid>org.springframework.boot</groupid><artifactid >spring-boot-starter-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><build><plugins>< Plugin><groupid>org.springframework.boot</groupid><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>< /project>
Second, the service provider (Eureka client)
When the client registers with the server, it provides some metadata, such as the host and port, the URL, the home page, and so on. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is typically removed from the registration server.
2.1 Annotation @enableeurekaclient
Package Com.lwc;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.enableeurekaclient;/** * @author Eddie */@ Enableeurekaclient@springbootapplicationpublic class Eurekaclientapplication {public    static void Main (string[] args) {        springapplication.run (eurekaclientapplication.class, args);}    }
The 2.2 application.yml configuration file is as follows:
Spring:  application:      name:eureka-client  Profiles:    active: ${@[email protected]:d EV}
The 2.3 application-dev.yml configuration file is as follows:
Eureka:  Client:    serviceurl:      defaultzone:http://localhost:8761/eureka/server:  port:8762
2.4 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.lwc</groupid > <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packagi Ng>jar</packaging> <name>eureka-client</name> <description>demo Project for Spring BOOT&L t;/description> <parent> <groupId>org.springframework.boot</groupId> <artifactid& Gt;spring-boot-starter-parent</artifactid> <version>2.0.0.RELEASE</version> <relativepa Th/> <!--lookup parent from repository to </parent> <properties> <project.build.sou Rceencoding>utf-8</project.build.sOurceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> &lt ;java.version>1.8</java.version> <spring-cloud.version>Finchley.M9</spring-cloud.version> & lt;/properties> <dependencies> <dependency> <groupid>org.springframework.boot&lt ;/groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> &L T;dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-c Loud-starter-netflix-eureka-server</artifactid> </dependency> <dependency> < Groupid>org.springframework.boot</groupid> &LT;ARTIFACTID&GT;SPRING-BOOT-STARTER-TEST&LT;/ARTIFACTID&G            T <scope>test</scope> </dependency> <dependency> <groupid>org.project lombok</groupid> <artifactId>lombok</artifactId> </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> </dependencymanagemen t> <build> <plugins> <plugin> <groupid>org.springframework. Boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> &LT;/PLUGIN&G        T </plugins> </build> <repositories> <repository> <id>spring-milestone  S</id>          <name>spring milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </r Epository> </repositories></project>
2.5 Get Port
Package Com.lwc.config;import Lombok. Data;import Org.springframework.boot.context.properties.configurationproperties;import org.springframework.stereotype.component;/** * @author Eddie.lee * @Package com.lwc.config * @ClassName Eurekaclientconfig * @description * @date created in 2018-03-26 17:16 * @modified by * * @Data @component@configurationproper Ties (prefix = "Server") public class Eurekaclientconfig {    private String port;}
2.6 Control Layer
Package Com.lwc.controller;import Com.lwc.config.eurekaclientconfig;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.getmapping;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.restcontroller;/** * @author Eddie.lee * @Package Com.lwc.controller * @ ClassName Eurekaclientcontroller * @description * @date created in 2018-03-26 17:19 * @modified by * * @RestController @reque    Stmapping ("/eureka") public class Eurekaclientcontroller {@Autowired private eurekaclientconfig eurekaclientconfig; @GetMapping ("/client") Public String Home (@RequestParam String name) {return ' Hi ' + name + ', I am from por    T: "+ eurekaclientconfig.getport (); }}

Eureka-server
http://localhost:8761

Eureka-client
Http://localhost:8762/eureka/client?name=eddie

Label

1-0, 1-1

SOURCE download
Https://github.com/eddie-code/SpringCloudDemo#springclouddemo

Springcloud First: 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.