SpringCloudSpringCloud 简单的 Demo

Posted 张学徒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloudSpringCloud 简单的 Demo相关的知识,希望对你有一定的参考价值。

IDEA 2022
jdk 1.8
所学的链接:springCloud — 初级篇(1) 内容更多

没有其他废话,就直接开始写项目

需要先创建一个数据库 cloudtest

create database cloudtest;
use cloudtest;

create table payment(
    id bigint(20) primary key auto_increment,
    serial varchar(200)
)character set utf8;

创建 SpringCloudDemo 项目

创建一个 Maven 项目,选中 Maven 直接点击下一步


项目名 SpringCloudDemo

点击 finish 进行创建

点击 pom.xml 文件,添加以下依赖

    <!-- 表示该工程是一个父工程 -->
    <packaging>pom</packaging>

    <!--统一管理jar包版本 -->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
        <lombok.version>1.18.10</lombok.version>
        <log4j.version>1.2.17</log4j.version>
        <mysql.version>5.1.46</mysql.version>
        <druid.version>1.2.6</druid.version>
        <mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
    </properties>

    <!--子模块继承之后,提供作用:锁定版本+子module不用写groupId和version -->
    <!-- 只是声明依赖,不会引入具体依赖 -->
    <dependencyManagement><!--定义规范,但不导入 -->
        <dependencies>

            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.1.2</version>
            </dependency>

            <!--spring boot 2.2.2 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--spring cloud Hoxton.SR3 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--spring cloud 阿里巴巴 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>mysql.version</version>
                <scope>runtime</scope>
            </dependency>

            <!-- druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>druid.version</version>
            </dependency>

            <!--mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>mybatis.spring.boot.version</version>
            </dependency>

            <!--junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>junit.version</version>
            </dependency>

            <!--log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>$log4j.version</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <!--热启动插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

默认创建的 src 文件夹可以直接删除了,当前示例用不到

添加一个模块,右键选中项目文件夹 – New – Module…,选中 Maven,点击下一步,

创建cloud-api-commons模块

存放一些公共功能的类


pom 文件中添加依赖

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

    <dependencies>

        <!-- DevTools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- 糊涂工具包 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>

创建实体类 Payment

package com.zcq.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author z
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable 

    private Long id;
    private String serial;


创建一个 JSON 工具类,用于返回响应的数据

package com.zcq.springcloud.util;

import lombok.Data;

@Data
public class JsonResult<T> 
    private Integer code;
    private String message;
    private T data;

    public JsonResult(Integer code, String message) 
        this(code, message, null);
    

    public JsonResult(Integer code, String message, T data) 
        super();
        this.code = code;
        this.message = message;
        this.data = data;
    

    public JsonResult() 
        super();
    


目录结构如下

创建 cloud-provider-payment8001 模块

创建模块步骤和上面创建 cloud-api-commons 模块的步骤一样,这个模块名设为 cloud-provider-payment8001,这个模块充当支付服务

pom 依赖

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <springboot.version>2.6.5</springboot.version>
    </properties>


    <dependencies>

        <!--另一个模块-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>springboot.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator </artifactId>
            <version>springboot.version</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>springboot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
            <version>springboot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>springboot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>springboot.version</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

    </dependencies>

Dao 层的 Mapper

package com.zcq.springcloud.mapper;

import com.zcq.springcloud.entity.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PaymentDao 

    int create(Payment payment);

    Payment getPaymentById(@Param(("id")) Long id);


Service 层

PaymentService

package com.zcq.springcloud.service;

import com.zcq.springcloud.entity.Payment;
import org.apache.ibatis.annotations.Param;

public interface PaymentService 

    int create(Payment payment);

    Payment getPaymentById(@Param(("id")) Long id);


PaymentServiceImpl

package com.zcq.springcloud.service.impl;

import com.zcq.springcloud.entity.Payment;
import com.zcq.springcloud.mapper.PaymentDao;
import com.zcq.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService 

    @Autowired
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) 
        return paymentDao.create(payment);
    

    @Override
    public Payment getPaymentById(Long id) 
        return paymentDao.getPaymentById(id);
    

Controller 层

SpringCloudSpringCloud 快速入门

springcloudspringcloud与springboot的版本对应关系

SpringCloudSpringCloud 之 Eureka注册中心

Spring Cloud主要组件应用实例

SpringCloud入门

SpringCloud基础概念