媒介
什么是SpringCloud?
Spring Cloud 是一系列框架的有序聚集,它使用 Spring Boot 的开发便利性简化了分布式系统的开发,比如服务注册、服务发现、网关、路由、链路追踪等。Spring Cloud 并不是重复造轮子,而是将市面上开发得比较好的模块集成进去,进行封装,从而减少了各模块的开发成本。也可以说Spring Cloud 提供了构建分布式系统所需的“全家桶”。
SpringCloud的优缺点:
优点:
- 简化开发: 提供了丰富的组件和模式,可以快速开发分布式系统。
- 微服务支持: 支持微服务架构,提供了服务注册与发现、负载均衡等功能。
- 分布式配置管理: 提供了分布式配置管理的本领,可以动态管理配置信息。
- 监控与管理: 提供了监控、管理和调优的工具,方便运维管理和故障排查。
- 生态系统: 有庞大的社区支持和活跃的开发,生态系统美满。
缺点:
配置繁多且复杂,体量越大,摆设治理成本越高,倒霉于维护,对团队挑战大
版本兼容性:必要留意 Spring Cloud 各个组件的版本兼容性,不同版本之间大概存在不兼容的情况。
1.SpringCloud版本选型
确定使用SpringCloud-alibaba的版本,这里选择(2021.0.4.0)
2.选择SpringCloud、SpringBoot版本
- https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
- 从上边网页中找到 Springcloud-alibaba 2021.0.4.0 所对应的版本 ,如下图:
- 这里使用的JDK 为 11.0.18
- Spring Cloud Version 对应的版本为 Spring Cloud 2021.0.4
- Spring Boot Version 对应的版本为 2.6.11
复制代码
3.选好版本,开始搭建SpringCloud项目
最好的训练就是动手实践,接下来我们要创建一个父工程,管理下边的不同服务(不同业务放到不同的服务中去,各个服务相互调用)。全程使用Maven进行搭建。
一. 构建父工程
构建父工程, 点击 File -> New -> Project
二. 创建项目
选择创建Maven项目,选择JDK ,点击Next
三. 填写相干名称
填写 Name、 GroupId、ArtifactId、Version ,填写完成,点击Finish
四. 配置父工程pom.xml
项目的父工程已经建好了,根据我们前边选择的版本依赖配置父工程pom.xml
父工程不必要写业务代码,可以删除src文件目次和没用的文件。
五. 创建子模块(服务)
完成上述步调,我们的父工程就已经搭建完成了。接下来就可以继承搭建其他的微服务工程了。首先搭建一个公共模块,为其他服务提供公共服务。
选中父工程,右键点击 New -> Module
六. 选择Maven ,JDK
这里与构建父工程一致,选择Maven,选择本身本机安装的JDK ,点击Next
七. 填写模块服务名称
填写子模块服务名称, 点击Finish 。
到这里我们就创建好一个common父服务了。
八. 新建 cloud-common-core模块
在common下边新建cloud-common-core模块
和上述创建模块一样 New -> Module
选择Maven ,JDK
填写模块名称,父模块为cloud-common
将父工程(四. 配置父工程pom.xml)声明的依赖添加到cloud-common-core的pom.xml中九. 增长MybatisPlusConfig配置
项目中使用的是MybatisPlus,所以在cloud-common-core增长MybatisPlusConfig配置- package com.waves.core.config;
- import com.baomidou.mybatisplus.annotation.DbType;
- import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * @author waves
- * @date 2024/4/24 17:06
- */
- @Configuration
- public class MybatisPlusConfig {
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- // 添加分页插件
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
- // 添加执行分析插件
- interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
- // 添加乐观锁插件
- interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
- return interceptor;
- }
- }
复制代码 十. 封装公共响应体
在cloud-common-core封装公共响应体,
创建响应码常量- /**
- * 返回状态码
- *
- * @author waves
- * @date 2024/4/24 17:06
- */
- public class HttpStatus {
- /** 操作成功 */
- public static int SUCCESS = 100;
- /** 警告消息 */
- public static int WARM = 101;
- /** 操作失败 */
- public static int ERROR = 103;
- }
复制代码 创建ResponseResult- package com.waves.core.web.domain;
- import com.waves.core.constant.HttpStatus;
- import java.util.HashMap;
- import java.util.Objects;
- /**
- * @author waves
- * @date 2024/4/24 17:28
- */
- public class ResponseResult extends HashMap<String, Object> {
- private static final long serialVersionUID = 1L;
- /** 状态码 */
- public static final String CODE_TAG = "code";
- /** 返回内容 */
- public static final String MSG_TAG = "message";
- /** 数据对象 */
- public static final String DATA_TAG = "data";
- /** 无参构造对象 初始化一个对象,表示一个空消息 */
- public ResponseResult(){
- }
- /**
- * 初始化一个新创建的 ResponseResult 对象
- *
- * @param code 状态码
- * @param msg 返回内容
- */
- public ResponseResult(int code, String msg)
- {
- super.put(CODE_TAG, code);
- super.put(MSG_TAG, msg);
- }
- /**
- * 初始化一个新创建的 ResponseResult 对象
- *
- * @param code 状态码
- * @param msg 返回内容
- * @param data 数据对象
- */
- public ResponseResult(int code, String msg, Object data)
- {
- super.put(CODE_TAG, code);
- super.put(MSG_TAG, msg);
- if (Objects.nonNull(data))
- {
- super.put(DATA_TAG, data);
- }
- }
- /**
- * 返回成功消息
- *
- * @return 成功消息
- */
- public static ResponseResult success()
- {
- return ResponseResult.success("操作成功");
- }
- /**
- * 返回成功数据
- *
- * @return 成功消息
- */
- public static ResponseResult success(Object data)
- {
- return ResponseResult.success("操作成功", data);
- }
- /**
- * 返回成功消息
- *
- * @param msg 返回内容
- * @return 成功消息
- */
- public static ResponseResult success(String msg)
- {
- return ResponseResult.success(msg, null);
- }
- /**
- * 返回成功消息
- *
- * @param msg 返回内容
- * @param data 数据对象
- * @return 成功消息
- */
- public static ResponseResult success(String msg, Object data)
- {
- return new ResponseResult(HttpStatus.SUCCESS, msg, data);
- }
- /**
- * 返回警告消息
- *
- * @param msg 返回内容
- * @return 警告消息
- */
- public static ResponseResult warn(String msg)
- {
- return ResponseResult.warn(msg, null);
- }
- /**
- * 返回警告消息
- *
- * @param msg 返回内容
- * @param data 数据对象
- * @return 警告消息
- */
- public static ResponseResult warn(String msg, Object data)
- {
- return new ResponseResult(HttpStatus.WARN, msg, data);
- }
- /**
- * 返回错误消息
- *
- * @return 错误消息
- */
- public static ResponseResult error()
- {
- return ResponseResult.error("操作失败");
- }
- /**
- * 返回错误消息
- *
- * @param msg 返回内容
- * @return 错误消息
- */
- public static ResponseResult error(String msg)
- {
- return ResponseResult.error(msg, null);
- }
- /**
- * 返回错误消息
- *
- * @param msg 返回内容
- * @param data 数据对象
- * @return 错误消息
- */
- public static ResponseResult error(String msg, Object data)
- {
- return new ResponseResult(HttpStatus.ERROR, msg, data);
- }
- /**
- * 返回错误消息
- *
- * @param code 状态码
- * @param msg 返回内容
- * @return 错误消息
- */
- public static ResponseResult error(int code, String msg)
- {
- return new ResponseResult(code, msg, null);
- }
- /**
- * 是否为成功消息
- *
- * @return 结果
- */
- public boolean isSuccess()
- {
- return Objects.equals(HttpStatus.SUCCESS, this.get(CODE_TAG));
- }
- /**
- * 是否为警告消息
- *
- * @return 结果
- */
- public boolean isWarn()
- {
- return Objects.equals(HttpStatus.WARN, this.get(CODE_TAG));
- }
- /**
- * 是否为错误消息
- *
- * @return 结果
- */
- public boolean isError()
- {
- return Objects.equals(HttpStatus.ERROR, this.get(CODE_TAG));
- }
- /**
- * 方便链式调用
- *
- * @param key
- * @param value
- * @return
- */
- @Override
- public ResponseResult put(String key, Object value)
- {
- super.put(key, value);
- return this;
- }
- }
复制代码 十一. 创建支付服务
与创建cloud-common同理创建cloud-provider-payment服务
在cloud-provider-payment模块的pom.xml中引入cloud-common-core,spring-boot-starter-web 的依赖
- <?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">
- <parent>
- <artifactId>wavesservice</artifactId>
- <groupId>com.waves</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>cloud-provider-payment</artifactId>
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.waves</groupId>
- <artifactId>cloud-common-core</artifactId>
- </dependency>
- </dependencies>
- </project>
复制代码 十二.编写支付代码和接口
1. 编写bootstrap.yml配置文件
因为使用的时MybatisPlus,只MybatisPlus配置,不必要配置Mybatis- # 服务端口
- server:
- port: 8088
- servlet:
- # 服务访问路径
- context-path: /paymentservice
- spring:
- application:
- # 服务名称
- name: paymentservice
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driverClassName: com.mysql.cj.jdbc.Driver
- druid:
- url: jdbc:mysql://localhost:3306/waves_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
- username: root
- password: root
- #mybatis:
- #配置mapper的扫描,找到所有的mapper:xml映射文件:
- # mapper-locations: classpath:mapper/*.xml
- # 搜索指定包别名
- # type-aliases-package: com.waves.**.domain
- mybatis-plus:
- # 不支持多包, 如有需要可在注解配置 或 提升扫包等级
- # 例如 com.**.**.mapper
- mapperPackage: com.waves.**.mapper
- # 对应的 XML 文件位置
- mapperLocations: classpath*:mapper/**/*Mapper.xml
- # 实体扫描,多个package用逗号或者分号分隔
- typeAliasesPackage: com.waves.**.domain
- # 启动时是否检查 MyBatis XML 文件的存在,默认不检查
- checkConfigLocation: false
- configuration:
- # 自动驼峰命名规则(camel case)映射
- mapUnderscoreToCamelCase: false
- # MyBatis 自动映射策略
- # NONE:不启用 PARTIAL:只对非嵌套 resultMap 自动映射 FULL:对所有 resultMap 自动映射
- autoMappingBehavior: PARTIAL
- # MyBatis 自动映射时未知列或未知属性处理策
- # NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息
- autoMappingUnknownColumnBehavior: NONE
- # 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl
- # 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl
- # 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpl
- logImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl
- global-config:
- # 是否打印 Logo banner
- banner: true
- dbConfig:
- # 主键类型
- # AUTO 自增 NONE 空 INPUT 用户输入 ASSIGN_ID 雪花 ASSIGN_UUID 唯一 UUID
- idType: ASSIGN_ID
- # 逻辑已删除值
- logicDeleteValue: 2
- # 逻辑未删除值
- logicNotDeleteValue: 0
- # 字段验证策略之 insert,在 insert 的时候的字段验证策略
- # IGNORED 忽略 NOT_NULL 非NULL NOT_EMPTY 非空 DEFAULT 默认 NEVER 不加入 SQL
- insertStrategy: NOT_NULL
- # 字段验证策略之 update,在 update 的时候的字段验证策略
- updateStrategy: NOT_NULL
- # 字段验证策略之 select,在 select 的时候的字段验证策略既 wrapper 根据内部 entity 生成的 where 条件
- where-strategy: NOT_NULL
- # 日志配置
- logging:
- pattern:
- dateformat: MM-dd HH:mm:ss:SSS
- level:
- com.waves: debug
- org.springframework: warn
复制代码 2.编写Payment支付服务的启动类
- package com.waves.payment;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.ComponentScan;
- /**
- * @author waves
- * @date 2024/4/24 22:39
- */
- @SpringBootApplication
- @MapperScan("${mybatis-plus.mapperPackage}") // 配置要扫描的包
- //@ComponentScan(basePackages = {"com.waves.core", "com.waves.payment"}) //配置要扫描的组件和配置
- @ComponentScan("com.waves")
- public class PaymentApplication {
- public static void main(String[] args) {
- SpringApplication.run(PaymentApplication.class,args);
- System.out.println("支付服务启动成功 (^-^)V。。。。");
- }
- }
复制代码 3.建表
- #创建sys_payment_type 支付类型表
- CREATE TABLE `sys_payment_type` (
- `pay_id` bigint(20) NOT NULL COMMENT '支付类型主键',
- `pay_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '支付类型名称',
- `remark` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '备注',
- PRIMARY KEY (`pay_id`) USING BTREE
- ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC;
- #插入数据
- INSERT INTO `sys_payment_type` (`pay_id`, `pay_name`, `remark`) VALUES (1, '支付宝', '使用支付宝进行支付');
- INSERT INTO `sys_payment_type` (`pay_id`, `pay_name`, `remark`) VALUES (2, '微信支付', '使用微信支付进行支付');
- INSERT INTO `sys_payment_type` (`pay_id`, `pay_name`, `remark`) VALUES (3, '银行转账', '通过银行转账进行支付');
复制代码 4.编写接口
在cloud-common-core 中编写 payment实体类- package com.waves.core.web.domain.entities;
- import com.baomidou.mybatisplus.annotation.IdType;
- import com.baomidou.mybatisplus.annotation.TableField;
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import lombok.experimental.Accessors;
- import java.io.Serializable;
- /**
- * 支付类型实体 如果进行网络通信 一定要实现序列化
- * @author waves
- * @date 2024/4/25 9:44
- */
- @Data //: 该注解会自动生成类的 getter、setter、equals、hashCode 和 toString 方法,从而减少样板代码。
- @NoArgsConstructor //: 自动生成一个无参构造方法,用于创建对象实例时不需要传入任何参数。
- @AllArgsConstructor //: 自动生成一个包含所有参数的构造方法,用于创建对象实例时需要传入所有字段的值。
- @Accessors(chain = true) //: 启用方法链(method chaining),允许在对象实例上连续调用多个 setter 方法,可以使代码更加简洁。
- @TableName("sys_payment_type") //:对应数据库表的名称
- public class SysPaymentType implements Serializable {
- /**
- * 支付Id
- * TableId 加在表主键上面 value 对应 字段名 ,type 为数据库ID自增
- */
- @TableId(value = "pay_id", type = IdType.AUTO)
- private Long payId;
- /**
- * 支付类型名称
- * TableField value 对应 字段名
- */
- @TableField(value = "pay_name")
- private String payName;
- /**
- * 备注
- */
- @TableField(value = "remark")
- private String remark;
- }
复制代码
mapper
SysPaymentMapper继承MybatisPlus 的 BaseMapper- package com.waves.payment.web.mapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.waves.core.web.domain.entities.SysPaymentType;
- /**
- * @author waves
- * @date 2024/4/25 9:44
- */
- @Mapper
- public interface SysPaymentMapper extends BaseMapper<SysPaymentType> {
- }
复制代码 Service
ISysPaymentService 继承MybatisPlus 的 IService- package com.waves.payment.web.service;
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.waves.core.web.domain.entities.SysPaymentType;
- /**
- * @author waves
- * @date 2024/4/25 9:44
- */
- @Service
- public interface ISysPaymentService extends IService<SysPaymentType> {
- }
复制代码 ServiceImpl
SysPaymentServiceImpl继承 继承MybatisPlus 的 IService 的 ServiceImpl 同时实现 我们本身的ISysPaymentService- package com.waves.payment.web.service.impl;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.waves.core.web.domain.entities.SysPaymentType;
- import com.waves.payment.web.mapper.SysPaymentMapper;
- import com.waves.payment.web.service.ISysPaymentService;
- import org.springframework.stereotype.Service;
- /**
- * @author waves
- * @date 2024/4/25 9:44
- */
- @Service
- public class SysPaymentServiceImpl extends ServiceImpl<SysPaymentMapper,SysPaymentType> implements ISysPaymentService {
- }
复制代码 Controller
- package com.waves.payment.web.controller;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.waves.core.web.domain.entities.SysPaymentType;
- import com.waves.payment.web.service.ISysPaymentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.Objects;
- /**
- * @author waves
- * @date 2024/4/25 9:44
- */
- @RestController
- @RequestMapping("/payment")
- public class SysPaymentController {
- @Autowired
- private ISysPaymentService paymentService;
- /**
- * 分页查询PaymentType,条件 payId ,payName
- * @return
- */
- @GetMapping("/listPage/{pageNum}/{pageSize}")
- public IPage<SysPaymentType> list(@PathVariable("pageNum") Integer pageNum
- , @PathVariable("pageSize") Integer pageSize
- , SysPaymentType paymentType){
- QueryWrapper<SysPaymentType> queryWrapper = new QueryWrapper<>();
- //判断支付类型名称 不为空,则进行条件查询
- queryWrapper.lambda()
- .like(Objects.nonNull(paymentType.getPayName()),SysPaymentType::getPayName,paymentType.getPayName())
- .eq(Objects.nonNull(paymentType.getPayId()),SysPaymentType::getPayId,paymentType.getPayId());
- return paymentService.page(new Page<>(pageNum,pageSize),queryWrapper);
- }
- /**
- * 根据支付Id查询支付类型
- * @param payId
- * @return
- */
- @GetMapping("/findById/{payId}")
- public SysPaymentType findById(@PathVariable("payId") Long payId){
- return paymentService.getById(payId);
- }
- }
复制代码 测试接口
测试根据ID查询
http://localhost:8088/paymentservice/payment/findById/1
返回数据如下图
到这里我们的项目就搭建完成了,下为项目结构图
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |