SpringCloud Article 1: Service Registration and Discovery (Eureka) and springcloudeureka

Source: Internet
Author: User

SpringCloud Article 1: Service Registration and Discovery (Eureka) and springcloudeureka
Preface:

Learn Basic SpringBoot knowledge

Introduction:

Spring cloud provides developers with some tools to quickly build distributed systems, including configuration management, service discovery, circuit breaker, routing, micro-proxy, event bus, global lock, decision election, distributed session, and so on. It runs in a simple environment and can be run on a developer's computer.

Tools:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2017.3 x64

I. Service Registration Center 1. 0 starts a service registration center, and only requires an annotation @ 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 backend cache. After each instance is registered, it needs to send a heartbeat to the Registration Center (so it can be completed in memory ), by default, the erureka server is also an eureka client. You must specify a server. The configuration file appication. yml of eureka server:
spring:  application:      name: eureka-server  profiles:    active: ${@profileActiv@:dev}
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"><modelVersion>4.0.0</modelVersion><groupId>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>
Ii. Service Provider (eureka client) 
 
When the client registers with the server, it provides metadata such as host and port, URL, and homepage. The Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually deleted from the registered 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: ${@profileActiv@:dev}
 
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>    <packaging>jar</packaging>    <name>eureka-client</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.0.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>Finchley.M9</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.projectlombok</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>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>    <repositories>        <repository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositories></project>
2.5 obtain 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@ConfigurationProperties(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@RequestMapping("/eureka")public class EurekaClientController {    @Autowired    private EurekaClientConfig eurekaClientConfig;    @GetMapping("/client")    public String home(@RequestParam String name) {        return "hi " + name + ", i am from port:" + eurekaClientConfig.getPort();    }}
 

Eureka-server
Http: // localhost: 8761

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

 

Tag

1-0, 1-1

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

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.