SpringCloud 初始化父子项目并集成 Consul 服务发现
准备工作
- IDEA2020.1
- JDK1.8
- Spring Cloud 版本:Hoxton.SR5
- Spring Boot 版本:2.3.0.RELEASE
安装 Consul
- windows 安装:https://www.yuque.com/ekko/app/go38vo
- Mac 安装:https://www.yuque.com/ekko/app/nzw3gi
开始
简介:consul 的功能
- 服务发现
- Key/Value 存储
- 健康检查
今天我们主要来学习实践服务发现功能先到 https://start.spring.io/ 初始化一个父项目

生成之后解压,先用 IDE 编辑文件修改 pom.xml,如下图,在底部 标签下切换国内阿里源
aliyunaliyunhttps://maven.aliyun.com/repository/publictruefalsespring-milestonesSpring Milestoneshttps://maven.aliyun.com/repository/springtruefalse
保存,然后使用 IDEA 打开,等 Maven 加载完毕
新建子项目
右击项目名称,New -> Module

包名自定义,不一定非要相同,这里的包名或许后面也要改掉

下一步选择包,先跳过,点击Finish 完成
关联父子项目
在父 Module的pom.xml中的标签上部新增
pro-service
如图示

在子项目 pro-service 中的 4.0.0 标签后追加
com.github.springtools SpringCloudPro 0.0.1
到这里会发现Maven找不到一些Spring Boot的依赖在父项目增加管理依赖版本号的定义

最终的父项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.github.springtools SpringCloudPro 0.0.1SpringCloudPro父项目pom1.8Hoxton.SR52.3.0.RELEASEpro-serviceorg.springframework.cloud spring-cloud-starter-consul-config org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test testorg.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version}pomimportorg.springframework.boot spring-boot-dependencies ${spring-boot.version}pomimportorg.springframework.boot spring-boot-maven-plugin aliyunaliyunhttps://maven.aliyun.com/repository/publictruefalsespring-milestonesSpring Milestoneshttps://maven.aliyun.com/repository/springtruefalse
子项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.github.springtools SpringCloudPro 0.0.1 pro-service 0.0.1pro-service业务模块1.8org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
项目结构图

集成服务发现 Consul
父项目添加如下依赖,在上述的最终父项目pom.xml中已经加过了,这里简要说明

在pro-service中的resources目录下, 删除自带的application.properties,新增bootstrap.yml
因为后续的config要用到,bootstrap比application.properties加载优先级别高
完善子项目并注册到 Consul
bootstrap.yml
spring: application: name: pro-service cloud: consul: discovery: health-check-path: /actuator/health # 检测实例健康 health-check-interval: 10s # 每隔10s检查 hostname: localhost # 配置实例地址 register: true # 自动注册 service-name: ${spring.application.name} # 实例名称 host: localhost port: 8500
ps: 这里的spring.application.name与spring.cloud.consul.discovery.service-name一定要加,不然会抛出异常
Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits-- 参考:https://www.jianshu.com/p/83d3a8105620
定义启动类 ProServiceApplication.java
package com.github.springtools.proservice;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableDiscoveryClient@SpringBootApplicationpublic class ProServiceApplication { @GetMapping("/") public String hello() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(ProServiceApplication.class, args); }}
启动

打开 Consul Web 控制台 http://localhost:8500

好了,今天就先写到这里了
源代码地址
https://github.com/Gleans/SpringCloudPro
ps: 写着写着又凌晨一点了,头发又少了,一天天儿~ 的过得也太快了,淦
全文完