当前位置: 首页 > news >正文

Spring Cloud Alibaba-(1)搭建项目环境

Spring Cloud Alibaba-(1)搭建项目环境

Spring Cloud Alibaba-(2)Nacos【服务注册与发现、配置管理】

Spring Cloud Alibaba-(3)OpenFeign【服务调用】

1.Spring Cloud Alibaba(官网:https://sca.aliyun.com/)

Spring Cloud Alibaba 是阿里巴巴结合自身丰富的微服务实践而推出的微服务开发的一站式解决方案,是 Spring Cloud 第二代实现的主要组成部分。吸收了 Spring Cloud Netflix 微服务框架的核心架构思想,并进行了高性能改进。自 Spring Cloud Netflix 进入停更维护后,Spring Cloud Alibaba 逐渐代替它成为主流的微服务框架。

2.版本适配(SpringBoot 3 项目 JDK 版本至少是17)

3.创建父工程--管理微服务和依赖

3.1 创建

3.2 父工程 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.dragon</groupId><artifactId>springcloud</artifactId><version>0.0.1-SNAPSHOT</version><name>springcloud</name><description>springcloud</description><packaging>pom</packaging><modules><module>order-service</module><module>gateway</module><module>common</module><module>user-service</module><module>product-service</module></modules><properties><java.version>21</java.version><spring-cloud.version>2023.0.1</spring-cloud.version><spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version><rocketmq.version>2.3.1</rocketmq.version><rocketmq-client.version>5.1.4</rocketmq-client.version><mysql-connector.version>8.3.0</mysql-connector.version><mybatis-plus.version>3.5.6</mybatis-plus.version><mybatis-spring.version>3.0.3</mybatis-spring.version><mybatis-plus-join.version>1.4.12</mybatis-plus-join.version><fastjson2.version>2.0.48</fastjson2.version><springdoc-openapi.version>2.5.0</springdoc-openapi.version><sa-token.version>1.38.0</sa-token.version></properties><!-- 父工程依赖版本管理 --><dependencyManagement><dependencies><!-- 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><!-- rocketmq --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>${rocketmq.version}</version></dependency><!-- rocketmq-client --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>${rocketmq-client.version}</version></dependency><!-- mysql 驱动 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>${mysql-connector.version}</version></dependency><!-- mybatis-plus-boot-starter 中 mybatis-spring 版本不够,排除之后引入新版本 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><!-- mybatis-plus 多表查询 --><dependency><groupId>com.github.yulichang</groupId><artifactId>mybatis-plus-join-boot-starter</artifactId><version>${mybatis-plus-join.version}</version></dependency><!-- fastjson2 处理 json 数据 --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson2.version}</version></dependency><!-- 使用 springdoc 生成 swagger 文档  --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>${springdoc-openapi.version}</version></dependency><!-- Sa-Token --><dependency><groupId>cn.dev33</groupId><artifactId>sa-token-spring-boot3-starter</artifactId><version>${sa-token.version}</version></dependency></dependencies></dependencyManagement><!-- 父工程依赖 --><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><!-- 热部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

4.创建微服务

4.1 公共微服务

4.1.1 创建

4.1.2 目录结构

4.1.3 订单、产品、用户实体类

package com.dragon.entity;import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;import java.io.Serializable;
import java.time.LocalDateTime;@Data
@Schema(name = "Orders", description = "订单表")
public class Orders implements Serializable {private static final long serialVersionUID = 1L;@Schema(description = "主键")@TableId(value = "id", type = IdType.ASSIGN_UUID)private String id;@Schema(description = "用户ID")private String userId;@Schema(description = "产品ID")private String productId;@Schema(description = "数量")private Integer amount;@Schema(description = "单价")private Double price;@Schema(description = "订单总额")private Double total;@Schema(description = "备注")private String remark;@Schema(description = "创建人ID")
//    @TableField(fill = FieldFill.INSERT)private String creatorId;@Schema(description = "创建时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;@Schema(description = "更新时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime ts;@Schema(description = "逻辑删除字段 0.正常 1.删除")@TableLogicprivate Integer deleteFlag;
}
package com.dragon.entity;import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;import java.io.Serializable;
import java.time.LocalDateTime;@Data
@Schema(name = "Product", description = "产品表")
public class Product implements Serializable {private static final long serialVersionUID = 1L;@Schema(description = "主键")@TableId(value = "id", type = IdType.ASSIGN_UUID)private String id;@Schema(description = "产品名")private String name;@Schema(description = "采购价")private Double price;@Schema(description = "售价")private Double sellPrice;@Schema(description = "库存")private Integer stock;@Schema(description = "备注")private String remark;@Schema(description = "创建人ID")
//    @TableField(fill = FieldFill.INSERT)private String creatorId;@Schema(description = "创建时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;@Schema(description = "更新时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime ts;@Schema(description = "逻辑删除字段 0.正常 1.删除")@TableLogicprivate Integer deleteFlag;
}
package com.dragon.entity;import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;@Data
@Schema(name = "User", description = "用户表")
public class User implements Serializable {private static final long serialVersionUID=1L;@Schema(description = "主键")@TableId(value = "id", type = IdType.ASSIGN_UUID)private String id;@Schema(description = "用户名")private String username;@Schema(description = "密码")@JsonIgnoreprivate String password;@Schema(description = "姓名")private String name;@Schema(description = "邮箱")@Emailprivate String email;@Schema(description = "头像地址")private String avatarUrl;@Schema(description = "创建人ID")
//    @TableField(fill = FieldFill.INSERT)private String creatorId;@Schema(description = "创建时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;@Schema(description = "更新时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime ts;@Schema(description = "逻辑删除字段 0.正常 1.删除")@TableLogicprivate Integer deleteFlag;
}

