SpringCloudSpringCloud 简单的 Demo
Posted 张学徒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloudSpringCloud 简单的 Demo相关的知识,希望对你有一定的参考价值。
IDEA 2022
jdk 1.8
所学的链接:springCloud — 初级篇(1) 内容更多
SpringCloud 版本问题
写 SpringCloud 项目一定要注意版本问题,一定要和 SpringBoot 的版本对应,否则 SpringCloud 启动失败
进入 https://spring.io/projects/spring-cloud/ 向下滚动查看对应 SpringCloud 版本支持的 SpringBoot 版本。
如下图 2021.0.x aka Jubilee 系列版本仅支持 SpringBoot 2.6.x 版本
学习方式
在这个教程下面的内容,除了在创建模块、application.yml 文件(配置)和 Application 启动类(注解)的部分需要多多注意以外,其他部分和正常的 SpringBoot 开发都没什么两样,其他的复制粘贴进行查看效果即可。
开始
直接开始写项目
需要先创建一个数据库 cloudtest
create database cloudtest;
use cloudtest;
create table payment(
`id` bigint(20) primary key auto_increment,
`serial` varchar(200)
)character set utf8;
insert into payment
values (1, 'zhangsan'),
(2, 'lisi'),
(3, 'wangwu');
创建 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>
<spring-boot.version>2.2.2.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud.alibaba.version>2.2.5.RELEASE</spring-cloud.alibaba.version>
<junit.version>4.12</junit.version>
<lombok.version>1.18.10</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>8.0.22</mysql.version>
<mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
</properties>
<!--子模块继承之后,提供作用:锁定版本+子module不用写groupId和version -->
<!-- 只是声明依赖,不会引入具体依赖 -->
<dependencyManagement><!--定义规范,但不导入 -->
<dependencies>
<!--spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$spring-boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--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 阿里巴巴 -->
<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>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>$mysql.version</version>
<scope>runtime</scope>
</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>
</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>
有时上方的依赖有个别不能下载,这时候就要到 https://mvnrepository.com/ 去搜索对应的
artifactId
名字去看看是否还能下载了,可能这个依赖有漏洞不能下载…比如阿里巴巴的 Druid 包(如下图)。那就得再找找看看能不能用,或改其他方法了,多百度看看…
默认创建的 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>
</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator </artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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 层
package com.zcq.springcloud.controller;
import com.zcq.springcloud.entity.Payment;
import com.zcq.springcloud.service.PaymentService;
import com.zcq.springcloud.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController
@Autowired
private PaymentService paymentService;
@PostMapping("/create")
public JsonResult<Payment> create(@RequestBody Payment payment)
final int result = paymentService.create(payment);
log.info("-- 插入结果:" + result);
if (result > 0)
return new JsonResult<>(200, "success", null);
else
return new JsonResult<>以上是关于SpringCloudSpringCloud 简单的 Demo的主要内容,如果未能解决你的问题,请参考以下文章
springcloudspringcloud与springboot的版本对应关系