Spring Cloud 微服务搭建 使用Maven

  • 1. 本文中使用的各组件版本
  • (1). Spring2.6.3
  • Spring2.6.3配置仓库为 spirng的仓库
  • (2). Spring2.6.7 JDK17
  • Spring2.6.7需要加入spring的四个仓库
  • (3). Spring3.2.5 JDK22
  • 2. 创建父工程
  • (1). 创建Maven项目
  • (2). 选择JDK 11 和 Maven项目
  • 最新版的依赖, 可以选择JDK17
  • (3). 项目命名
  • (4). 完成
  • (5). 删除src目录
  • (6). 引入依赖
  • 添加``
  • (7). 引入Maven打包配置
  • 3. 创建子模块
  • (1). 创建Maven项目
  • (2). 根据需要引入依赖
  • 1). Spring MVC
  • 2). 数据库驱动
  • 3). nacos注册中心服务发现
  • 4). nacos注册中心配置
  • 5). Feign
  • 6). Sentinel
  • 7). Spring Cloud sleuth
  • 8). Zipkin
  • 9). Gateway
  • 10). SpringSecurity
  • 4. 创建另一个子模块 (子模块依赖子模块)
  • (1). 创建Maven项目
  • (2). 子模块依赖子模块
  • (3). 测试
  • 1). clean
  • 2). package
  • 5. Mybatis依赖引入
  • (1). 父工程引入依赖
  • (2). 子模块引入依赖
  • 6. 依赖需要的相关常用配置 (.properties)
  • (1). 一般的配置
  • (2). 数据库驱动配置
  • (3). nacos注册中心服务发现
  • (4). nacos注册中心配置
  • (5). Sentinel
  • (6). Feign
  • (7). Zipkin
  • 1). 配置
  • 2). 指定数据库启动:
  • 3). 指定的数据库建表语句
  • (8). Sleuth
  • (9). Gateway
  • 7. 依赖Jar包
  • (1). Jar下载
  • (2). Jar启动
  • java -jar xxx.jar
  • java -jar xxx.jar &
  • nohup java -jar xxx.jar &
  • 8. docker 镜像
  • (1). nacos
  • (2). sentinel
  • (3). zipkin
  • (4). 注意


1. 本文中使用的各组件版本

jdk

Spring Cloud

Spring Cloud Alibaba

Spring Boot

nacos

sentinel

zipkin

OpenJDK 11

Hoxton.SR8

2.2.1.RELEASE

2.3.3.RELEASE

1.3.2

1.8.0

2.23.15

<dependencyManagement>
        <!--        Spring Boot-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--            Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--            Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            
        </dependencies>
        
    </dependencyManagement>

(1). Spring2.6.3

<dependencyManagement>
        <!--        Spring Boot-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>5.3.16</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Data-->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-bom</artifactId>
                <version>2021.1.2</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <!--        Spring Security-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-bom</artifactId>
                <version>5.6.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Spring2.6.3配置仓库为 spirng的仓库

最新版的部分依赖需要spring的仓库

<repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>

        <repository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot Repository</name>
            <url>https://repo.spring.io/snapshot</url>
        </repository>
    </repositories>

(2). Spring2.6.7 JDK17

<properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <spring.boot.version>2.6.7</spring.boot.version>
        <spring.framework.version>5.3.19</spring.framework.version>
        <spring.cloud.version>2021.0.2</spring.cloud.version>
        <spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
        <spring.data.version>2021.1.4-SNAPSHOT</spring.data.version>
        <spring.security.version>5.6.4-SNAPSHOT</spring.security.version>
        <hutool.version>5.7.22</hutool.version>
        <passay.version>1.6.1</passay.version>
        <java-otp.version>0.3.1</java-otp.version>
        <redisson.version>3.17.0</redisson.version>
        <slf4j-api.version>1.7.30</slf4j-api.version>
    </properties>


    <dependencyManagement>
        <!--        Spring Boot-->
        <dependencies>

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j-api.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.framework.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Data-->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-bom</artifactId>
                <version>${spring.data.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <!--        Spring Security-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-bom</artifactId>
                <version>${spring.security.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        文件, json, 字符串等处理-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>

            <!--        密码校验框架-->
            <dependency>
                <groupId>org.passay</groupId>
                <artifactId>passay</artifactId>
                <version>${passay.version}</version>
            </dependency>

<!--            opt-->
            <dependency>
                <groupId>com.eatthepath</groupId>
                <artifactId>java-otp</artifactId>
                <version>${java-otp.version}</version>
            </dependency>

<!--            redis分布式锁-->
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>${redisson.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

Spring2.6.7需要加入spring的四个仓库

<repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>

        <repository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot Repository</name>
            <url>https://repo.spring.io/snapshot</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

(3). Spring3.2.5 JDK22

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.2.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>6.1.6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2023.0.0.0-RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Data-->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-bom</artifactId>
                <version>2023.1.3</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <!--        Spring Security-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-bom</artifactId>
                <version>6.2.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        MySQL-->
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <version>8.3.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.eatthepath</groupId>
                <artifactId>java-otp</artifactId>
                <version>0.4.0</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.8.27</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>3.3.4</version>
            </dependency>

            <dependency>
                <groupId>jakarta.validation</groupId>
                <artifactId>jakarta.validation-api</artifactId>
                <version>3.0.2</version>
            </dependency>


            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.17.0</version>
            </dependency>

            <!--        redis分布式锁-->
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>3.27.2</version>
            </dependency>


        </dependencies>
    </dependencyManagement>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

2. 创建父工程

(1). 创建Maven项目

springcloud 搭建父依赖 springcloud maven依赖_spring

(2). 选择JDK 11 和 Maven项目

最新版的依赖, 可以选择JDK17

springcloud 搭建父依赖 springcloud maven依赖_spring cloud_02

(3). 项目命名

springcloud 搭建父依赖 springcloud maven依赖_微服务_03

(4). 完成

springcloud 搭建父依赖 springcloud maven依赖_spring_04

(5). 删除src目录

因为父工程不写代码, 所有删除不影响

springcloud 搭建父依赖 springcloud maven依赖_微服务_05

(6). 引入依赖

  • 旧版
<dependencyManagement>
<!--        Spring Boot-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            
<!--            Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

<!--            Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • 最新版
<dependencyManagement>
        <!--        Spring Boot-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>5.3.16</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Cloud Alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--        Spring Data-->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-bom</artifactId>
                <version>2021.1.2</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <!--        Spring Security-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-bom</artifactId>
                <version>5.6.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>

        <repository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot Repository</name>
            <url>https://repo.spring.io/snapshot</url>
        </repository>
    </repositories>

插入位置

springcloud 搭建父依赖 springcloud maven依赖_微服务_06

  • 旧版
  • 新版

添加<packaging></packaging>

springcloud 搭建父依赖 springcloud maven依赖_微服务_07

(7). 引入Maven打包配置

<!--    maven插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

springcloud 搭建父依赖 springcloud maven依赖_spring_08

3. 创建子模块

(1). 创建Maven项目

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_09


按照父工程的创建过程, 名字按照自己的需要来

springcloud 搭建父依赖 springcloud maven依赖_微服务_10


点开父工程pom, 可以直接看到自动加入了module

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_11

(2). 根据需要引入依赖

springcloud 搭建父依赖 springcloud maven依赖_微服务_12

1). Spring MVC

<!--        Spring MVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2). 数据库驱动

<!--        数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

3). nacos注册中心服务发现

使用JDK11对应的依赖

<!--        nacos注册中心服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

使用最新版所对应的依赖

<!--        nacos注册中心服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

4). nacos注册中心配置

JDK11使用对应的依赖

<!--        nacos注册中心配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

最新版springboot对应的依赖

<!--        nacos注册中心配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
		<dependency>
 			<groupId>org.springframework.cloud</groupId>
 			<artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>

5). Feign

<!--        feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

6). Sentinel

<!--        sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

7). Spring Cloud sleuth

<!--       Spring Cloud sleuth-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

8). Zipkin

老版本依赖

<!--        zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

新版本依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

9). Gateway

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
10). SpringSecurity

新版

<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

4. 创建另一个子模块 (子模块依赖子模块)

(1). 创建Maven项目

按照相同的步骤创建

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_13

(2). 子模块依赖子模块

order900 依赖 common

<dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

springcloud 搭建父依赖 springcloud maven依赖_spring cloud_14


注意

groupId
artifactId
version

要与引入的子模块一致

(3). 测试

测试一下是否可以打包成功

1). clean

在父工程的pom下, 选择clean, 清理一下

springcloud 搭建父依赖 springcloud maven依赖_spring cloud_15


BUILD SUCCESS 表示成功

springcloud 搭建父依赖 springcloud maven依赖_微服务_16


此时的目录如下

springcloud 搭建父依赖 springcloud maven依赖_spring_17

2). package

springcloud 搭建父依赖 springcloud maven依赖_spring_18


BUILD SUCCESS

springcloud 搭建父依赖 springcloud maven依赖_微服务_19


看看目录

springcloud 搭建父依赖 springcloud maven依赖_maven_20


出现了jar文件

这个就是用来部署到服务器上的

5. Mybatis依赖引入

(1). 父工程引入依赖

<!--            mybatis-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_21


引入后刷新一下Maven

(2). 子模块引入依赖

<!--        mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_22


springcloud 搭建父依赖 springcloud maven依赖_微服务_23


测试一下成不成功

6. 依赖需要的相关常用配置 (.properties)

(1). 一般的配置

# 启动端口, 默认是8080
server.port=9003
# 应用的名称
spring.application.name=video-service

(2). 数据库驱动配置

# 数据库驱动名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库的url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
# 数据库用户名和密码
spring.datasource.username=root
spring.datasource.password=123456

springcloud 搭建父依赖 springcloud maven依赖_springcloud 搭建父依赖_24

