soul源码学习-20210114
发布日期:2021-06-28 18:55:00 浏览次数:3 分类:技术文章

本文共 8535 字,大约阅读时间需要 28 分钟。

目标:

  • 运行soul-admin工程日志分析
  • 运行soul-bootstrap工程日志分析
  • 思考
  • 总结

 

1.运行soul-admin工程日志分析

1.1在配置了mysql地址和用户名、密码后,启动成功。在启动日志中会发现有如下信息:

Thu Jan 14 14:46:36 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

提示信息说明的很清楚,在mysql 5.7版本数据库链接字符串中需要添加ssl的设置。在配置文件中找到相应的配置并添加。如下所示:

 

datasource:  url: jdbc:mysql://localhost:3306/soul?useUnicode=true&characterEncoding=utf-8&useSSL=false  username: root  password: 123456  driver-class-name: com.mysql.jdbc.Driver

再次启动后没有提示了。

PS:在mysql 5.7 任何数据库连接最好添加这个参数设置

 

1.2初始化数据库:

2021-01-14 14:55:24.289 INFO 14844 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1654 ms

2021-01-14 14:55:24.702 INFO 14844 --- [ main] o.d.s.a.s.init.LocalDataSourceLoader : execute soul schema sql: META-INF/schema.sql

 

开始数据库中并没有创建soul要使用库和表,所以这里日志中打印出LocalDataSourceLoader中会执行schema.sql文件。每次启动都会去执行这个脚本。脚本中的执行语句是进行了判断,如果存在就不执行。但是需要注意:脚本中的insert 语句执行结果不会插入数据,数据库会反馈

Duplicate entry '1' for key 'PRIMARY'

1.3AutowiredAnnotationBeanPostProcessor:

接下来日志中大量的出现AutowiredAnnotationBeanPostProcessor

2021-01-14 14:55:25.285 INFO 14844 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : Inconsistent constructor declaration on bean with name 'appAuthController': single autowire-marked constructor flagged as optional - this constructor is effectively required since there is no default constructor to fall back to: public org.dromara.soul.admin.controller.AppAuthController(org.dromara.soul.admin.service.AppAuthService)

根据这段日志说明,名为“appAuthController”的bean上的构造函数声明不一致:单个autowire标记的构造函数被标记为可选的-此构造函数实际上是必需的,因为没有可回退到的默认构造函数。

public class AppAuthController {    private final AppAuthService appAuthService;    /**     * Instantiates a new App auth controller.     *     * @param appAuthService the app auth service     */    @Autowired(required = false)    public AppAuthController(final AppAuthService appAuthService) {        this.appAuthService = appAuthService;

 

这样写的目的是什么?和以下代码的区别是什么?

@Autowiredprivate AppAuthService appAuthService;

 

1.4 使用HikariPool作为数据连接池

2021-01-14 14:55:25.827 INFO 14844 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

2021-01-14 14:55:25.842 INFO 14844 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.

 

这里使用了spring boot 默认的数据库连接池

 

1.5 工程中使用了actuator插件

2021-01-14 14:55:26.262 INFO 14844 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'

说明soul-admin开启了actuator监控。actuator的配置在哪里呢?

 

1.6 工程启动了swagger 在线文档

2021-01-14 14:55:26.440 INFO 14844 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]

访问 可以查看API情况。系统没有登录的情况下也可以访问到swagger。如果正式环境部署的话,会存在安全问题。正式环境需要将swagger屏蔽。

 

1.7 加载欢迎界面

2021-01-14 14:55:26.684 INFO 14844 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]

 

1.8 启动完成

2021-01-14 14:55:27.375 INFO 14844 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9095 (http) with context path ''

 

2.运行soul-bootstrap工程日志分析

2.1 打印soul的logo

2021-01-14 23:02:33.998 INFO 22028 --- [ main] org.dromara.soul.web.logo.SoulLogo :

这个类实现了ApplicationListener接口。

