2)SpringCloud Alibaba微服务实战二 服务注册

导读:在之前一篇文章中我们准备好了基于SpringCloud Alibaba的基础组件,本期主要内容是将所有的服务注册进Nacos,并让account-service和product-service能对外提供基础的增删改查能力

数据准备

分别创建这三个数据库  account账户,order订单,user用户

本系列文章都是基于account-service,product-service,order-service,所以我们先准备好这三张基础表结构。

----------------------------------------------------------------------------------------------------------account

/*

 Navicat Premium Data Transfer

 Source Server         : 微服务专用

 Source Server Type    : MySQL

 Source Server Version : 50736

 Source Host           : 192.168.182.129:3306

 Source Schema         : db_account

 Target Server Type    : MySQL

 Target Server Version : 50736

 File Encoding         : 65001

 Date: 14/03/2023 15:33:49

*/

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------

-- Table structure for account

-- ----------------------------

DROP TABLE IF EXISTS `account`;

CREATE TABLE `account`  (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `card_no` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `balance` double(11, 0) NULL DEFAULT NULL,

  `status` int(11) NULL DEFAULT NULL,

  `user_id` int(11) NULL DEFAULT NULL,

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Records of account

-- ----------------------------

INSERT INTO `account` VALUES (1, '1', '1', 500, 1, 1);

INSERT INTO `account` VALUES (2, '2', '2', 1500, 0, 1);

-- ----------------------------

-- Table structure for undo_log

-- ----------------------------

DROP TABLE IF EXISTS `undo_log`;

CREATE TABLE `undo_log`  (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `branch_id` bigint(20) NOT NULL,

  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `rollback_info` longblob NOT NULL,

  `log_status` int(11) NOT NULL,

  `log_created` datetime(0) NOT NULL,

  `log_modified` datetime(0) NOT NULL,

  `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  PRIMARY KEY (`id`) USING BTREE,

  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Records of undo_log

-- ----------------------------

SET FOREIGN_KEY_CHECKS = 1;

--------------------------------------------------------------------------------------------------------------order

/*

 Navicat Premium Data Transfer

 Source Server         : 微服务专用

 Source Server Type    : MySQL

 Source Server Version : 50736

 Source Host           : 192.168.182.129:3306

 Source Schema         : db_order

 Target Server Type    : MySQL

 Target Server Version : 50736

 File Encoding         : 65001

 Date: 14/03/2023 15:34:04

*/

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------

-- Table structure for transaction_record

-- ----------------------------

DROP TABLE IF EXISTS `transaction_record`;

CREATE TABLE `transaction_record`  (

  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,

  `tran_no` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `card_no` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `transaction_date` datetime(0) NULL DEFAULT NULL,

  `amount` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `balance` int(11) NULL DEFAULT NULL,

  `transaction_type` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `remark` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Records of transaction_record

-- ----------------------------

INSERT INTO `transaction_record` VALUES (2, '90ee209f307d4c088ca076ff53e00e7f', '2', '2022-12-02 15:52:07', '5', 995, '转出', '2转1');

INSERT INTO `transaction_record` VALUES (3, '5309afc620bf47648f455bbd27fe727b', '1', '2022-12-02 07:57:51', '-600', 405, '转出', '1转2');

INSERT INTO `transaction_record` VALUES (4, '1cb9498fe0aa4c39b636d8d9e67ab337', '1', '2022-12-02 08:00:06', '-600', 405, '转出', '1转2');

INSERT INTO `transaction_record` VALUES (5, '536d58c16a9c47949bfc47b461a7ce77', '1', '2022-12-02 08:00:15', '-600', 405, '转出', '1转2');

INSERT INTO `transaction_record` VALUES (6, 'dd9316a9cdc245028ac0cbf93a1f6b10', '1', '2022-12-02 08:00:40', '-600', 405, '转出', '1转2');

INSERT INTO `transaction_record` VALUES (7, 'f53a47abb2ad4c9f930c2591f7be607a', '1', '2022-12-02 08:00:50', '-600', 405, '转出', '1转2');

INSERT INTO `transaction_record` VALUES (8, '6809ebc4a448442a8f7c143bff322a62', '2', '2022-12-02 12:26:25', '573', 1000, '收账', '2卡转入573');

INSERT INTO `transaction_record` VALUES (9, 'fe9160412ad2468fb831d2e1d276ad58', '2', '2022-12-05 08:47:06', '500', 1500, '收账', '2卡转入500');

INSERT INTO `transaction_record` VALUES (10, 'f6ab76aefd1e40b6bef6f52c73d51107', '2', '2022-12-05 08:50:33', '400', 1900, '收账', '2卡转入400');

INSERT INTO `transaction_record` VALUES (11, 'cbda96c8e3f64a8a9392d475011bcffa', '1', '2022-12-05 08:53:54', '900', 1000, '收账', '1卡转入900');

INSERT INTO `transaction_record` VALUES (12, '2a338fdac3b544bb9d267c3584b66b08', '2', '2022-12-09 07:09:22', '500', 1500, '进账', '2进账500');

-- ----------------------------

-- Table structure for undo_log

-- ----------------------------

DROP TABLE IF EXISTS `undo_log`;

CREATE TABLE `undo_log`  (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `branch_id` bigint(20) NOT NULL,

  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `rollback_info` longblob NOT NULL,

  `log_status` int(11) NOT NULL,

  `log_created` datetime(0) NOT NULL,

  `log_modified` datetime(0) NOT NULL,

  `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  PRIMARY KEY (`id`) USING BTREE,

  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Records of undo_log

-- ----------------------------

SET FOREIGN_KEY_CHECKS = 1;

-----------------------------------------------------------------------------------------------------------user

/*

 Navicat Premium Data Transfer

 Source Server         : 微服务专用

 Source Server Type    : MySQL

 Source Server Version : 50736

 Source Host           : 192.168.182.129:3306

 Source Schema         : bankonline

 Target Server Type    : MySQL

 Target Server Version : 50736

 File Encoding         : 65001

 Date: 14/03/2023 15:34:25

*/

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------

-- Table structure for user

-- ----------------------------

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user`  (

  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),

  `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Records of user

-- ----------------------------

INSERT INTO `user` VALUES (1, 'chaoge', '123456', '15307413463', '2022-11-18 11:24:21', '2022-11-18 11:24:21');

INSERT INTO `user` VALUES (2, '66', '123456', '11111111111', '2022-11-18 07:50:27', '2022-11-18 07:50:27');

SET FOREIGN_KEY_CHECKS = 1;

基础框架搭建

       在你的IDEA中建立一个多模块的Maven项目(过程略...),项目整体截图如下:

 

cloud-alibaba13的pom.xml中导入依赖
 

<!--    打包方式-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
</parent>

<properties>
    <!--maven.compiler.source 和 maven.compiler.target 指定java编译器版本为8-->
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <!--指定项目源码的字符集编码为UTF-8-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--指定SpringCloud版本-->
    <spring.cloud.version>2020.0.1</spring.cloud.version>
    <!--指定SpringCloudAlibaba版本-->
    <spring.cloud.alibaba.version>2021.1</spring.cloud.alibaba.version>
    <!--指定mysql的版本-->
    <mysql-java.version>8.0.13</mysql-java.version>
    <!--指定mybatis-plus的版本-->
    <mp.version>3.4.1</mp.version>

    <!--mybatis-plus代码生成器-->
    <mp-generator>3.4.1</mp-generator>
    <!--mybatis-plus模拟生成器的版本 -->
    <velocity.version>2.3</velocity.version>
    <!--knife4j Swagger接口文档工具的增强版本-->
    <knife4j.version>2.0.8</knife4j.version>
    <!--swagger-annotations-->
    <swagger-annotations>1.5.10</swagger-annotations>
    <!--最强工具类-->
    <hutool-all>5.8.7</hutool-all>

</properties>

<!--<dependencyManagement>标签用于管理项目的依赖库-->
<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>

        <!--MyBatis Plus框架的启动器,简化MyBatis的配置和使用-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mp.version}</version>
        </dependency>
        <!--MyBatis Plus代码生成器,根据数据库表结构自动生成MyBatis的实体类、Mapper接口等代码-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>${mp.version}</version>
        </dependency>

        <!--MySQL数据库驱动,用于连接MySQL数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-java.version}</version>
        </dependency>

        <!--Swagger注解库,用于生成API文档。-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations}</version>
        </dependency>

        <!-- Velocity模板引擎的核心库,用于生成代码模板-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>${velocity.version}</version>
        </dependency>

        <!--Knife4j的微服务版本,用于生成增强版的Swagger接口文档-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-micro-spring-boot-starter</artifactId>
            <version>${knife4j.version}</version>
        </dependency>

        <!--Knife4j的Spring Boot版本,用于生成增强版的Swagger接口文档-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>${knife4j.version}</version>
        </dependency>

        <!-- Hutool工具类库,提供了丰富的Java工具类,简化开发过程-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool-all}</version>
        </dependency>

    </dependencies>
