SpringCloud 初始化父子项目并集成 Consul 服务发现
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,如下图,在底部 </build>
标签下切换国内阿里源
<repositories> <!--阿里云主仓库,代理了maven central和jcenter仓库--> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <!--阿里云代理Spring 官方仓库--> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://maven.aliyun.com/repository/spring</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
保存,然后使用 IDEA 打开,等Maven加载完毕
新建子项目
右击项目名称,New -> Module
包名自定义,不一定非要相同,这里的包名或许后面也要改掉
下一步选择包,先跳过,点击Finish
完成
关联父子项目
在父 Module
的pom.xml
中的<dependencies>
标签上部新增
<modules> <module>pro-service</module> </modules>
如图示
在子项目 pro-service
中的 <modelVersion>4.0.0</modelVersion>
标签后追加
<parent> <groupId>com.github.springtools</groupId> <artifactId>SpringCloudPro</artifactId> <version>0.0.1</version> </parent>
到这里会发现Maven
找不到一些Spring Boot
的依赖
在父项目增加管理依赖版本号的定义
最终的父项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.springtools</groupId> <artifactId>SpringCloudPro</artifactId> <version>0.0.1</version> <name>SpringCloudPro</name> <description>父项目</description> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR5</spring-cloud.version> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> <modules> <module>pro-service</module> </modules> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <!--支持Spring Boot 2.3.X--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.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> <!--阿里云主仓库,代理了maven central和jcenter仓库--> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <!--阿里云代理Spring 官方仓库--> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://maven.aliyun.com/repository/spring</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
子项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.github.springtools</groupId> <artifactId>SpringCloudPro</artifactId> <version>0.0.1</version> </parent> <artifactId>pro-service</artifactId> <version>0.0.1</version> <name>pro-service</name> <description>业务模块</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
项目结构图
集成服务发现 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 @SpringBootApplication public 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:写着写着又凌晨一点了,头发又少了,一天天儿~的过得也太快了,淦
把知识以白话的形式写出来,力求看了我的文章,可以不记得我,但一定学到一点知识,就是我最大的欣慰,简洁Java知识与开发,完善技术栈,水滴石穿,希望可以帮到你