4.2 订单微服务

4.2.1 创建

4.2.2 目录结构

4.2.3 bootstrap.yml

server:port: 8000spring:profiles:active: publicapplication:name: order-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***

4.2.4 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.dragon</groupId><artifactId>springcloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><name>order-service</name><artifactId>order-service</artifactId><properties><java.version>21</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 自定义 common --><dependency><groupId>com.dragon</groupId><artifactId>common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- nacos-discovery 服务发现 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos-config 配置中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- openfeign 服务调用 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sentinel 限流降级 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!-- seata 分布式事务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId></dependency><!-- loadbalancer 负载均衡 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><!-- sentinel 规则保存到 nacos 配置中心 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency><!-- rocketmq --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId></dependency></dependencies>
</project>

4.3 产品微服务

4.3.1 创建

4.3.2 目录结构

4.3.3 bootstrap.yml

server:port: 8400spring:application:name: product-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***

4.3.4 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.dragon</groupId><artifactId>springcloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>product-service</artifactId><packaging>jar</packaging><name>product-service</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 自定义 common --><dependency><groupId>com.dragon</groupId><artifactId>common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- seata 分布式事务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId></dependency><!-- loadbalancer 负载均衡 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency></dependencies>
</project>

4.4 用户微服务

4.4.1 创建

4.4.2 目录结构

4.4.3 bootstrap.yml

server:port: 8300spring:application:name: user-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***

4.4.4 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.dragon</groupId><artifactId>springcloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>user-service</artifactId><packaging>jar</packaging><name>user-service</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 自定义 common --><dependency><groupId>com.dragon</groupId><artifactId>common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- rocketmq --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.3.1</version></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>5.3.0</version></dependency></dependencies>
</project>

http://www.mrgr.cn/news/30132.html

相关文章:

  • 3216. 交换后字典序最小的字符串
  • Webpack 1.13.2 执行 shell 命令解决 打印时没有背景色和文字颜色的问题
  • 为什么卷积现在不火了:CNN研究热度降温的深层原因分析
  • scala的练习题
  • spring cloud 入门笔记1(RestTemplate,Consul)
  • GitLab 中文发行版最新版重点功能解读
  • 光控资本:沪指涨0.59%,酿酒板块大幅拉升,数字货币概念等活跃
  • java操作邮件带附件发送
  • Salesforce逆袭老大哥SAP
  • 9 个个性化电子邮件签名示例,展示您的独特声音
  • 公益入理塘,爱尔眼科“专科联盟”挂牌
  • YOLOv9改进策略【卷积层】| AKConv: 具有任意采样形状和任意参数数量的卷积核
  • 雷朋太阳镜和AEG的制胜法宝是:音乐节以及数据驱动的品牌推广
  • NEXT.js 创建postgres数据库-关联github项目-连接数据库-在项目初始化数据库的数据
  • 图数据归一化
  • 【GEE中水体提取的水体指数法】
  • 第160天:安全开发-Python-蓝队项目流量攻击分析文件动态监控Webshell检测
  • ROS学习笔记13——rosbag功能包的简单使用
  • Spark-累加器源码分析
  • 图神经网络池化方法
  • miniconda环境配置
  • Java基础笔记:Java基础语法
  • 企业建站如何选择服务器配置?
  • SOMEIP_ETS_117: SD_Entry_references_options_of_same_kind
  • Redis淘汰策略与持久化
  • 一篇文章教你搞定缓存三兄弟(缓存雪崩、缓存击穿、缓存穿透)