</dependencyManagement>

clound-common(公共工具服务)

1.导入依赖

在clound-common 的pom.xml导入

<dependencies>
    <!--MyBatis Plus框架的启动器,简化MyBatis的配置和使用-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>

    <!--MySQL数据库驱动,用于连接MySQL数据库。-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!--Lombok库,通过注解减少Java代码的冗余,简化开发。-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <!--Spring Boot的Web组件,用于开发Web应用程序。-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--Swagger注解库,用于生成API文档。-->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
    </dependency>

    <!--Fastjson库,用于在Java对象和JSON之间进行转换和操作。-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.78</version>
    </dependency>

</dependencies>

clound-common作为一个工具类

2.导入以下类

entity 

----------------------------------------------------------------------------Account

package com.zhaoxin.common.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @program: t139springcloudalibaba
 * @description: 账户类
 * @author: Mr.Nie
 * @create: 2022-06-15 15:31
 **/
@Getter
@Setter
@Accessors(chain = true)
@TableName("account")
public class Account {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    private String cardNo;

    private String password;

    private Double balance;

    private Integer status;

    private Integer userId;
}

------------------------------------------------------------TransactionRecord
package com.zhaoxin.common.entity;

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.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Date;


@Getter
@Setter
@Accessors(chain = true)
@TableName("transaction_record")
public class TransactionRecord  {//extends Model<TransactionRecord>

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("tran_no")
    private String tranNo;

