SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version

A, spring cloud Introduction
In view of "the history of the easiest Spring Cloud Tutorial" is very popular with readers again I tend to upgrade a bit versions currently supported version of the Spring Boot version 2.0.3.RELEASE, Spring Cloud version Finchley.RELEASE.

Finchley official version of the document as follows
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud provides a number of tools to quickly build distributed systems including configuration management for developers, service discovery, circuit breakers, routing, micro broker, event bus, global lock, decision-making campaign, distributed session and so on. It runs a simple environment can run on a PC developers. Otherwise spring cloud is based on the need to develop in so springboot to springboot have a certain understanding, if not understand, you can see this article two hours to learn springboot. In addition to not understand the word "micro Services Architecture" can be understood by the search engine under "micro-services architecture."

Second, create a registry service
where I was registered as a service using Eureka and components found As will be out after the Consul article details.

2.1 First create a maven main project.

First, create a Maven project introduced primary reliance spring Boot version 2.0.3.RELEASESpring Cloud version Finchley.RELEASE in its pom file. Learn springcloud architecture can be added to beg 3536247259 This pom pom file as the parent file acts as dependent on other version control module works inherit the pom. This series of articles in all pom other articles of this model with the pom same. Again it is not repeated introduction. code show as below

<?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.forezp</groupId>
    <artifactId>sc-f-chapter1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>sc-f-chapter1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>eureka-server</module>
        <module>service-hi</module>
    </modules>

    <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.RELEASE</spring-cloud.version>
    </properties>

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

2.2 Then create two model projects: ** a model project registered as a service center that is Eureka Server, and the other as Eureka Client.

Here is an example to create detailed instructions for creating server process

Right Project -> Create model-> select spring initialir following figure SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version
next step -> select cloud discovery-> eureka server, and then been next on the list.
SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version
After the creation of the project pom.xml inherits its parent pom file and the introduction of spring-cloud-starter-netflix- eureka-server -dependent code is as follows

<?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.forezp</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>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

2.3 Starting a service registry requires only a comment @EnableEurekaServer need to add this comment on the start of the project application class springboot

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }
}

2.4 eureka is a component that is not highly available backside cache needs to send a heartbeat every instance erureka server can be also completed a eureka client default, a server must be specified in the memory after the registration to the registry. eureka server configuration file appication.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

By eureka.client.registerWithEurekafalse and fetchRegistryfalse to show that he is a eureka server.

2.5 eureka server is the interface to start the project, open the browser to access
HTTP: // localhost: 8761 , the following interface
SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version
No application available to be found there is no service ...... _
because there is no registration service of course there can be no services were found.

Third, create a service provider (eureka client)
when the client registers with the server it will provide some metadata such as the host and port URL home page. Eureka server receiving heartbeat messages from each client instance. If the heartbeat time-out is usually the instance is deleted from the registration server.

Create a similar process with the server, create the pom.xml as follows

<?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.forezp</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceHiApplication.class, args );
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

It is not enough merely @EnableEurekaClient also need to specify their own service centers registered in the configuration file the following address application.yml profile

server:
  port: 8762

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Need to specify spring.application.name, this is very important in this call each other after the service and between services are generally based on this name.
Start Project Open http: // localhost: 8761 That eureka server URL
SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version
SpringCloud distributed micro-cloud infrastructure services First: service registration and discovery Eureka (Finchley version
you will find that a service has been registered in the service of the service named SERVICE-HI, port 7862

Then open http: // localhost:? 8762 / hi name = forezp you will see in the browser:

hi forezp,i am from port:8762

Guess you like

Origin blog.51cto.com/14622290/2457816