(3). nacos注册中心服务发现

# nacos注册中心地址
spring.cloud.nacos.discovery.server-addr=192.168.100.130:8848
# 使用的命名空间
spring.cloud.nacos.discovery.namespace=SpringCPQuestion
# 发现服务
spring.cloud.nacos.discovery.service=${spring.application.name}
# 分组
spring.cloud.nacos.group=DEFAULT_GROUP

(4). nacos注册中心配置

bootstrap.properties中配置

# 应用的名称
spring.application.name=video-service
# nacos配置中心地址
spring.cloud.nacos.config.server-addr=192.168.100.130:8848
# 使用的配置文件类型
spring.cloud.nacos.config.file-extension=properties
# 文件后缀
spring.profiles.active=dev

上述配置在注册中心添加配置文件时, 应该以下图方式命名

springcloud 搭建父依赖 springcloud maven依赖_微服务_25


{spring.application.name}-{spring.profiles.active}

(5). Sentinel

# sentinel控制台端口
spring.cloud.sentinel.transport.dashboard=192.168.100.130:8080
# 与sentinel通信的端口
spring.cloud.sentinel.transport.port=9999

(6). Feign

# 开启feign对sentinel的支持
feign.sentinel.enabled=true

(7). Zipkin

1). 配置

# zipkin地址
spring.zipkin.base-url=http://192.168.100.130:9411/
# 不⽤开启服务发现
spring.zipkin.discovery-client-enabled=false

2). 指定数据库启动:

java -jar zipkin-server-x.xx.x-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=数据库名字 --MYSQL_USER=root --MYSQL_PASS=数据库密码

例如

java -jar zipkin-server-2.23.15-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456 &

3). 指定的数据库建表语句

CREATE TABLE IF NOT EXISTS zipkin_spans (
                                            `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
                                            `trace_id` BIGINT NOT NULL,
                                            `id` BIGINT NOT NULL,
                                            `name` VARCHAR(255) NOT NULL,
                                            `remote_service_name` VARCHAR(255),
                                            `parent_id` BIGINT,
                                            `debug` BIT(1),
                                            `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
                                            `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
                                            PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
                                                  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
                                                  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
                                                  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
                                                  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
                                                  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
                                                  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
                                                  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
                                                  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
                                                  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
                                                  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
                                                  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
                                                   `day` DATE NOT NULL,
                                                   `parent` VARCHAR(255) NOT NULL,
                                                   `child` VARCHAR(255) NOT NULL,
                                                   `call_count` BIGINT,
                                                   `error_count` BIGINT,
                                                   PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

(8). Sleuth

# 设置采样精度, 默认为0.1, 即10%, 这⾥配置1, 是记录全部的sleuth信息
spring.sleuth.sampler.probability=1.0

(9). Gateway

# 开启⽹关拉取nacos的服务
spring.cloud.gateway.discovery.locator.enabled=true
# 路由唯⼀标识
spring.cloud.gateway.routes[0].id=product-service
# 下面的配置以数组方式定义
# 从nacos获取名称转发, lb是负载均衡轮训策略
spring.cloud.gateway.routes[0].uri=lb://order-service
# 优先级,数字越⼩优先级越⾼
spring.cloud.gateway.routes[0].order=1
# 配置转发
spring.cloud.gateway.routes[0].predicates=Path=/order-gateway/**
# 过滤器:去掉第⼀层前缀
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

7. 依赖Jar包

(1). Jar下载

| nacos|sentinel|zipkin

|–|–|—|—|–|—|–|–|–|

|nacos 1.3.2|sentinel 1.8.0|zipkin 2.23.15

springcloud 搭建父依赖 springcloud maven依赖_spring cloud_26

(2). Jar启动

java -jar xxx.jar

   当前窗口被锁定, 关闭窗口或者打断程序, 程序退出

java -jar xxx.jar &

   当前窗口不锁定, 系统关闭时或终止程序时, 程序退出

nohup java -jar xxx.jar &

   后台运行程序, 系统关闭时, 程序退出

8. docker 镜像

启动docker

systemctl start docker

拉取

docker pull 镜像仓库:版本号

(1). nacos

拉取

docker pull nacos/nacos-server

启动

docker run --env MODE=standalone --name nacos -d -p 8848:8848 镜像ID

(2). sentinel

拉取

docker pull bladex/sentinel-dashboard:latest

启动

docker run --name sentinel -d -p 8858:8858 镜像ID

(3). zipkin

拉取

docker pull openzipkin/zipkin:latest

启动

docker run --name zipkin -d -p 9411:9411 镜像id

指定数据库启动

docker run --name zipkin -d -p 9411:9411 镜像id --STORAGE_TYPE=mysql --MYSQL_HOST=数据库ip地址 --MYSQL_TCP_PORT=端口号 --MYSQL_DB=数据库名称 --MYSQL_USER=账号 --MYSQL_PASS=密码

(4). 注意

在服务器上启动镜像后, 需要开放对应的端口, 否则是访问不了的