一、Spring Cloud简介
Spring Boot版本2.1.1.RELEASE,Spring Cloud版本为Greenwich.RC2。
Greenwich版本的官方文档如下:http://cloud.spring.io/spring-cloud-static/Greenwich.RC2/single/spring-cloud.html
Spring Cloud为开发人员提供了快速构建分布式微服务系统的一些工具(服务发现与注册、配置管理、断路器、路由、微代理、消息总线、全局锁、决策竞选、分布式会话等等)。Spring Cloud是基于Spring Boot搭建,所以需要开发中对Spring Boot有一定的了解。
二、创建服务注册中心
当前采用Eureka作为服务注册与发现的组件(还可以使用Zookeeper/Consul )。
2.1 首先创建一个maven 主工程its-spb-master(使用聚合工程)
首先创建一个主Maven工程,在其pom文件引入依赖,Spring Boot版本2.1.1.RELEASE,Spring Cloud版本为Greenwich.RC2。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。后续Spring Cloud一系列文章全部采用聚合工程模式,its-spb-master工程pom:
<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.its.parent</groupId>
<artifactId>its-spb-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>its-spb-parent</name>
<description>its-spb-parent</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- SpringBoot整合Web端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合test -->
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<modules>
<module>its-spb-eureka</module>
<module>its-spb-config-servers</module>
<module>its-spb-common</module>
<module>its-spb-zuul</module>
<module>its-spb-base-servers-api</module>
<module>its-spb-base-servers</module>
<module>its-spb-order-servers-api</module>
<module>its-spb-order-servers</module>
<module>its-spb-web</module>
</modules>
</project>
2.2 创建Eureka Server工程its-spb-eureka
<?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.its.eureka</groupId>
<artifactId>its-spb-eureka</artifactId>
<version>1.0.0</version>
<name>its-spb-eureka</name>
<description>its-spb-eureka</description>
<parent>
<groupId>com.its.parent</groupId>
<artifactId>its-spb-parent</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<!-- SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.3 启动EurekaServer注册中心,创建ItsSpbEurekaApplication 类,@EnableEurekaServer开启EurekaServer
package com.its.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* SpringBoot启动
*
*/
@SpringBootApplication
@EnableEurekaServer
public class ItsSpbEurekaApplication {
// @EnableEurekaServer 表示开启EurekaServer服务 开启注册中心
public static void main(String[] args) {
SpringApplication.run(ItsSpbEurekaApplication.class, args);
}
// SpringCloud支持三种注册中心:Eureka/Zookeeper/Consul
}
2.4 its-spb-eureka配置appication.yml:
server:
###注册中心服务端口号
port: 8101
eureka:
instance:
###注册中心IP地址
hostname: 127.0.0.1
client:
serviceUrl:
###注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为自己是注册中心,是否需要将自己注册给自己的注册中心(集群的时候需要设置为true)
registerWithEureka: false
###因为自己是注册中心,不需要去检索服务信息
fetchRegistry: false
###开发环境关闭自我保护机制,保证不可用服务及时剔除
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 2000
###通过 http://127.0.0.1:8101/ 可访问注册中心
###集群
#server:
# ###注册中心服务端口号
# port: 8101
#
#spring:
# application:
# ###服务别名--服务注册到注册中心名称
# name: its-spb-eureka
#eureka:
# instance:
# ###注册中心IP地址
# hostname: 127.0.0.1
# client:
# serviceUrl:
# ###注册地址
####eureka集群,通过启动多台eureka服务,相互注册实现集群,端口为另一台eureka服务
# defaultZone: http://${eureka.instance.hostname}:8102/eureka/
# ###因为自己是注册中心,是否需要将自己注册给自己的注册中心(集群的时候需要设置为true)
# registerWithEureka: true
# ###因为自己是注册中心,不需要去检索服务信息
# fetchRegistry: true
###Eureka自我保护机制:
####是为了防止EurekaClient可以正常运行,但是与EurekaServer网络不通的情况下,EurekaServer不会将EurekaClent服务剔除
####默认情况下EurekaClient定时向EurekaServer端发送心跳包,如果EurekaServer端在一定时间内没有收到EurekaClient发送的心跳包,便会直接从服务注册列表中剔除该服务(默认90S)
####但是在短时间内丢失了大量服务实例心跳,这时EurekaServer会开启自我保护机制,不会剔除该服务
####默认是开启自我保护机制,建议在开发环境关闭,生产环境开启
2.5 运行ItsSpbEurekaApplication类启动Eureka Server,启动成功后浏览器访问http://127.0.0.1:8101,可查看Eureka注册中心
三、创建一个微服务提供者工程its-spb-base-servers(Eureka Client)
微服务向Eureka Server注册后,它会提供一些元数据(主机和端口,URL,主页等)。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该提供者实例从注册Eureka Server中移除。
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.its.base.servers</groupId>
<artifactId>its-spb-base-servers</artifactId>
<version>1.0.0</version>
<name>its-spb-base-servers</name>
<description>its-spb-base-servers</description>
<parent>
<groupId>com.its.parent</groupId>
<artifactId>its-spb-parent</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<!-- SpringBoot整合Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- SpringBoot整合Zookeeper客户端 需要注意Zookeeper的安装版本 <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> -->
</dependencies>
</project>
appication.yml配置文件:
server:
###Base服务端口号
port: 8201
spring:
application:
###服务别名--服务注册到注册中心名称
name: its-spb-base-servers
#不同注册中心更换,接口方式调用不变,只需要变更yml注册中心配置文件及Maven pom依赖信息
#Eureka注册中心配置
eureka:
client:
serviceUrl:
###注册地址
defaultZone: http://127.0.0.1:8101/eureka/
###集群
# defaultZone: http://127.0.0.1:8101/eureka/,http://127.0.0.1:8102/eureka/
###是否需要将自己服务注册到eureka注册中心(true:是,false:否)
registerWithEureka: true
###是否需要去检索服务信息(true:是,false:否)
fetchRegistry: true
###心跳检测与续约时间,测试时将值设置小些,保证服务关闭后注册中心能及时剔除
instance:
###EurekaClient向EurekaServer端发送心跳的时间间隔,单位为秒
leaseRenewalIntervalInSeconds: 1
###EurekaServer在收到最后一次心跳之后的等待时间上限,单位为秒,超时则剔除
leaseExpirationDurationInSeconds: 2
#Zookeeper注册中心配置
# cloud:
# zookeeper:
# connect-string: 10.202.107.143:2181
创建Controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberApiController {
@Value("${server.port}")
private String port;
@ApiOperation("获取会员信息")
@GetMapping("/getMember")
public String getMember() {
return "this is member server" + port;
}
}
运行ItsSpbBaseServersApplication类启动Eureka Client
package com.its.base.servers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
// @EnableEurekaClient 将当前服务注册到Eureka注册中心,如果注册中心是Eureka使用,向注册中心注册服务
@EnableEurekaClient
// @EnableDiscoveryClient如果注册中心是Zookeeper、Connsul使用,向注册中心注册服务
// @EnableDiscoveryClient
public class ItsSpbBaseServersApplication {
public static void main(String[] args) {
SpringApplication.run(ItsSpbBaseServersApplication.class, args);
}
}
浏览器访问http://127.0.0.1:8101,查看微服务注册到Eureka信息