@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)@Slf4jpublic class SoulLogo implements ApplicationListener
{ private static final String SOUL_LOGO = "\n" + " _ \n" + " | | \n" + " ___ ___ _ _| | \n" + "/ __|/ _ \\| | | | |\n" + "\\__ \\ (_) | |_| | |\n" + "|___/\\___/ \\__,_|_|\n" + " \n" + " \n"; private final AtomicBoolean alreadyLog = new AtomicBoolean(false); @Override public void onApplicationEvent(final ApplicationEnvironmentPreparedEvent event) { if (!alreadyLog.compareAndSet(false, true)) { return; } log.info(buildBannerText());

 

在这里实现了Logo的打印。这里提供给我们一个自定义logo的方式。

 

2.2 加载插件

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[global] [org.dromara.soul.plugin.global.GlobalPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[sign] [org.dromara.soul.plugin.sign.SignPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[waf] [org.dromara.soul.plugin.waf.WafPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[rate_limiter] [org.dromara.soul.plugin.ratelimiter.RateLimiterPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[hystrix] [org.dromara.soul.plugin.hystrix.HystrixPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[resilience4j] [org.dromara.soul.plugin.resilience4j.Resilience4JPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[divide] [org.dromara.soul.plugin.divide.DividePlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[webClient] [org.dromara.soul.plugin.httpclient.WebClientPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[divide] [org.dromara.soul.plugin.divide.websocket.WebSocketPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[alibaba-dubbo-body-param] [org.dromara.soul.plugin.alibaba.dubbo.param.BodyParamPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[dubbo] [org.dromara.soul.plugin.alibaba.dubbo.AlibabaDubboPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[monitor] [org.dromara.soul.plugin.monitor.MonitorPlugin]

2021-01-14 23:02:36.638 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[response] [org.dromara.soul.plugin.httpclient.response.WebClientResponsePlugin]

2021-01-14 23:02:36.639 INFO 22028 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[response] [org.dromara.soul.plugin.alibaba.dubbo.response.DubboResponsePlugin]

 

这里在第一次浏览不知道是如何加载的~~

 

2.3 使用websocket 去和soul-admin进行连接

2021-01-14 23:03:18.902 INFO 22028 --- [ocket-connect-1] o.d.s.p.s.d.w.WebsocketSyncDataService : websocket reconnect is successful.....

 

为什么使用websocket去连接admin。设计思想是什么?

 

2.4 工程中使用了actuator插件

2021-01-14 23:02:38.902 INFO 22028 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'

 

这里通过连接无法访问actuator。 当设置成true 时,需要连接redis 。没有redis 的话会出现异常。PS: 在浏览器上访问 实际上会转发给配置的服务,然后把后端服务的信息返回。我启动后没有配置后端服务,返回的信息如下:

{"code":-107,"message":"Can not find selector, please check your configuration!","data":null}

日志显示:

2021-01-14 23:28:11.471 ERROR 13280 --- [-work-threads-1] o.d.soul.plugin.base.utils.CheckUtils : can not match selector data: divide

 

2.5 netty 服务启动成功

2021-01-14 23:02:39.734 INFO 22028 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 9195

这里信息说明spring boot 使用netty 方式启动的。一般没有配置默认使用的是tomcat 作为容器启动的。

 

3.思考:

  • 对于一些需要提前初始化或者实例化的内容。会在启动的时候先加载。而且加载是有顺序性的。这部分需要我们对spring boot 启动后加载对象的生命周期有了解才能更好使用。
  • spring boot actuator 是个很好的东西。可以监控自身以及服务器信息。使用的时候需要注意权限控制,需要考虑安全性。有些信息正式环境是不能让获取到的,但是内部监控系统又需要这些数据。可以使用IP白名单、拦截器等方式做。更优雅的方式暂时还没有想到。
  • swagger 也同样有安全性问题。开发、测试环境用处很大。上到正式环境的时候可以动态修改扫描包的方式屏蔽接口,从而使正式环境无法访问。
  • 如何打印出自己的Logo。在这里有了实现,以后自己的项目如果需要可以参考实现方式
  • bootstrap是一个启动工程。启动后如何加载其他组件,如何做到优雅的加载组件是非常值得学习的。在这次学习中会加强学习,对公司中间层的设计有很大帮助。
  • bootstrap作为网关需要满足将后端服务器指标进行转化。这就是为什么在网关上访问actuator实际上是在做转发,而不是返回网关监控信息。网关自身的监控信息应该可以从其他方式获取到。
  • 看到了sprint boot 以netty 方式启动。通常spring boot 默认启动方式是tomcat 。对于spring boot 启动容器多了一点认识。

 

4.总结:

  • 仔细阅读了admin 和 bootstrap 启动日志,很粗的了解了启动加载信息。
  • 看到了一些类使用构造函数方式注入,但是没有想到这样的优势。
  • 可以使用actuator来获取自身应用以及服务器信息。为监控系统提供信息员。
  • 通过使用ApplicationListener接口来实现自定义Logo.
  • 看到了sprint boot 以netty 方式启动。

转载地址:https://blog.csdn.net/xt00002003/article/details/112645858 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:编程语言介绍
下一篇:学习笔记2021-01-13

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月02日 13时08分33秒