
简介:consul的功能
今天我们主要来学习实践服务发现功能
先到 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>到这里会发现<?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<?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**
```yamlspring:
application:
name: pro-servicecloud:
consul: discovery: health-check-path: /actuator/health # 检测实例健康 health-check-interval: 10s # 每隔10s检查 hostname: localhost # 配置实例地址 register: true # 自动注册 service-name: ${spring.application.name} # 实例名称 host: localhost port: 8500ps:这里的`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`
```javapackage 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:写着写着又凌晨一点了,头发又少了,一天天儿~的过得也太快了,淦
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。