一. Spring Cloud简介
Spring Cloud为开发者提供了一套快速构建分布式系统的工具,比如配置中心,服务发现,服务发现,熔断器,智能路由,分布式session,集群状态管理等。使用Spring Cloud开发可以快速搭建你所需要的服务或应用。他可以在不同的分布式环境中运行,包括用户的个人电脑,公司的内网环境,以及这种云服务器中。
目前官方文档中,已经推出了以下版本的Spring Cloud,Release Train代表的是当前Spring Cloud的版本,Boot Version代表的是当前Spring Cloud所依赖的Spring Boot版本,如果版本不匹配,可能出现一些意想不到的错误。
Release Train | Boot Version |
---|---|
Hoxton | 2.2.x |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
二. 环境搭建
1. 创建父工程
父工程中管理着Spring Cloud的版本,以及子工程模块
在创建好的maven父工程添加以下依赖管理,dependencyManagement中管理着Spring Cloud和Spring Boot所依赖的所有版本,后续在子工程中就不需要再指定版本信息了。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 创建服务注册中心子模块
注册中心子模块的pom文件中添加如下依赖,用作Eureka注册中心的服务端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在子模块根目录创建com包,并在com包下创建程序启动类
启动类中添加注解@EnableEurekaServer,表明该应用程序是Eureka服务端
@SpringBootApplication
@EnableEurekaServer
public class RegisterCenterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterCenterApplication.class, args);
}
}
在resource文件夹下创建application.yml文件,为eureka服务注册中心提供必要的配置
server:
port: 3000 # Eureka应用的web端口
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 是否将应用本身注册到服务中心,单机可以设置成false,如果是Eureka服务端要做集群,那么设置为true,这样服务端集群之间才能做服务同步
fetch-registry: false # 是否拉取集群上的其他节点的注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服务端默认注册地址
spring:
application:
name: cloud-register-center # 应用名,后续注册到注册中心的服务名,后续做负载均衡等都是以该服务名作为请求路径
以main方法的方式启动RegisterCenterApplication
启动成功后浏览器打开http://localhost:3000/
三. 创建服务注册客户端
客户端子模块
在该模块下的pom文件添加以下依赖
<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-web</artifactId>
</dependency>
按照服务端一样的流程创建启动类,此处不需要@EnableEurekaServer,因为这里是客户端,可以加上
@EnableEurekaClient注解,我这里的Spring Cloud版本不加也可以
@SpringBootApplication
public class DqcsbApplication {
public static void main(String[] args) {
SpringApplication.run(DqcsbApplication.class, args);
}
}
在resource文件夹下创建application.yml文件,为eureka服务注册客户端的配置
server:
port: 3000 # Eureka应用的web端口
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true # 客户端这里需要设置为true,因为我们需要把我们的应用注册到服务中心去
fetch-registry: true # 这里也设置为true,因为我们需要拉取注册信息,获取到其他微服务的注册信息,这样才能正确调用
service-url:
defaultZone: http://localhost:3000/eureka/ # 服务端默认注册地址
spring:
application:
name: cloud-dqcsb # 应用名
启动客户端应用,这时你会发现再次打开http://localhost:3000,服务端已经有应用注册上了,而应用名就是上面配置文件的CLOUD-DQCSB大写