    @TableField("card_no")
    private String cardNo;

    @TableField("transaction_date")
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date transactionDate;

    @TableField("amount")
    private String amount;

    @TableField("balance")
    private Integer balance;

    @TableField("transaction_type")
    private String transactionType;

    @TableField("remark")
    private String remark;


//    @Override
//    public Serializable pkVal() {
//        return this.id;
//    }

}

----------------------------------------------------------------------User
package com.zhaoxin.common.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

@Getter
@Setter
@Accessors(chain = true)
@TableName("user")
public class User extends Model<User> {

    /**
     * 编号
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 用户名
     */
    @TableField("username")
    private String username;

    /**
     * 手机号
     */
    @TableField("phone")
    private String phone;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 修改时间
     */
    @TableField("update_time")
    private Date updateTime;


    @Override
    public Serializable pkVal() {
        return this.id;
    }

}

execption

----------------------------------------------------------------------BusinessException

package com.zhaoxin.common.exception;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 业务逻辑错误总类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BusinessException extends RuntimeException {//特别的错的返回值
    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "错误信息")
    private String errMsg;
    //用result的code 和 mess 做自己的
}

----------------------------------------------------------------------GlobalExceptionHandle

package com.zhaoxin.common.exception;

import com.zhaoxin.common.response.R;
import com.zhaoxin.common.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//拦截controller 全局异常
@RestControllerAdvice
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandle {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception e) {
//        if(e instanceof ArrayIndexOutOfBoundsException){
//            log.info("数组越界错误");
//            return R.error(ResultCode.SHUZUOUT);
//        }
        //算数错误
        if (e instanceof ArithmeticException) {
            log.info("算数错误");
            return R.error(ResultCode.ARITHMETIC_EXCEPTION);
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            return R.error(ResultCode.COMMON_FAIL);
        } else if (e instanceof BusinessException) {
            //借马换马
            return R.error()
                    .code(((BusinessException) e).getCode())
                    .message(((BusinessException) e).getErrMsg());
        } else {
            return R.error();//有重载的
        }

    }
}


