1.docker 安裝consul
docker-compose.yaml
version: "3"
services:
consul:
image: consul:1.4.4
container_name: consul
environment:
- CONSUL_BIND_INTERFACE=eth0
ports:
- "8500:8500"
這里使用的是consul的1.4.4版本的image,可以根據(jù)需要更換不同的版本。
在docker-compose.yaml文件所在路勁執(zhí)行如下指令后
docker-compose up -d
查看當前容器運行情況
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
417107b6159c consul:1.4.4 "docker-entrypoint.s…" 6 days ago Up 2 hours 8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp consul
此時,可以訪問consul的dashboard界面
localhost:500
2.創(chuàng)建基于springboot的client
上述docker安裝的consul server作為服務發(fā)現(xiàn)中心,此時創(chuàng)建client并注冊到注冊中心。
2.1 依賴版本
name | version |
---|---|
spring-boot | 2.7.15 |
spring-cloud | 2021.0.8 |
JAVA | 11 |
Kotlin | 1.6 |
Maven | 3.9 |
2.2 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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sb-consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sb-consul</name>
<description>sb-consul</description>
<properties>
<java.version>11</java.version>
<kotlin.version>1.6.21</kotlin.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</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-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>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
注意: 這里重要的有兩個依賴,分別是:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
這個用來注冊到consul的注冊中心
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
這個是用來做健康檢測的,不加的話,請求接口HTTP GET http://192.168.19.123:10086/actuator/health會失敗,認為服務不健康。(這里我理解為服務不健康,就不可以對外提供服務,但是我本地起了兩個一樣的服務,一個是通過健康檢測的,一個沒有,但是都可以訪問到。不知道為什么。。)
2.3 啟動類
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
class SbConsulApplication
fun main(args: Array<String>) {
runApplication<SbConsulApplication>(*args)
}
注意添加注解@EnableDiscoveryClient,不過是使用Eureka還是Consul這種注冊中心,都需要指明服務發(fā)現(xiàn)的client。
2.4 application.properties
spring.application.name=YYtest
server.port=10086
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.serviceName=${spring.application.name}
這里主要就是指明服務發(fā)現(xiàn)的配置內(nèi)容,即Consul server的地址。
3 搭建完成
這個時候,再訪問Consul的dashboard界面,可以看到服務已經(jīng)注冊成功。
點擊我們的服務YYtest 服務文章來源:http://www.zghlxwxcb.cn/news/detail-696162.html
4. 總結(jié)
先感受了一下Consul,又好像什么都沒感受一樣。繼續(xù)深入學習。。。文章來源地址http://www.zghlxwxcb.cn/news/detail-696162.html
到了這里,關于spring boot + Consul 示例 (Kotlin版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!