1 Spring Cloud简介
Spring Cloud(官网:https://spring.io/projects/spring-cloud)是Spring Framework全家桶的一员,主要是为开发人员提供了快速构建分布式系统中一些常见的工具集合(例如配置管理、服务发现、熔断器、智能路由、微代理、控制总线、一次性令牌、全局锁、决策竞选、分布式会话、集群状态),Spring Cloud基于Spring Boot,所以学习之前要先熟悉Spring Boot的知识。对于分布式和微服务相关架构概念,如果有不了解的地方,建议去baidu一下,先熟悉一下相关概念。
目前为止,Spring Cloud的子项目也有很多了,包括像Spring Cloud Netflix、Spring Cloud Alibaba、Spring Cloud Azure等等,不同子项目下使用了不同的组件,有的也使用了一些相同组件,虽然框架选用组件不同,但是学会一个,其它同类型组件也会触类旁通,这里先学习一下Spring Cloud Netflix相关体系组件。
Spring Cloud Netflix体系包含了上述工具集合的一些开源实现,包括服务注册中心Eureka,客户端负载均衡组件Ribbon,熔断器Hystrix,熔断器监控Hystrix Dashboard,网关组件ZUUL等,能够实现服务注册与发现、负载均衡、熔断器及路由等功能,以上也统称为Netflix OSS(Open Source),是由Netflix公司主持开发的一套代码框架和库。
由于Spring Cloud和Spring Boot有严格的版本对应关系,这里我们Spring Cloud版本选用Greenwich版(G版),对应Spring Boot版本为2.1.13
集成开发环境:IDEA,Maven3.6.3,JDK1.8
2 Eureka简介
Spring Cloud Netflix体系选用Eureka作为服务中心,服务的注册和发现都需要经过Eureka,包括Eureka Server和Eureka Client。
- Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(就是微服务中的客户端和服务端)
- Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)
我们可以使用声明式Java配置直接创建内置Eureka Server,在开发环境用起来比较方便。
3 构建Maven项目
由于Spring Cloud内容较多,我们构建模块化的项目集合。首先建立根项目,新建Maven Project,填入项目名称、地址及Maven配置
删除src等无用文件,保留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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>org.dothwinds</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0.0</version>
<properties>
<jdk.version>1.8</jdk.version>
<spring-cloud.version>Greenwich.SR5</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>
4 构建Eureka Server
继续新建Eureka Server模块,我们访问https://start.spring.io,手动构建一个Spring Boot模块下载到本地,当然在IDEA里通过Spring Initializr创建也可以,创建好之后引入到项目中做为其中的一个模块,修改POM文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dothwinds</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.dothwinds</groupId>
<artifactId>spring-cloud-study-eureka</artifactId>
<version>1.0.0</version>
<name>spring-cloud-study-eureka</name>
<description>Spring Cloud study Eureka Server</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
5 修改Eureka Server代码及配置
在启动类中添加注解@EnableEurekaServer开启EurekaServer
package org.dothwinds.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudStudyEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStudyEurekaApplication.class, args);
}
}
添加配置信息application.yml
server:
port: 8100 #访问端口
spring:
application:
name: eureka-server #服务名称
eureka:
client:
fetch-registry: false #是否需要去注册中心获取其它服务地址,集群的时候要用,单server不用
register-with-eureka: false #是否去eureka注册,本身就是服务中心,无需注册
service-url:
defaultZone: http://localhost:8101/eureka #指向与本地实例相同的主机。
instance:
hostname: localhost
启动服务,绑定8100端口,访问Eureka Server界面,这样Eureka Server就启动成功了。
6 构建Eureka Client
按照上述构建Eureka Server的模式构建新的项目模块Service-Provider,作为Eureka Client,配置好POM文件及修改相关代码
在启动类上加入@EnableEurekaClient,在配置文件中加入指向Eureka Server的相关配置,如下所示:
package org.dothwinds.serviceprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudStudyServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStudyServiceProviderApplication.class, args);
}
}
修改application.yml文件
server:
port: 9001
spring:
application:
name: service-provider #应用名称,会显示在eureka server中
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/ #eureka server地址
启动服务,去Eureka Server中查看服务已经注册进去了。
参考资料:
代码:https://gitee.com/dothwinds/Spring-Cloud-Study/tree/master/spring-cloud-study-eureka