Spring Cloud Alibaba 学习实践(一)
一、环境准备(使用Docker搭建基本环境)
使用 VMware 虚拟机搭建的 CentOS7 作为基础的环境
Linux系统的固定IP设定为 192.168.10.20
Linux系统的登录用户为 root
1.1. Docker 安装
1.1.1. 检查和卸载旧版本docker
$ yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
1.1.2. 添加软件源
$ yum install -y yum-utils
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
1.1.3. 安装Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
1.1.4. 启动 Docker
$ systemctl start docker
1.1.5. 检查 Docker 安装情况
$ docker --version
1.1.6. 设置 Docker 开启自启动
$ systemctl enable docker
1.1.7. 创建本次环境所需要的 Docker 自定义网络
$ docker network create demo
1.1.8. 配置 Docker 镜像加速
$ sudo tee /etc/docker/daemon.json <<-'EOF'
# 加入以下配置项
{
"registry-mirrors": [
"https://dockerproxy.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://ccr.ccs.tencentyun.com"
]
}
<<-'EOF'
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
1.2. 安装 MySQL8
本次使用最新版的mysql:debian版本(此版本包含了完整的数据库性能检查工具)
mysql:oracle 版本的缺少一些性能检查工具
1.2.1. 创建MySQL对应的本地文件夹
$ mkdir -p /root/docker/mysql/data /root/docker/mysql/conf /root/docker/mysql/logs
1.2.2. 编写mysql的my.cnf配置文件
[client]
default-character-set = utf8mb4
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
# 字符集
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
default_authentication_plugin= mysql_native_password
# 是否对sql语句大小写敏感,0:大小写敏感,1:忽略大小写区别。,只能在初始化服务器时配置。禁止在服务器初始化后更改
# 设置为2时,表名和数据库名按声明存储,但以小写形式进行比较
lower_case_table_names = 2
# 最大连接数
max_connections = 1000
# Innodb缓存池大小
innodb_buffer_pool_size = 4G
# 表文件描述符的缓存大小
table_open_cache_instances=1
table_open_cache=2000
table_definition_cache=2000
!includedir /etc/mysql/conf.d/
1.2.3. 编写 docker-compose.yml 文件
version: '3'
services:
mysql:
image: mysql:debian
restart: always
privileged: true
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
networks:
- demo
volumes:
- /root/docker/mysql/log:/var/log/mysql:rw
- /root/docker/mysql/data:/var/lib/mysql:rw
- /root/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf:rw
networks:
demo:
external: true
1.2.4. 使用 docker compose 创建容器
$ docker compose up -d
1.2.5. 检查mysql安装情况
$ docker ps
$ docker exec -it <mysql容器名称> mysql -u root -p
二、创建项目
使用idea 创建父工程
编辑 settings.gradle.kts
rootProject.name = "sca-demo"
rootProject.children.forEach { project ->
project.buildFileName = "${project.name}.gradle.kts"
project.children.forEach { pro ->
pro.buildFileName = "${pro.name}.gradle.kts"
}
}
编辑父工程的build.gradle.kts
plugins {
id("java")
id("idea")
id("java-library")
id("org.springframework.boot") version "2.6.13"
id("io.spring.dependency-management") version "1.1.3"
}
group = "com.anbuis.sca"
version = "1.0-SNAPSHOT"
repositories {
mavenLocal()
maven("https://maven.aliyun.com/nexus/content/repositories/google")
maven("https://maven.aliyun.com/nexus/content/groups/public")
maven("https://maven.aliyun.com/nexus/content/repositories/jcenter")
maven("https://plugins.gradle.org/m2/")
mavenCentral()
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
val lombokVersion = "1.18.26"
val springCloudVersion = "2021.0.5"
val springCloudAlibabaVersion = "2021.0.5.0"
val hutoolVersion = "5.8.16"
val knife4jVersion = "4.1.0"
val fastjsonVersion = "2.0.28"
val swaggerAnnotationVersion = "2.2.8"
subprojects {
apply {
plugin("java")
plugin("idea")
plugin("java-library")
plugin("org.springframework.boot")
plugin("io.spring.dependency-management")
}
group = "com.anbuis.shop"
version = "1.0-SNAPSHOT"
repositories {
mavenLocal()
maven("https://maven.aliyun.com/nexus/content/repositories/google")
maven("https://maven.aliyun.com/nexus/content/groups/public")
maven("https://maven.aliyun.com/nexus/content/repositories/jcenter")
maven("https://plugins.gradle.org/m2/")
mavenCentral()
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
springBoot {
buildInfo()
}
// 所有子模块都会有的依赖
dependencies {
// Hutool
implementation("cn.hutool:hutool-all")
// FastJson
implementation("com.alibaba.fastjson2:fastjson2")
// SpringDoc Swagger Annotation
implementation("io.swagger.core.v3:swagger-annotations")
// Junit Test
implementation("org.springframework.boot:spring-boot-starter-test")
// Lombok
compileOnly("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
testCompileOnly("org.projectlombok:lombok:${lombokVersion}")
testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
}
dependencyManagement {
// 导入SpringCloud和SpringCloudAlibaba对应的依赖
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}")
mavenBom("com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}")
}
// 定义依赖,后续子模块中就可以不用写版本号
dependencies {
dependency("cn.hutool:hutool-all:${hutoolVersion}")
dependency("com.github.xiaoymin:knife4j-openapi3-spring-boot-starter:${knife4jVersion}")
dependency("com.alibaba.fastjson2:fastjson2:${fastjsonVersion}")
dependency("io.swagger.core.v3:swagger-annotations:${swaggerAnnotationVersion}")
}
}
tasks.jar {
enabled = true
}
tasks.test {
useJUnitPlatform()
}
}
创建子模块
common
公共模块(所有项目都有可能使用的工具,数据结构,异常等)
mvc-config
MVC公共配置模块(所有需要用到MVC的模块使用到的公共配置)
service
微服务模块(微服务模块的父模块)
:service:service-config
微服务通用配置模块(微服务需要使用到的公共配置,如SpringDoc,OpenFeign,Seata,Ribbon,loadbalancer,mybatis-plus,日志收集等)
:service:service-sdk
微服务开发工具包模块(微服务间的请求通讯的服务开发工具集,后续如果会开放的话,可以通过这个jar包进行一个SpringBoot的自动配置)
:service:user-service
用户微服务模块(主要是用户相关服务请求处理)
:service:order-service
订单微服务模块(主要是订单香港服务请求处理)
将子模块添加到 settings.gradle.kts 文件中,类似maven中的
rootProject.name = "sca-demo"
include(
"common",
"mvc-config",
"service",
":service:service-config",
":service:service-sdk",
":service:user-service",
":service:order-service"
)
rootProject.children.forEach { project ->
project.buildFileName = "${project.name}.gradle.kts"
project.children.forEach { pro ->
pro.buildFileName = "${pro.name}.gradle.kts"
}
}
刷新gradle依赖之后,得到的目录结构如下:
以上就是项目前期的基本架构
标签:service,Spring,Alibaba,maven,模块,mysql,docker,com,Cloud From: https://www.cnblogs.com/code2code/p/17712887.html