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

举报
程序员欣宸 发表于 2022/07/04 07:03:13 2022/07/04
【摘要】 大家好,从本章开始我们一起进入SpringCloud的源码世界,通过源码分析再结合实战,一起加深对SpringCloud体系的认识

欢迎访问我的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;

应用简介

  • 用列表简介本章要开发的三个应用:
应用名称 作用 占用端口
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这三个文件夹下,如下图红框所示:

image.png

  • 接下来准备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: 8081
eureka:
  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
@EnableEurekaServer
public class SpringclouddeepeurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringclouddeepeurekaApplication.class, args);
	}
}
  • 启动应用,浏览器访问8081端口,可见Eureka页面如下图:

image.png

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

服务提供者

  • 基于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: 8082
spring:
  application:
    name: springcloud-deep-provider
eureka:
  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
@EnableDiscoveryClient
public 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
 */
@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());
    }
}

image.png

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

image.png

  • 至此,服务提供方应用开发完毕,最后一个是服务消费方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: 8083
spring:
  application:
    name: springcloud-deep-consumer
eureka:
  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
@EnableDiscoveryClient
public 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
 */
@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);
    }
}

image.png

image.png

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

image.png

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

Spring Cloud源码下载

  • 简单说说如何在GitHub下载对应版本的Spring Cloud源码:
  • Spring Cloud的GitHub主页:https://github.com/spring-cloud;
  • 假设我要下载的是子工程spring-cloud-commons的源码,版本号1.3.0-RELEASE,点击下图红框中的链接:

image.png

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

image.png

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

image.png

Netflix源码下载

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

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

欢迎关注华为云博客:程序员欣宸

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

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。