搭建SpringCloudAlibaba工程_基于SpringBoot3.+

首先搭建Maven私服,参考我另一篇博客

新建父工程project,删除所有文件,仅保留pom.xml,并做出如下修改,可直接复制粘贴,切记调整你为自己项目的groupIdartifactId

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

    <groupId>org.ashe</groupId>
    <artifactId>base</artifactId>
    <!-- 不允许上传SNAPSHOT快照版本 -->
    <version>1.0.0</version>

    <name>spring-cloud</name>
    <description>For Study SpringCloud Alibaba</description>
    <!-- Maven打包为pom而非jar -->
    <packaging>pom</packaging>

    <properties>
        <!-- java -->
        <java.version>17</java.version>

        <!-- Spring Cloud Alibaba -->
        <spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>

        <!-- Spring Cloud -->
        <spring.cloud.version>2022.0.0</spring.cloud.version>

        <!-- Spring Boot -->
        <spring-boot.version>3.0.2</spring-boot.version>

    </properties>

    <!-- dependencyManagement管理依赖后,子模块的pom.xml若想要集成父工程的依赖,必须显式地申明 -->
    <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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </build>
    <distributionManagement>
        <!-- mvn deploy 上传到指定仓库(Maven私服) -->
        <repository>
            <!-- 这里的id要与你settings.xml文件中保持一致 -->
            <id>ashe</id>
            <url>http://服务器ip:8081/repository/private-maven/</url>
        </repository>
    </distributionManagement>

</project>

版本参考 版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

mvn clean  mvn deploy  将pom打包上传至Maven私服,以便子工程可以成功引入依赖。

 

新建子工程project,然后在pom.xml中依赖于父工程的pom

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

    <parent>
        <groupId>org.ashe</groupId>
        <artifactId>base</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>order</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

</project>

先启动nacos,再启动子工程项目

微服务雏形,搭建成功!至此我们可以明白:微服务其实就是一群SpringBoot应用,注册到Nacos,发起请求统一通过Gateway入口,由Loadbalancer做多节点之间的负载均衡,集成OpenFeign方便服务内部之间调用,Sentinel则是对流量进行实时监控和控制。

SpringCloud只是将若干SpringBoot组合在一起,应对高并发带来的一系列问题。相对网上各种PPT的术语,这样你是不是更容易理解呢?

posted @ 2023-08-21 20:57  Ashe|||^_^  阅读(142)  评论(0编辑  收藏  举报