response
 
-------------------------------------------------------------CustomizeResultCode 
package com.zhaoxin.common.response;
public interface CustomizeResultCode {
    /**
     * 获取错误状态码
     *
     * @return 错误状态码
     */
    Integer getCode();

    /**
     * 获取错误信息
     *
     * @return 错误信息
     */
    String getMessage();
}

----------------------------------------------------------------------R
package com.zhaoxin.common.response;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;


@Data
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;//可以不要

    @ApiModelProperty(value = "返回码")
    private Integer code;//200 302

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    /**
     * 构造方法私有化,里面的方法都是静态方法
     * 达到保护属性的作用
     */
    private R() {

    }

    /**
     * 这里是使用链式编程 无需掌握怎么写,会用就行
     */
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMessage(ResultCode.SUCCESS.getMessage());
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResultCode.COMMON_FAIL.getCode());
        r.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return r;
    }

    public static R error(ResultCode resultCode) {//错误码
        R r = new R();
        r.setSuccess(false);
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMessage());
        return r;
    }

    /**
     * 自定义返回成功与否
     *
     * @param success
     * @return
     */
    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}

-------------------------------------------------------------------ResultCode

package com.zhaoxin.common.response;

public enum ResultCode implements CustomizeResultCode {
    /* 成功 */
    SUCCESS(200, "成功"),

    /* 默认失败 */
    COMMON_FAIL(999, "失败"),

    /* 参数错误:1000~1999 */
    PARAM_NOT_VALID(1001, "参数无效"),
    PARAM_IS_BLANK(1002, "参数为空"),
    PARAM_TYPE_ERROR(1003, "参数类型错误"),
    PARAM_NOT_COMPLETE(1004, "参数缺失"),


    USER_NOT_DELETE(201, "s删除失败"),
    USER_NOT_ToCHANNElEXCEL_fail(201, "导入失败"),
    USER_NOT_ToCHANNElEXCEL_succeed(200, "导入成功"),

    /* 用户错误 */
    USER_NOT_FIND(2000, "对象没有找到"),
    USER_NOT_LOGIN(2001, "用户未登录"),
    USER_ACCOUNT_EXPIRED(2002, "账号已过期"),
    USER_CREDENTIALS_ERROR(2003, "密码错误"),
    USER_CREDENTIALS_EXPIRED(2004, "密码过期"),
    USER_ACCOUNT_DISABLE(2005, "账号不可用"),
    USER_ACCOUNT_LOCKED(2006, "账号被锁定"),
    USER_ACCOUNT_NOT_EXIST(2007, "账号不存在"),
    USER_ACCOUNT_ALREADY_EXIST(2008, "账号已存在"),
    USER_ACCOUNT_USE_BY_OTHERS(2009, "账号下线"),
    USER_ACCOUNT_USE_BY_OTHERReason(2022, "对不起你查询到内容不存咋"),
    USER_ACCOUNT_USE_BY_OTHERReason154(2023, "对不起你查询到牛逼"),
    /*部门错误*/
    DEPARTMENT_NOT_EXIST(3007, "部门不存在"),
    DEPARTMENT_ALREADY_EXIST(3008, "部门已存在"),
    DEPARTMENT_ALREADY_EXIST1(3009, "部门已存在9"),
    /* 业务错误 */
    NO_PERMISSION(3001, "没有权限"),

