SpringCloud 初始化父子項目並集成 Consul 服務發現

SpringCloud 初始化父子項目並集成 Consul 服務發現

準備工做

  • IDEA2020.1
  • JDK1.8
  • Spring Cloud版本:Hoxton.SR5
  • Spring Boot 版本:2.3.0.RELEASE

安裝Consul

開始

簡介:consul的功能java

  • 服務發現
  • Key/Value存儲
  • 健康檢查

今天咱們主要來學習實踐服務發現功能
先到 https://start.spring.io/ 初始化一個父項目
初始化一個父項目
生成以後解壓,先用IDE編輯文件修改pom.xml,以下圖,在底部 </build> 標籤下切換國內阿里源git

<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加載完畢github

新建子項目

右擊項目名稱,New -> Module
New -&gt; Module
包名自定義,不必定非要相同,這裏的包名或許後面也要改掉
設置子模塊信息
下一步選擇包,先跳過,點擊Finish 完成web

關聯父子項目

在父 Modulepom.xml中的<dependencies>標籤上部新增spring

<modules>
        <module>pro-service</module>
    </modules>

如圖示
修改後的父pom
在子項目 pro-service 中的 <modelVersion>4.0.0</modelVersion> 標籤後追加apache

<parent>
        <groupId>com.github.springtools</groupId>
        <artifactId>SpringCloudPro</artifactId>
        <version>0.0.1</version>
    </parent>

到這裏會發現Maven找不到一些Spring Boot的依賴
在父項目增長管理依賴版本號的定義
在父項目增長管理依賴版本號
最終的父項目pom.xmlbootstrap

<?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.xmlwindows

<?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>

項目結構圖
項目結構圖app

集成服務發現 Consul

父項目添加以下依賴,在上述的最終父項目pom.xml中已經加過了,這裏簡要說明maven

依賴簡要說明
pro-service中的resources目錄下,刪除自帶的application.properties,新增bootstrap.yml

由於後續的config要用到,bootstrapapplication.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.namespring.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:寫着寫着又凌晨一點了,頭髮又少了,一每天兒~的過得也太快了,淦

公衆號

相關文章
相關標籤/搜索