写点什么

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

作者:程序员欣宸
  • 2022 年 7 月 04 日
  • 本文字数:6281 字

    阅读完需:约 21 分钟

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

欢迎访问我的 GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

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

全文概览

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


  1. 开发三个简单应用,包括:注册中心、服务提供方、服务消费方,后续研究和实战都在这三个应用基础上进行;

  2. 下载后续分析研究中用到的源码,包括 SpringCloud 和 Netflix 的,版本和三个简单应用的一致;

版本列表

  1. JDK:1.8;

  2. spring boot:1.5.9.RELEASE;

  3. spring cloud:Edgware.RELEASE;

应用简介

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



实战源码下载

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


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



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

Eureka 应用

  • 基于 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>
复制代码


  • resources 目录下创建配置文件 application.yml,内容如下:


server:  port: 8081eureka:  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://localhost:8081/eureka/
复制代码


  • 启动类 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@EnableEurekaServerpublic class SpringclouddeepeurekaApplication {
public static void main(String[] args) { SpringApplication.run(SpringclouddeepeurekaApplication.class, args); }}
复制代码


  • 启动应用,浏览器访问 8081 端口,可见 Eureka 页面如下图:



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

服务提供者

  • 基于 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>
复制代码


  • resources 目录下创建配置文件 application.yml,内容如下:


server:  port: 8082spring:  application:    name: springcloud-deep-providereureka:  client:    serviceUrl:      defaultZone: http://localhost:8081/eureka/  instance:    prefer-ip-address: true
复制代码


  • 启动类 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@EnableDiscoveryClientpublic class SpringclouddeepproviderApplication {
public static void main(String[] args) { SpringApplication.run(SpringclouddeepproviderApplication.class, args); }}
复制代码


  • 创建一个 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 */@RestControllerpublic 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()); }}
复制代码


  • 启动应用,浏览器访问路径:http://localhost:8082/hello/123 ,可收到 server 端响应如下图:



  • 此时去刷新 Eureka 的页面,发现服务已经注册成功了,如下图:



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

服务消费方

  • 基于 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>
复制代码


  • resources 目录下创建配置文件 application.yml,内容如下:


server:  port: 8083spring:  application:    name: springcloud-deep-consumereureka:  client:    serviceUrl:      defaultZone: http://localhost:8081/eureka/  instance:    prefer-ip-address: true
复制代码


  • 启动类 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@EnableDiscoveryClientpublic class SpringclouddeepconsumerApplication {
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
public static void main(String[] args) { SpringApplication.run(SpringclouddeepconsumerApplication.class, args); }}
复制代码


  • 创建一个 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 */@RestControllerpublic 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); }}
复制代码


  • 启动应用,浏览器访问路径:http://localhost:8083/consume/tom,可收到 server 端响应如下图:



  • 再试试获取服务信息的接口:http://localhost:8083/serviceinfo,可以看到服务信息:



  • 此时去刷新 Eureka 的页面,发现两个应用都已注册成功,如下图:



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

Spring Cloud 源码下载

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

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

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



  • 在 spring-cloud-commons 工程主页中,点击下图红框中的"releases"链接:



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


Netflix 源码下载

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

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

欢迎关注 InfoQ:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

发布于: 刚刚阅读数: 4
用户头像

搜索"程序员欣宸",一起畅游Java宇宙 2018.04.19 加入

前腾讯、前阿里员工,从事Java后台工作,对Docker和Kubernetes充满热爱,所有文章均为作者原创,个人Github:https://github.com/zq2599/blog_demos

评论

发布
暂无评论
Spring Cloud源码分析之Eureka篇第一章:准备工作_Java_程序员欣宸_InfoQ写作社区