    /*运行时异常*/
    ARITHMETIC_EXCEPTION(9001, "算数异常");

    private Integer code;

    private String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

 account-biz

1.pom.xml导入依赖

<dependencies>
    <!--Spring Cloud Feign库的启动器,用于实现微服务之间的调用-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <!--Spring Cloud负载均衡器,用于在微服务之间分配请求。-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-loadbalancer</artifactId>
    </dependency>

    <!--Swagger注解库。-->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
    </dependency>

    <!--Sentinel与Nacos的数据源集成,用于实现动态规则配置。-->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>

    <!--Spring Cloud的启动器,用于解决无法加载bootstrap.yaml文件的问题。-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>

    <!--阿里云Nacos的配置客户端,用于获取配置信息。-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

    <!--阿里云Nacos的服务发现客户端,用于注册和发现微服务实例。-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!--自定义的公共模块,用于封装一些通用的功能和工具类。-->
    <dependency>
        <groupId>com.zhaoxin</groupId>
        <artifactId>clound-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--Spring Boot的Web组件,用于开发Web应用程序。-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--Knife4j的Spring Boot版本,用于生成增强版的Swagger接口文档。-->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-micro-spring-boot-starter</artifactId>
    </dependency>

    <!-- Velocity模板引擎的核心库,用于生成代码模板-->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
    </dependency>

    <!--Knife4j的Spring Boot版本,用于生成增强版的Swagger接口文档-->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
    </dependency>

    <!--MyBatis Plus代码生成器,根据数据库表结构自动生成MyBatis的实体类、Mapper接口等代码-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>${mp.version}</version>
    </dependency>

</dependencies>

 模块

 config
-----------------------------------------------------------SwaggerConfiguration
package com.zhaoxin.account.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


@Configuration
@EnableSwagger2WebMvc
//@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    @Bean(value = "userApi")
    @Order(value = 1)
    public Docket groupRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhaoxin.account.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo groupApiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui很棒 161和162一起努力!!!")
                .description("<div style='font-size:14px;color:red;'>swagger-bootstrap-ui-demo RESTful APIs</div>")
                .termsOfServiceUrl("http://www.group.com/")
                .contact("group@qq.com")
                .version("1.0")
                .build();
    }

}
controller

------------------------------------------------------------------------------------------------AccountController

package com.zhaoxin.account.controller;

import com.zhaoxin.account.service.AccountService;
import com.zhaoxin.common.entity.Account;
import com.zhaoxin.common.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuchao
 * @since 2023-11-29
 */
@RestController
@Api("转账服务")
public class AccountController {

    @Resource
    private AccountService accountService;

    @GetMapping("/getAccountList")
    @ApiOperation("转账")
    public R getAccountList(){
        List<Account> accountList = accountService.list();
        return R.ok().data("accountList",accountList);
    }


    //转账
    @ApiOperation("转账")
    @PostMapping("transfer")
    public R transfer(String cardNo, String otherCardNo, double amount){
        accountService.transfer(cardNo, otherCardNo, amount);
        return R.ok().data("xxx","转账成功");
    }


}

mapper
 
--------------------------------------------------------------------AccountMapper 
package com.zhaoxin.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhaoxin.common.entity.Account;

import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author wuchao
 * @since 2023-11-29
 */
public interface AccountMapper extends BaseMapper<Account> {
    //扣钱
    void decrease(Map map);
    //加钱
    void increase(Map map);

}

service
------------------------------------------------------------------AccountService
package com.zhaoxin.account.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.zhaoxin.common.entity.Account;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author wuchao
 * @since 2023-11-29
 */
