# Sentinel 控制台
> 源码地址: https://github.com/alibaba/Sentinel
### 一、前言
本文将修改`sentinel-dashboard`源码,集成`mybatis-plus`,持久化配置到`mysql`,然后制作docker版的sentinel镜像,方便后期快速部署sentinel-mysql。
1. Docker version 20.10.8, build 3967b7d
2. docker-compose version 1.29.2, build 5becea4c
3. sentinel-dashboard:1.8.2
4. mybatis-plus-boot-starter:3.4.3.4
5. mysql5.7
### 二、docker-compose快速部署Sentinel - MySQL版
> 可参考 [https://gitee.com/zhengqingya/docker-compose](https://gitee.com/zhengqingya/docker-compose)
```shell
# 准备
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx
# 运行
docker-compose -f docker-compose-sentinel-mysql.yml -p sentinel up -d
```
### 三、访问测试
访问 [http://127.0.0.1:8858/#/dashboard](http://127.0.0.1:8858/#/dashboard)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7874ae846cf244d58e79b971ea530075.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YOR5riF,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/c3407fc23c9b454fa72f13552f4b0835.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YOR5riF,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/4431c1af97c740448e4e55f5e94c0976.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YOR5riF,size_20,color_FFFFFF,t_70,g_se,x_16)
如果sentinel日志报错如下
```
2021-10-03 13:05:47.493 ERROR 1 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher : Failed to fetch metric from <http://192.168.101.88:8719/metric?startTime=1633237412000&endTime=1633237418000&refetch=false> (ConnectionException: Connection timed out)
```
解决:让sentinel所在机器能够访问sentinel客户端ip和端口,即上面日志中的`192.168.101.88:8719`
![在这里插入图片描述](https://img-blog.csdnimg.cn/4adbd00e87e645aa90fc9ed19299afb5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YOR5riF,size_20,color_FFFFFF,t_70,g_se,x_16)
### 四、sentinel-dashboard源码修改
sentinel源码下载 [https://github.com/alibaba/Sentinel](https://github.com/alibaba/Sentinel)
进入`sentinel-dashboard`模块
![在这里插入图片描述](https://img-blog.csdnimg.cn/dfa343b07fad47f9860b73fab941699a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YOR5riF,size_20,color_FFFFFF,t_70,g_se,x_16)
#### 1、新增如下依赖
```xml
<!-- ========================= 数据库相关 ========================== -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- mybatis-plus -->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<!-- lombok插件 -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
```
#### 2、application.yml配置
> 温馨小提示:源码对应为`application.properties`文件,被我修改为`application.yml`了
> mysql相关信息自行修改即可
```yml
auth:
# auth settings
filter:
exclude-url-suffixes: htm,html,js,css,map,ico,ttf,woff,png
exclude-urls: /,/auth/login,/auth/logout,/registry/machine,/version
# If auth.enabled=false, Sentinel console disable login
password: sentinel
username: sentinel
# logging settings
logging:
file: ${user.home}/logs/csp/sentinel-dashboard.log
level:
org:
springframework:
web: INFO
pattern:
file: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'
# console: %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
# Inject the dashboard version. It's required to enable
# filtering in pom.xml for this resource file.
sentinel:
dashboard:
version: '@project.version@'
# cookie name setting
server:
servlet:
session:
cookie:
name: sentinel_dashboard_cookie
port: 8858
# mysql setting
mysql:
host: 127.0.0.1
port: 3306
username: root
password: root
db-name: sentinel
# spring settings
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
# 配置数据源
datasource:
url: jdbc:mysql://${mysql.host}:${mysql.port}/${mysql.db-name}?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否进行SSL连接 解决则加上 &useSSL=false
username: ${mysql.username}
password: ${mysql.password}
driver-class-name: com.mysql.jdbc.Driver
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:**/*Mapper.xml
# 实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.alibaba.csp.sentinel.dashboard.mysql.entity
# 以下配置均有默认值,可以不设置
global-config:
# 关闭MP3.0+自带的banner
banner: false
db-config:
# 主键类型 0:"数据库ID自增", 1:"不操作", 2:"用户输入ID",3:"数字型snowflake", 4:"全局唯一ID UUID", 5:"字符串型snowflake";
id-type: auto
# 字段策略
insert-strategy: not_null
update-strategy: not_null
select-strategy: not_null
# 驼峰下划线转换
table-underline: true
# 逻辑删除配置
logic-delete-field: isDeleted # 全局逻辑删除的实体字段名
logic-delete-value: 1 # 逻辑删除全局值(1表示已删除,默认为 1)
logic-not-delete-value: 0 # 逻辑未删除全局值(0表示未删除,默认为 0)
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
cache-enabled: false
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 解决oracle更新数据为null时无法转换报错,mysql不会出现此情况
jdbc-type-for-null: 'null'
```
#### 3、数据库表准备
新建数据库`sentinel-dashboard` & 新建表`sentinel-dashboard`
```sql
CREATE TABLE `t_sentinel_metric`
(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id,主键',
`gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
`gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
`app` varchar(100) DEFAULT NULL COMMENT '应用名称',
`timestamp` datetime DEFAULT NULL COMMENT '统计时间',
`resource` varchar(500) DEFAULT NULL COMMENT '资源名称',
`pass_qps` int(11) DEFAULT NULL COMMENT '通过qps',
`success_qps` int(11) DEFAULT NULL COMMENT '成功qps',
`block_qps` int(11) DEFAULT NULL CO