spring boot + Consul 示例 (Kotlin版)

文章目录

  • 1.docker 安装consul
  • 2.创建基于springboot的client
    • 2.1 依赖版本
    • 2.2 pom.xml
    • 2.3 启动类
    • 2.4 application.properties
  • 3 搭建完成
  • 4. 总结

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,可以根据需要更换不同的版本。
在docker-compose.yaml文件所在路劲执行如下指令后

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
spring boot + Consul 示例 (Kotlin版)_第1张图片

2.创建基于springboot的client

上述docker安装的consul server作为服务发现中心,此时创建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


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.15version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>sb-consulartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>sb-consulname>
    <description>sb-consuldescription>
    <properties>
        <java.version>11java.version>
        <kotlin.version>1.6.21kotlin.version>
        <spring-cloud.version>2021.0.8spring-cloud.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.modulegroupId>
            <artifactId>jackson-module-kotlinartifactId>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-reflectartifactId>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-stdlib-jdk8artifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-consul-discoveryartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlinsourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlintestSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.jetbrains.kotlingroupId>
                <artifactId>kotlin-maven-pluginartifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strictarg>
                    args>
                    <compilerPlugins>
                        <plugin>springplugin>
                    compilerPlugins>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlingroupId>
                        <artifactId>kotlin-maven-allopenartifactId>
                        <version>${kotlin.version}version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

注意: 这里重要的有两个依赖,分别是:

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>

这个用来注册到consul的注册中心

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
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这种注册中心,都需要指明服务发现的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}

这里主要就是指明服务发现的配置内容,即Consul server的地址。

3 搭建完成

这个时候,再访问Consul的dashboard界面,可以看到服务已经注册成功。
spring boot + Consul 示例 (Kotlin版)_第2张图片
点击我们的服务YYtest 服务
spring boot + Consul 示例 (Kotlin版)_第3张图片
spring boot + Consul 示例 (Kotlin版)_第4张图片

4. 总结

先感受了一下Consul,又好像什么都没感受一样。继续深入学习。。。

你可能感兴趣的:(kotlin,spring-cloud,spring,boot,consul)