Introduction to eureka for Spring Cloud

Original Link: https://blog.csdn.net/boling_cavalry/article/details/81809929

Full-text overview

This chapter is to prepare for further study, which mainly contains the following two parts:
1. Develop three simple applications, including registration centers, service providers and service consumers. Follow-up research and actual operations are based on these three applications.
2. Download the source code used in subsequent analysis, including SpringCloud and Netflix, with versions consistent with the three simple applications;

Version List

  1. JDK: 1.8;
  2. spring boot: 1.5.9.RELEASE;
  3. spring cloud: Edgware.RELEASE;

Introduction to Applications

A list of three applications to be developed in this chapter:

apply name Effect Occupy Port
springclouddeepeureka Registration Center 8081
springclouddeepprovider service provider 8082
springclouddeepconsumer Service consumer 8083

Actual Source Download

You can also download the source code for these three applications from github, with the address and link information shown in the following table:

Name link Remarks
Project Home https://github.com/zq2599/blog_demos The project's home page on GitHub
git repository address (https) https://github.com/zq2599/blog_demos.git Warehouse address for the project source code, https protocol
git warehouse address (ssh) git@github.com:zq2599/blog_demos.git Warehouse address for the project source code, ssh protocol

There are several folders in this git project. The source code for this chapter is shown in the following red box under the three folders: springclouddeepeureka, springclouddeepprovider, and springclouddeepconsumer:

Now prepare the demo project, starting with Eureka:

Eureka application

  1. Create a springclouddeepeureka web application based on maven boot, 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.bolingcavalry</groupId>
    <artifactId>springclouddeepeureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springclouddeepeureka</name>
    <description>Demo project for Eureka server</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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>
    </properties>

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

    <!--spring cloud Dependency Management-->
    <!-- Introduce spring cloud Dependency on -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62


2. Create a configuration file, application.yml, in the resources directory, as follows:

server:
  port: 8081
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


3. In the startup class SpringclouddeepeurekaApplication.java, add the comment @EnableEurekaServer:

package com.bolingcavalry.springclouddeepeureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringclouddeepeurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringclouddeepeurekaApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14


4. Start the application, browser access port 8081, Eureka page can be seen as follows:


5. Up to this point, the registry has been successfully started;

Service Provider

  1. Create a web application springclouddeepprovider based on maven boot, 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.bolingcavalry</groupId>
    <artifactId>springclouddeepprovider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springclouddeepprovider</name>
    <description>Demo project for Spring Cloud service provider</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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-client</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>Edgware.RELEASE</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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65


2. Create a configuration file, application.yml, in the resources directory, as follows:

server:
  port: 8082
spring:
  application:
    name: springcloud-deep-provider
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
  instance:
    prefer-ip-address: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11


3. In the startup class SpringclouddeepproviderApplication.java, add the comment @EnableDiscoveryClient:

package com.bolingcavalry.springclouddeepprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringclouddeepproviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringclouddeepproviderApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15


4. Create a Controller class to provide http services:

package com.bolingcavalry.springclouddeepprovider.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description : Provide normal http services
 * @Author : za2599@gmail.com
 * @Date : 2018-08-18 18:25
 */
@RestController
public class HelloService {

    @GetMapping("hello/{name}")
    public String hello(@PathVariable String name){
        return "Hello " + name + ", " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22


5. Start the application and browser access path: http://localhost:8082/hello/123 , you can receive a server-side response as follows:


6. Go to refresh Eureka's page at this time and find that the service has been successfully registered, as shown below:


At this point, the service provider application development is complete, and the last one is the service consumer springclouddeepconsumer.

Service consumer

  1. Create a springclouddeepconsumer web application based on maven boot, 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.bolingcavalry</groupId>
    <artifactId>springclouddeepconsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springclouddeepconsumer</name>
    <description>Demo project for Spring Cloud service consumer</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55


2. Create a configuration file, application.yml, in the resources directory, as follows:

server:
  port: 8083
spring:
  application:
    name: springcloud-deep-consumer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
  instance:
    prefer-ip-address: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11


3. In the startup class SpringclouddeepconsumerApplication.java, add the comment @EnableDiscoveryClient:

package com.bolingcavalry.springclouddeepconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringclouddeepconsumerApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringclouddeepconsumerApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23


4. Create a Controller class to provide http services:

package com.bolingcavalry.springclouddeepconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Description : Remote Call Test
 * @Author : qin_zhao@kingdee.com
 * @Date : 2018-08-18 19:10
 */
@RestController
public class ConsumerServiceController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("serviceinfo")
    public String serviceinfo(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("springcloud-deep-provider");
        return null==serviceInstance ? "service not found" : serviceInstance.toString();
    }

    @GetMapping("consume/{name}")
    public String consume(@PathVariable String name){
               return this.restTemplate.getForObject("http://springcloud-deep-provider/hello/" + name, String.class);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35


5. Start the application and browser access path: http://localhost:8083/consume/tom , you can receive a server-side response as follows:


6. Try again the interface for getting service information: http://localhost:8083/serviceinfo , you can see the service information:


7. Go to refresh Eureka's page at this time and find that both applications have been registered successfully, as shown below:


Up to this point, the service consumer applications have been developed. In the following chapters, our analysis and actual operations are carried out on these three applications.

Spring Cloud Source Download

Simply say how to download the corresponding version of the Spring Cloud source in GitHub:
1. Spring Cloud's GitHub home page: https://github.com/spring-cloud
2. Assume that I am downloading the source code for the subproject spring-cloud-commons, version 1.3.0-RELEASE, click the link in the red box below:


3. On the spring-cloud-commons project home page, click the "releases" link in the red box below:


4. As shown below, there are several release s available. Select the version you want and click on the link in the red box to download the source code:

Netflix Source Download

Netflix source address: https://github.com/Netflix , in the following chapters, we will download the corresponding release version source here as needed;

Now that we are ready, let's explore the wonderful world of Spring Cloud.

Tags: Spring Maven Java github

Posted on Fri, 06 Sep 2019 19:38:12 -0700 by erisande