Spring Cloud源码分析之Eureka篇第一章:准备工作

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

大家好,从本章开始我们一起进入SpringCloud的源码世界,通过源码分析再结合实战,一起加深对SpringCloud体系的认识;

原文链接:https://blog.csdn.net/boling_cavalry/article/details/81809929

全文概览

本章是为后续的深度学习做准备工作,主要包含以下两部分:

  1. 开发三个简单应用,包括:注册中心、服务提供方、服务消费方,后续研究和实战都在这三个应用基础上进行;
  2. 下载后续分析研究中用到的源码,包括SpringCloud和Netflix的,版本和三个简单应用的一致;

版本列表

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

应用简介

用列表简介本章要开发的三个应用:

应用名称 作用 占用端口
springclouddeepeureka 注册中心 8081
springclouddeepprovider 服务提供方 8082
springclouddeepconsumer 服务消费方 8083

实战源码下载

稍后会详细介绍这三个应用的开发和测试步骤,您也可以在github下载这三个应用的源码,地址和链接信息如下表所示:

名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议

这个git项目中有多个文件夹,本章源码分别在springclouddeepeureka、springclouddeepprovider、springclouddeepconsumer这三个文件夹下,如下图红框所示:
20191102100797\_1.png

接下来准备demo工程吧,先从Eureka开始:

Eureka应用

  1. 基于maven创建一个spring boot的web应用springclouddeepeureka,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.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依赖管理-->
        <!-- 引入spring cloud的依赖 -->
        <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. resources目录下创建配置文件application.yml,内容如下:
    server:
      port: 8081
    eureka:
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://localhost:8081/eureka/
  1. 启动类SpringclouddeepeurekaApplication.java中,添加注解@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. 启动应用,浏览器访问8081端口,可见Eureka页面如下图:
    20191102100797\_2.png

  2. 至此,注册中心启动成功;

服务提供者

  1. 基于maven创建一个spring boot的web应用springclouddeepprovider,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.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. resources目录下创建配置文件application.yml,内容如下:
    server:
      port: 8082
    spring:
      application:
        name: springcloud-deep-provider
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8081/eureka/
      instance:
        prefer-ip-address: true
  1. 启动类SpringclouddeepproviderApplication.java中,添加注解@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. 创建一个Controller类,用于提供http服务:
    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 : 提供普通的http服务 * @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. 启动应用,浏览器访问路径:http://localhost:8082/hello/123,可收到server端响应如下图:
    20191102100797\_3.png

  2. 此时去刷新Eureka的页面,发现服务已经注册成功了,如下图:
    20191102100797\_4.png

至此,服务提供方应用开发完毕,最后一个是服务消费方springclouddeepconsumer;

服务消费方

  1. 基于maven创建一个spring boot的web应用springclouddeepconsumer,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.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. resources目录下创建配置文件application.yml,内容如下:
    server:
      port: 8083
    spring:
      application:
        name: springcloud-deep-consumer
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8081/eureka/
      instance:
        prefer-ip-address: true
  1. 启动类SpringclouddeepconsumerApplication.java中,添加注解@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. 创建一个Controller类,用于提供http服务:
    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 : 远程调用测试的 * @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. 启动应用,浏览器访问路径:http://localhost:8083/consume/tom,可收到server端响应如下图:
    20191102100797\_5.png

  2. 再试试获取服务信息的接口:http://localhost:8083/serviceinfo,可以看到服务信息:
    20191102100797\_6.png

  3. 此时去刷新Eureka的页面,发现两个应用都已注册成功,如下图:
    20191102100797\_7.png

至此,服务消费方应用开发完毕,后续的章节中,我们的分析和实战都在这三个应用上进行;

Spring Cloud源码下载

简单说说如何在GitHub下载对应版本的Spring Cloud源码:

  1. Spring Cloud的GitHub主页:https://github.com/spring-cloud

  2. 假设我要下载的是子工程spring-cloud-commons的源码,版本号1.3.0-RELEASE,点击下图红框中的链接:
    20191102100797\_8.png

  3. 在spring-cloud-commons工程主页中,点击下图红框中的”releases”链接:
    20191102100797\_9.png

  4. 如下图,这里有多个release版本,挑选您所需的版本,点击红框中的链接即可下载源码:
    20191102100797\_10.png

Netflix源码下载

Netflix源码地址:https://github.com/Netflix,后面的章节中,我们会按需要来这里下载对应的release版本源码;

至此,咱们的准备工作已经完成,接下来一起去探索精彩的Spring Cloud世界吧;


来源:http://ddrv.cn

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring Cloud源码分析之Eureka篇第一章:准备工作

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