maven搭建spring-cloud项目

一、首先在父工程中引入springcloud

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

设置jdk编译的版本

	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <!--编译版本-->
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

二、子工程中引入spring-boot-web

	<properties>
        <spring.boot-version>2.3.12.RELEASE</spring.boot-version>
    </properties>
    <dependencies>
         <!--注意:由于 spring-boot-starter-web 默认替我们引入了核心启动器 spring-boot-starter,
        因此,当 Spring Boot  项目中的 pom.xml 引入了 spring-boot-starter-web 的依赖后,
        就无须在引入 spring-boot-starter 核心启动器的依赖了-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

三、编写启动类

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class , args);
    }
}

注意:springcloud和springboot的版本对应

### 如何搭建 Spring Cloud Alibaba 微服务框架 #### 准备工作 为了成功构建基于 Spring Cloud Alibaba 的微服务体系,需先安装并配置好 Java 开发环境以及 Maven 或 Gradle 构建工具。确保 JDK 版本不低于 8u45 及以上版本[^1]。 #### 创建父项目结构 通过 IDE (如 IntelliJ IDEA) 新建一个多模块 Maven 工程作为整个项目的根目录,在 `pom.xml` 中引入必要的依赖管理: ```xml <dependencyManagement> <dependencies> <!-- 统一管理 spring cloudspring boot 版本 --> <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> ``` #### 添加子模块 针对不同业务功能创建多个独立的服务提供者与消费者应用,每个都应继承自上述定义好的 parent POM 文件,并按需添加特定组件的支持,比如 Nacos 注册中心、Sentinel 流控规则引擎等。 对于 Web 应用场景,默认提供了对 WebServlet、WebFlux 等多种协议栈的支持;而对于远程调用,则集成了 OpenFeign 客户端和服务发现机制,简化了分布式系统的开发流程。 #### 配置文件设置 在各个子模块下的 resources 路径下放置 application.yml 或 properties 形式的配置项,用于指定诸如服务器端口、数据库连接字符串以及其他第三方中间件参数等内容。特别注意要开启 Eureka/Nacos 自动注册特性以便于后续集群部署时能够自动感知其他成员的存在状态变化情况。 #### 启动类编写 最后一步是在每一个应用程序入口处标注 @EnableDiscoveryClient 注解以激活服务治理能力,并实现具体的业务逻辑处理方法即可完成基本版的 Spring Cloud Alibaba 平台建设工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值