public interface AccountService extends IService<Account> {
    void transfer(String cardNo, String otherCardNo, double amount);//转账
}
impl
------------------------------------------------------------AccountServiceImpl 
package com.zhaoxin.account.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaoxin.account.mapper.AccountMapper;
import com.zhaoxin.account.service.AccountService;
import com.zhaoxin.common.entity.Account;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2023-11-29
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    //@Transactional
    @Override
    public void transfer(String cardNo, String otherCardNo, double amount) {
//        if(amount<=0){
//            throw new BusinessException(ResultCode.money.getCode(),
//                    ResultCode.money.getMessage());
//        }
//        LambdaQueryWrapper<Account> lanmb=new LambdaQueryWrapper<>();
//        lanmb.eq(Account::getCardNo,cardNo);
//        Account account = this.baseMapper.selectOne(lanmb);
//        if(account==null){//卡号存在吗
//
//        }
//        if(account.getStatus()==1){//卡被锁了,不能转
//
//        }
//        if(account.getBalance()==0){//没钱不能转账
//
//        }
        Map<String,Object> map1=new HashMap<>();
        map1.put("cardNo",cardNo);
        map1.put("amount",amount);
        this.baseMapper.decrease(map1);
        //int i=8/0;
        Map<String,Object> map2=new HashMap<>();
        map2.put("cardNo",otherCardNo);
        map2.put("amount",amount);
        this.baseMapper.increase(map2);
    }
}
AccountServiceApplication
package com.zhaoxin.account;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"com.zhaoxin.account","com.zhaoxin.common"})
@MapperScan(basePackages = "com.zhaoxin.account.mapper")
public class AccountServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(AccountServiceApplication.class,args);
    }
}

resourse

mapper
 

---------------------------------------------------------------------------------AccountMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhaoxin.account.mapper.AccountMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zhaoxin.common.entity.Account">
        <id column="id" property="id" />
        <result column="card_no" property="cardNo" />
        <result column="password" property="password" />
        <result column="balance" property="balance" />
          <result column="status" property="status" />
            <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, card_no, password, balance,status,user_id
    </sql>

    <update id="decrease" parameterType="Map">
    update account set balance=balance-#{amount}
    where card_no=#{cardNo}
    </update>

    <update id="increase" parameterType="Map">
    update account set balance=balance+#{amount}
    where card_no=#{cardNo}
    </update>
</mapper>

Yaml

spring:
  application:
    name: account-service
  datasource:
    url: jdbc:mysql://192.168.44.132:3306/account?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: root
    username: root
    hikari:
      pool-name: dataHikariCP
      minimum-idle: 5
      idle-timeout: 180000
      maximum-pool-size: 10
      max-lifetime: 180000
      auto-commit: true
      connection-timeout: 30000
      connection-test-query: SELECT 1
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.44.132:8848
    loadbalancer:
      cache:
        enabled: false
      ribbon:
        enabled: false
server:
  port: 8081


mybatis-plus:
    configuration:
      map-underscore-to-camel-case: true
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    mapper-locations: classpath*:/mapper/**/*.xml
    type-aliases-package: com.zhaoxin.common.entity

 

启动


打开services



三个都是一样的order-biz和user-biz自己写

 

打包

1)打包

maven下的 Lifecycle ==》下分别双击(clean,compile,package,install)

如果没有报错则会在target包下得到两个jar包,分别是后缀为.jar和.jar.original的文件

account-service-1.0-SNAPSHOT.jar

account-service-1.0-SNAPSHOT.jar.original

2)链接linux

1)找到 工具栏Tools下的 Deployment 下的 Browse Remote Host 打开linux中的层级目录,接下来新建一个目录存放 ,我是在 /usr/local/docker/cloud-alibaba然后吧刚才的account-service-1.0-SNAPSHOT.jar拖入到新建的包中,

 

2)在工具栏中找到Tools,点击 Start SHH session 输入主机地址和密码进行链接,cd 到刚才的目录 使用java -jar account-service-1.0-SNAPSHOT.jar 部署到Linux服务器


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值