开始
- 鉴于目前对微服务的认知,按大致如下顺序展开: 注册中心、provider、consumer、集群、负载均衡、容错。
- 本项目使用 spring-cloud 的版本为 Hoxton, 当前时间 2020.03.13 。
- 基于 spring-boot 构建项目, 查看 spring-cloud 和 spring-boot 版本的对应 https://cloud.spring.io/sprin...
- jdk version 1.8
- 项目 和 子模块均是 maven 项目。
- 在已经知道配置文件重要性的前提下,尽量少埋伏笔,给出目前最全的配置文件内容。
-
各系统端口按下表计划,并随时补充
角色 端口 Consumer 31000 Gateway 32000 Provider 33000 Eureka Server 34000 Dashboard 35000 Config Server 36000 - IDE 为 Intellij IDEA
创建根项目
- 新建 maven 项目:
java.spring-cloud.Hoxton
-
pom
文件添加 properties<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
-
修改
pom
文件,按官网的描述:Release Train Version: Hoxton.SR3 Supported Boot Version: 2.2.5.RELEASE
确定版本,如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
at 2020.01.29: spring boot 解决了 2.2.3 的大部分问题,但是升级到 2.2.3 后,也是翻车连连。将版本升级至 2.2.4 可解决目前已知问题。
maven 与 java 在继承方面的定义一样,都是单继承。单独使用 spring-boot 时候,可以使用 parent 标签继承 spring-boot 的 pom 文件,但是想继承多个 pom 是不可能的。 <scope>import</scope> 用于解决这个问题。
这里采用继承
spring-boot-starter-parent
,引入spring-cloud-dependencies
。spring-boot-starter-parent
中有很多写好的插件、构建等可以直接拿来用,因为 spring cloud 也是基于 spring boot 封箱的。 可以跟踪到引入的 pom 中看看都有什么。另外,根项目只需要 使用 dependencyManagement 预定义 dependencies 即可,严禁使用 dependencies ,此时项目下 lib 库仍然没包含任何 jar。子项目按需引入。
工具类项目准备
- 新建 model :
spring-cloud.s01.support
-
时间工具类,新建类:
EastEightInstantUtil
, 该类主要处理东八区时间 和 时间字符串。public class EastEightInstantUtil { public static ZoneId chinaTimeZone = ZoneId.of("Asia/Shanghai"); public static Clock eastEightClock = Clock.offset(Clock.systemUTC(), Duration.ofHours(+8)); private static DateTimeFormatter dateTimeFormatterMillis = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(eastEightClock.getZone()); private static DateTimeFormatter dateTimeFormatterSecond = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(eastEightClock.getZone()); /** * 获取指定时间字符串,形如:2020-01-02T03:04:05.123456789Z * 1秒 = 1_000毫秒 = 1_000_000微秒 = 1_000_000_000纳秒 * @param year 指定的年份 * @param month 指定的月份,枚举类型 * @param day 指定日期,确定该月是否有该日 * @param hour 指定小时 * @param minute 指定分钟 * @param second 指定秒 * @param nano 指定纳秒,如不需要可传入 0。 * @return LocalDateTime 或 Instant 的时间字符串,形如: 2020-01-02T03:04:05.123456789Z */ public static String getLocalDateTimeStr(int year, Month month, int day, int hour, int minute, int second, int nano) { return LocalDateTime.of(year, month, day, hour, minute, second, nano).toString() + "Z"; } public static LocalDateTime getLocalDateTime(int year, Month month, int day, int hour, int minute, int second, int nano) { return LocalDateTime.of(year, month, day, hour, minute, second, nano); } public static Instant nowInstant() { return Instant.now(eastEightClock); } public static long getNowEpochSecond() { return Instant.now(eastEightClock).getEpochSecond(); } public static long getNowEpochMilli() { return Instant.now(eastEightClock).toEpochMilli(); } public static long getNowEpochNano() { return Instant.now(eastEightClock).getNano(); } public static Instant fromLocalDateTimeStr(String str) { return Instant.parse(str); } public static String nowDateTimeWithMillisStr() { return dateTimeFormatterMillis.format(Instant.now(eastEightClock)); } public static String nowDateTimeWithSecondStr() { return dateTimeFormatterSecond.format(Instant.now(eastEightClock)); } }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。