Bootstrap

SpringCloudAlibaba框架搭建

SpringCloudAlibaba说明

主要讲解SpringCloudAlibaba核心组件 Nacos(服务注册与发现和分布式配置中心)、Sentinel(服务保护框架)、Seata(分布式事务解决框架)、阿里云OSS、Alibaba Cloud SchedulerX等。

SpringCloudAlibaba中文社区地址:

https://github.com/alibaba/spring-cloud-alibaba/blob/2.2.x/README-zh.md


项目结构图

springcloud-parent

|----ophiux-nacos      注册中心和配置中心

|----ophiux-gateway        服务网关

|----ophiux-base-api        微服务feign api和实体类

|---------ophiux-member-api       

|---------ophiux-order-api

|---------ophiux-product-api

|----ophiux-cloud-module

|----------ophiux-member        会员服务

|----------ophiux-order              订单服务

|----------ophiux-product          商品服务

|----ophiux-common
|----------ophiux-common-core                核心类
|----------ophiux-common-data                数据

|----------ophiux-common-log                   日志
|----------ophiux-common-swagger        swagger工具类封装
|----------ophiux-common-tools               工具类

新建父级项目springcloud-parent

Maven  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.0</modelVersion>
    <groupId>com.ophiux</groupId>
    <artifactId>springcloud-parent</artifactId>
    <version>1.0.0</version>

    <packaging>pom</packaging>

    <modules>
        <module>ophiux-nacos</module>
        <module>ophiux-common</module>
        <module>ophiux-cloud-module</module>
        <module>ophiux-gateway</module>
        <module>ophiux-boot-start</module>
        <module>ophiux-base-api</module>
    </modules>

    <!--<repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
    </repositories>-->

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>

        <spring-boot.version>2.5.5</spring-boot.version>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
        <!-- <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version> -->

		<seata.version>1.5.1</seata.version>

        <fastjson.version>1.2.83</fastjson.version>
        <mybatis-plus.version>3.4.3.2</mybatis-plus.version>
        <!--xxl-job在使用-->
        <mybatis-spring-boot-starter.version>2.2.0</mybatis-spring-boot-starter.version>
        <mysql-connector.version>8.0.24</mysql-connector.version>
        <druid.version>1.2.8</druid.version>
        <swagger3.version>3.0.0</swagger3.version>
        <knife4j.version>3.0.3</knife4j.version>
        <modelmapper.version>2.3.0</modelmapper.version>
        <redisson.version>3.13.6</redisson.version>
        <xxl-job-core.version>2.3.0</xxl-job-core.version>
        <hutool-all.version>5.8.4</hutool-all.version>
        <commons-lang3.version>3.12.0</commons-lang3.version>

    </properties>

    <dependencies>
        <!--bootstrap 启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <!--配置文件处理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </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>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

    </dependencies>


    <dependencyManagement>
        <dependencies>
            <!--spring boot 依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.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>


            <!--工具包-->
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-common-tools</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!--核心jar-->
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-common-core</artifactId>
                <version>${project.version}</version>
            </dependency>

            <!--swagger-->
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-common-swagger</artifactId>
                <version>${project.version}</version>
            </dependency>


            <!-- 业务Feign -->
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-order-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-member-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-product-api</artifactId>
                <version>${project.version}</version>
            </dependency>


            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-member</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.ophiux</groupId>
                <artifactId>ophiux-order</artifactId>
                <version>${project.version}</version>
            </dependency>



            <!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <!-- model转化 -->
            <dependency>
                <groupId>org.modelmapper</groupId>
                <artifactId>modelmapper</artifactId>
                <version>${modelmapper.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
                <optional>true</optional>
            </dependency>

			
			<!-- seata分布式事务-->
			<dependency>
				<groupId>io.seata</groupId>
				<artifactId>seata-spring-boot-starter</artifactId>
				<version>${seata.version}</version>
			</dependency>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool-all.version}</version>
            </dependency>



        </dependencies>
    </dependencyManagement>


    <profiles>
        <!--使用 mvn clean package -Pprod 进行激活prod环境的配置,否则激活默认环境-->
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <profiles.active>dev</profiles.active>
                <!--https://github.com/alibaba/nacos/issues/3460  默认的命名空间不要去设置namespace -->
                <nacos.namespace/>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
                <nacos.namespace>test</nacos.namespace>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
                <nacos.namespace>prod</nacos.namespace>
            </properties>
        </profile>
    </profiles>
    <build>
        <resources>
            <resource>
                <!-- 替换application.yml中 @artifactId@ -->
                <directory>src/main/resources</directory>
                <!--替换掉resource下的占位符-->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

Nacos整合

新建ophiux-nacos子项目(nacos项目启动形式)

maven仓库还没有上传最新依赖jar,这个暂时不做使用,使用客户端方式启动

Maven 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <artifactId>ophiux-nacos</artifactId>
    <packaging>jar</packaging>
    <name>ophiux-nacos</name>
    <description>nacos 注册配置中心</description>

    <parent>
        <groupId>com.ophiux</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <properties>
        <nacos.groupId>com.pig4cloud.nacos</nacos.groupId>
        <!-- maven仓库找不到2.1.0的jar,用2.0.4代替 -->
        <nacos.version>2.0.4</nacos.version>
        <hutool.version>5.7.15</hutool.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>${nacos.groupId}</groupId>
            <artifactId>nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>${nacos.groupId}</groupId>
            <artifactId>nacos-naming</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>${nacos.groupId}</groupId>
            <artifactId>nacos-istio</artifactId>
            <version>${nacos.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

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

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-system</artifactId>
            <version>${hutool.version}</version>
        </dependency>

    </dependencies>



</project>

新建配置文件bootstrap.yml

db:
  num: 1
  user: root
  password: root
  url:
    0: jdbc:mysql://127.0.0.1:3306/nacos?serverTimezone=UTC&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true



nacos:
  core:
    auth:
      enabled: false
        userAgentAuthWhite: false
      system.type: nacos
      default.token.secret.key: SecretKey012345678901234567890123456789012345678901234567890123456789
  security:
    ignore:
      urls: /,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**


spring:
  application:
    name: @project.artifactId@
  datasource:
    platform: mysql  #这个过期属性不能修改,nacos 代码对此有硬编码
  security:
    enabled: true

useAddressServer: true

management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    export:
      influx:
        enabled: false
      elastic:
        enabled: false

https://github.com/alibaba/nacos.git 下的console模块下的源码拷贝到项目中,作为nacos启动。也可以直接使用nacos客户端。

nacos启动

从nacos官方网站下载客户端2.1.0 windows版本

 https://github.com/alibaba/nacos/releases/tag/2.1.0 

新建数据库nacos,然后将nacos-mysql.sql中的sql在数据库初始化。

修改nacos/conf下的application.properties 配置文件,直连数据库,然后直接运行。

访问地址:localhost:8848/nacos
默认账号密码:nacos/nacos

  源码请查阅:https://gitee.com/hhlhhl/springcloud-parent.git

;