架构选型
技术选型
技术选型:
1、Spring Boot 2.2.5.RELEASE、Spring CLoud Hoxton.SR3、 Springcloud Alibaba 2.2.0.RELEASE、Nacos 1.2.1 、Seata 1.2.0、Gateway 2.2.2.RELEASE、Feign 2.2.2.RELEASE、Sentinel、Swagger2、Security、Jwt、OAuth2、lombok、MybatisPlus
2、JDK1.8、IDEA
3、React /Flux
4、MQTT、COAP、NB—IOT、
5、5G
6、Redis、mycat+mysql
7、中间件:RocketMQ、Netty、FastDFS/MongoDB、ElasticSearch
8、负载均衡:LVS、Nginx
9、分布式事务:TX-LCN(不推荐)/seata
10、分布式任务调度:xxl-job
11、分布式锁:Zookeeper
12、分布式ID⽣成:snowflake
12、序列化:jackson;禁止fastjson
安全测试工具:AppScan
代码审查:SonarQube 和 SonarLint
IDEA 插件:SonarLint 、 Alibaba Java Coding Guidelines
SonarQube:https://docs.sonarqube.org
SonarLint: https://www.sonarlint.org/
考虑:
限流、并发、缓存
开发⼯具类
IDEA 插件:
1. Lombok
2. .ignore
3. Alibaba Cloud Toolkit
4. Alibaba Java Coding Guidelines
5. CodeGlance
6. Restful Toolkit
7. git commit template(代码提交管理规范插件)
前端:
1. eslint
2. tslint
ORM 框架:
1. Mybatis
2. Springboot JPA
基本⼯具包
1. Guava 28.2-jre
2. Hutool 5.2.3
3. Excel ⼯具类: EasyExcel 2.1.6
4. Json ⼯具类: Fastjson 1.2.66
安全工具:
1. AppScan
2. SonarQube 和 SonarLint(idea)
3. sonarscanner(前端代码):https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
运维相关
1. 数据库分库分表⽅案: MyCat
# git commit template
type用于说明commit的类别,常用的标识如下:
feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style:格式(不影响代码运行的变动,空格,格式化,等等)
refactor:重构(即不是新增功能,也不是修改bug的代码变动
perf:性能 (提高代码性能的改变)
test:增加测试或者修改测试
build:影响构建系统或外部依赖项的更改(maven,gradle,npm 等等)
ci:对CI配置文件和脚本的更改
chore:对非 src 和 test 目录的修改
revert:Revert a commit
使用参考规范 https://blog.csdn.net/noaman_wgs/article/details/103429171
包格式
- sd
- controller
- v1(V1版本控制器)
- v2(V2版本控制器)
- mapper
- service
- mapper
- controller
- domian:数据库实体类
- infra :基础
- feign
- utils
- XXXApp.jar :运行主类
公共配置
fdfs: 192.22.1.21:8888
nacos:192.22.1.21:8500/nacos(nacos/zlwl@nacos)
xxl-job-admin:http://192.22.1.21:9600/xxl-job-admin/ (admin/zlwl@job)
rabbitmq: http://192.22.1.21:15672/ (rabbit/zlwl@rabbit)
rocketmq: http://192.22.1.21:8090/
redis:192.22.1.21:6379 wlw@redis
Springcloud Alibaba 简介
概述
Spring Cloud Alibaba
致力于提供分布式应用服务开发的一站式解决方案。项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
此项目包含的组件内容,主要选取自阿里巴巴开源中间件和阿里云商业化产品,但也不限定于这些产品。
如果您对 Roadmap 有任何建议或想法,欢迎在 issues 中或者通过其他社区渠道向我们提出,一起讨论。
已包含的组件:
Sentinel
阿里巴巴开源产品,把流量作为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Nacos
阿里巴巴开源产品,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
RocketMQ
Apache RocketMQ™ 基于 Java 的高性能、高吞吐量的分布式消息和流计算平台。
Dubbo
Apache Dubbo™ 是一款高性能 Java RPC 框架。
Seata
阿里巴巴开源产品,一个易于使用的高性能微服务分布式事务解决方案。
Alibaba Cloud OSS
阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
Alibaba Cloud SchedulerX
阿里中间件团队开发的一款分布式任务调度产品,支持周期性的任务与固定时间点触发任务。
Alibaba Cloud SMS
覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道。
下载,安装nacos
下载
手册
https://nacos.io/zh-cn/
管方集群安装手册 https://nacos.io/zh-cn/docs/deployment.html
安装
#安装:
$ tar -zxvf nacos-server-$version.tar.gz
$ cd nacos/bin
配置 MySQL 数据库
启动集群
单机版:./startup.sh -m standalone
$ sh ./nacos-8848/bin/startup.sh -m cluster
$ sh ./nacos-8849/bin/startup.sh -m cluster
$ sh ./nacos-8850/bin/startup.sh -m cluster
防火墙
查看防火墙状态
systemctl status firewalld;
$ firewall-cmd --permanent --add-port=8848/udp --add-port=8848/tcp
$ firewall-cmd --reload
$ firewall-cmd --list-ports
访问
http://192.22.1.21:8848/nacos
http://192.22.1.21:8849/nacos
http://192.22.1.21:8850/nacos
用户名/密码:nacos/nacos
升级nacos1.x 到 2.x
1、sh nacos-8848/bin/shutdown.sh
2、配置application.properties
server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.22.1.21:3306/nacos-8848?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=Developer@2019
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.basedir=
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-ui/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=false
nacos.core.auth.default.token.expire.seconds=18000
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.caching.enabled=true
nacos.core.auth.enable.userAgentAuthWhite=true
nacos.core.auth.server.identity.key=
nacos.core.auth.server.identity.value=
nacos.istio.mcp.server.enabled=false
3、配置conf/cluster.conf
#it is ip
#example
192.22.1.68:8848
192.22.1.88:8848
192.22.1.94:8848
4、启动集群
单机 sh startup.sh -m standalone
集群 sh ./bin/startup.sh -m cluster
内置数据源:sh startup.sh -p embedded
使用外置数据源(mysql):sh startup.sh
5、检查
tail -f -n 100 /root/nacos-8848/logs/start.out
tail -f -n 100 logs/naming-server.log
集成springboot springcloud (grpc:9848 9849)
1. 版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
2. 问题依赖:https://nacos.io/zh-cn/docs/2.0.0-compatibility.html
3. 开启支持grpc:firewall-cmd --permanent --add-port=9848/udp --add-port=9848/tcp --add-port=9848/udp --add-port=9849/tcp
<!--nacos 2.0-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.0-BETA</version>
</dependency>
防火墙
查看防火墙状态
systemctl status firewalld;
$ firewall-cmd --permanent --add-port=8848/udp --add-port=8848/tcp
$ firewall-cmd --permanent --add-port=9848/udp --add-port=9848/tcp
$ firewall-cmd --permanent --add-port=9849/udp --add-port=9849/tcp
$ firewall-cmd --reload
$ firewall-cmd --list-ports
nginx 安装grpc配置
参考
nginx配置同时支持grpc(http2):https://blog.csdn.net/xuxuxu1222/article/details/90175528
https://osswangxining.github.io/nginx-grpc/
Nginx配置代理gRPC的方法:https://www.jb51.net/article/137330.htm
Nginx官网:http://nginx.org/en/docs/http/ngx_http_v2_module.html
nginx转发grpc服务(grpc ->grpc):https://www.cnblogs.com/yonyong/p/14014605.html
nacos2.0 nginx 配置
根据https://nacos.io/zh-cn/docs/2.0.0-compatibility.html 图
正确的 nginx.conf
在三台机器上部署了nacos-server
a:8848 b:8848 c:8848
然后在a上配置nginx, 其中nginx.conf 的 stream内容
# 参考:nginx配置nacos集群转发及stream转发tcp四层配置
stream {
upstream nacosGrpc {
server a:9848; // X + 1000
server b6:9848;
server c:9848;
}
server {
listen 9080; // Y + 1000
proxy_pass nacosGrpc;
}
}
http {
# ignore something here
#gzip on;
upstream nacosClu {
server a:8848; // X = nacos端口, 默认8848
server b:8848;
server c:8848;
}
server {
listen 8080; // Y = nginx监听端口, 但是端口别跟其他的冲突了
server_name nginxIp;
location / {
proxy_pass http://nacosClu;
}
# ignore something here
}
}
控制台地址: nginxIp: Y/nacos
server-addr地址: nginxIp: Y
nginx配置完整版
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#设定负载均衡的服务器列表 xxl-job-admin
upstream xxl-job-cluster{
server 192.22.1.82:9600 weight=1;
server 192.22.1.83:9600 weight=1;
server 192.22.1.84:9600 weight=1;
}
server {
listen 9600;
server_name localhost;
location / {
proxy_pass http://xxl-job-cluster;
proxy_set_header Host $host:9600;
proxy_set_header X-Forward-For $remote_addr;
root /xxl-job-admin;
index index.php index.html index.htm;
}
}
# nacos-cluster 集群部署
# upstream nacos-cluster{
# server 192.22.1.68:8848 weight=1;
# server 192.22.1.88:8848 weight=1;
# server 192.22.1.98:8848 weight=1;
# }
# server {
# listen 8848;
# server_name location;
# location / {
# #请求转向taishan定义的服务器列表
# proxy_pass http://nacos-cluster;
# #将请求头转发给后端服务器
# proxy_set_header Host $host:8500;
# #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
# proxy_set_header X-Forward-For $remote_addr;
# root /nacos/;
# index index.php index.html index.htm;
# }
# }
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
stream {
upstream cluster {
server 192.22.1.68:8848 weight=1;
server 192.22.1.88:8848 weight=1;
server 192.22.1.98:8848 weight=1;
}
server {
listen 8500;
proxy_pass cluster;
}
}
Seata 安装
下载地址
- seata-server官网地址
- 下载编译:seata-server-1.2.0.tar.gz
- 下载源码:Source code(zip)
配置
- 配置nacos配置(前置条件)
- 将nacos/conf/nacos-mysql.sql导入自己的数据库
- 配置修改nacos/conf/application.properties
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.22.1.21:3306/nacos-8848?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=Developer@2019
-
启动nacos(单机版)
cd /nacos/bin
sh startup.sh -m standalone
- 下载seata
- 使用db模式导入配置:路径: seata-1.2.0\script\server\db\mysql.sql
- 配置文件:路径:seata-1.2.0\script\config-center\config.txt
- 脚本:路径:seata-1.2.0\script\config-center\nacos\nacos-config.sh
将上面的config.txt文件复制到seata目录,nacos中的nacos-config.sh nacos-config.py复制到seata的conf目录
-
初始化seata
sh nacos-config.sh -h 192.22.1.21 -p 8848 -g SEATA_GROUP -
启动 seata
nohup ./bin/seata-server.sh >log.out 2>1 &
nohup sh seata-server.sh -p 8091 -h 127.0.0.1 >log.out 2>1 &
###启动
参考
工程目录
架构中application与service的区别是什么?
- service提供了基础服务功能;application组装基础服务功能,提供给用户直接可用的业务。
- service服务粒度小、功能基础,不易发生改变;application提供上游业务功能,紧贴业务需求,容易发生改变。
- 形成service支撑application的整体架构,增加多变的application甚至不需要变动service。
springcloud-alibaba 整体父工程
├─api-common 公共包
│ ├─api-common-core 核心工具包
│ ├─api-common-auth 权限工具包
│ ├─api-common-log 日志工具包
│ └─api-common-redis redis工具包
├─xxl-job API网关,端口:9600
├─api-gateway API网关,端口:9200
├─api-system 系统管理
│ ├─api-system-common 系统管理公共业务
│ ├─api-system-auth 授权中心 端口:9202
│ └─api-system-manage 系统管理 端口:9201
├─api-transaction 分布式事务,端口:9300
├─smoke-fire-platform 智慧消防大数据平台,端口:9002
├─smart-fire-platform 智慧消防大数据平台,端口:9901
├─device-manage 设备管理,端口:(9001-9005)(跳过9002)
│ ├─device-common-module 设备公共module
│ ├─device-access 设备接入(基础信息接入,设备和CtWing平台对接),端口:9001
│ ├─device-install 设备安装(设备安装巡检),端口:9003
│ └─device-message 设备消息管理,端口:9004
├─message-manage 消息管理,端口:9006
├─publicize-manage 宣传教育,端口:9007
└─demo demo ,端口 10000
组件
- 注册中心 192.22.1.21:8500 nacos zlwl@nacos
- 分布式任务中心 192.22.1.21:9600 admin/admin@wlw
实战
Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
参考
- github地址
- 阿里巴巴解决方案之Spring Cloud Alibaba
- Spring Cloud Alibaba实战(一) - 概述
- nacos官网
- 集群参考1-Nacos集群部署说明
- 集群参考2-集群模式部署Nacos
- Nacos系列:Nacos的三种部署模式-终极版
- Spring Cloud Alibaba到底坑不坑?
- springcloud alibaba 版本说明
- 搭建生产可用的Nacos集群-nginx
- RocketMQ Example
- ruoyi-cloud-nacos-参考
- spring-cloud-alibaba版本说明
- Seata 的AT、Saga和TCC模式
- springcloud seata nacos环境搭建
- SPRINGCLOUD 集成 NACOS 1.2.1 和 SEATA 1.1.0
- MybatisPlus 组件集成-官方文档
- JasperReport生成PDF中文不显示处理