一.Springboot

1.Spring Boot、Spring MVC 和 Spring 有什么区别?

分别描述各自的特征:

Spring:框架就像一个家族,有众多衍生产品例如boot、security、jpa等等;但他们的基础都是Spring 的ioc和 aop,ioc提供了依赖注入的容器,aop解决了面向切面编程,然后在此两者的基础上实现了其他延伸产品的高级功能。

Spring MVC:提供了一种轻度耦合的方式来开发web应用;它是Spring的一个模块,是一个web框架;通过DispatcherServlet,ModelAndView和View Resolver,开发web应用变得很容易;解决的问题领域是网站应用程序或者服务开发——URL路由、Session、模板引擎、静态Web资源等等。

Spring Boot:实现了auto-configuration自动配置(另外三大神器actuator监控,cli命令行接口,starter依赖),降低了项目搭建的复杂度。它主要是为了解决使用Spring框架需要进行大量的配置太麻烦的问题,所以它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具;同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box)。

所以,用最简练的语言概括就是:

Spring:是一个“引擎”;

Spring MVC:是基于Spring的一个 MVC 框架;

Spring Boot:是基于Spring的条件注册的一套快速开发整合包。

springboot的启动类入口

用过springboot的技术人员很显而易见的两者之间的差别就是视觉上很直观的:springboot有自己独立的启动类(独立程序)

  1. @SpringBootApplication
  2. publicclassApplication {
  3. publicstaticvoidmain(String\[\] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }

从上面代码可以看出,Annotation定义(@SpringBootApplication)和类定义(SpringApplication.run)最为耀眼,所以要揭开SpringBoot的神秘面纱,我们要从这两位开始就可以了。

SpringBootApplication接口用到了这些注解

  1. @Target(ElementType.TYPE)// 注解的适用范围,其中TYPE用于描述类、接口(包括包注解类型)或enum声明
  2. @Retention(RetentionPolicy.RUNTIME)// 注解的生命周期,保留到class文件中(三个生命周期)
  3. @Documented// 表明这个注解应该被javadoc记录
  4. @Inherited// 子类可以继承该注解
  5. @SpringBootConfiguration// 继承了Configuration,表示当前是注解类
  6. @EnableAutoConfiguration// 开启springboot的注解功能,springboot的四大神器之一,其借助@import的帮助
  7. @ComponentScan(excludeFilters = {// 扫描路径设置
  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10. public@interfaceSpringBootApplication {
  11. ...
  12. }

架构培训文档 - 图1

在其中比较重要的有三个注解,分别是:

  1)@SpringBootConfiguration// 继承了Configuration,表示当前是注解类

  2)@EnableAutoConfiguration// 开启springboot的注解功能,springboot的四大神器之一,其借助@import的帮助

  3)@ComponentScan(excludeFilters = { //扫描路径设置(具体使用待确认)

  接下来对三个注解一一详解,增加对springbootApplication的理解:

  A)@Configuration注解

  按照原来xml配置文件的形式,在springboot中我们大多用配置类来解决配置问题

   配置bean方式的不同:

    a)xml配置文件的形式配置bean

  1. <?xml version="1.0"encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
  5. default-lazy-init="true">
  6. <!--bean定义-->
  7. </beans>

b)java configuration的配置形式配置bean

  1. @Configuration
  2. publicclassMockConfiguration{
  3. //bean定义
  4. }

  注入bean方式的不同:

    a)xml配置文件的形式注入bean

  1. <bean id="mockService">
  2. ...
  3. </bean>

   b)java configuration的配置形式注入bean

  1. @Configuration
  2. publicclassMockConfiguration{
  3. @Bean
  4. publicMockService mockService(){
  5. returnnewMockServiceImpl();
  6. }
  7. }

任何一个标注了@Bean的方法,其返回值将作为一个bean定义注册到Spring的IoC容器,方法名将默认成该bean定义的id。

  表达bean之间依赖关系的不同:

  1. xml配置文件的形式表达依赖关系
  1. <bean id="mockService">
  2.   <propery name ="dependencyService"ref="dependencyService"/>
  3. </bean>
  4. <bean id="dependencyService"></bean>

    b)java configuration配置的形式表达依赖关系(重点)

    如果一个beanA的定义依赖其他beanB,则直接调用对应的JavaConfig类中依赖beanB的创建方法就可以了。

  1. @Configuration
  2. publicclassMockConfiguration{
  3.   @Bean
  4.   publicMockService mockService(){
  5.   returnnewMockServiceImpl(dependencyService());
  6.   }
  7.   @Bean
  8.   publicDependencyService dependencyService(){
  9.   returnnewDependencyServiceImpl();
  10.   }
  11. }

  2)@ComponentScan注解

  作用:a)对应xml配置中的元素;

     b)(重点)ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义;

     c)将这些bean定义加载到IoC容器中.

  我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。

  注:所以SpringBoot的启动类最好是放在rootpackage下,因为默认不指定basePackages

3)@EnableAutoConfiguration

    此注解顾名思义是可以自动配置,所以应该是springboot中最为重要的注解。

    在spring框架中就提供了各种以@Enable开头的注解,例如:@EnableScheduling、@EnableCaching、@EnableMBeanExport等;@EnableAutoConfiguration的理念和做事方式其实一脉相承简单概括一下就是,借助@Import的支持,收集和注册特定场景相关的bean定义。 

  @EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器【定时任务、时间调度任务】

  @EnableMBeanExport是通过@Import将JMX相关的bean定义加载到IoC容器【监控JVM运行时状态】

    @EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器。

     @EnableAutoConfiguration作为一个复合Annotation,其自身定义关键信息如下:

  1. @SuppressWarnings("deprecation")
  2. @Target(ElementType.TYPE)
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Inherited
  6. @AutoConfigurationPackage【重点注解】
  7. @Import(AutoConfigurationImportSelector.class)【重点注解】
  8. public@interfaceEnableAutoConfiguration {
  9. ...
  10. }

其中最重要的两个注解已经标注:

  1. @AutoConfigurationPackage【重点注解】
  2. @Import(AutoConfigurationImportSelector.class)【重点注解】

  当然还有其中比较重要的一个类就是:

  1. AutoConfigurationImportSelector.class
  2. AutoConfigurationPackage注解:
  3. @Target(ElementType.TYPE)
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. @Inherited
  7. @Import(AutoConfigurationPackages.Registrar.class)
  8. public@interfaceAutoConfigurationPackage {
  9. }
  10. [通过@Import(AutoConfigurationPackages.Registrar.class)](mailto:通过@Import(AutoConfigurationPackages.Registrar.class))
  11. Static classRegistrarimplementsImportBeanDefinitionRegistrar, DeterminableImports {
  12. @Override
  13. publicvoidregisterBeanDefinitions(AnnotationMetadata metadata,
  14. BeanDefinitionRegistry registry) {
  15. register(registry,newPackageImport(metadata).getPackageName());
  16. }
  17. ……
  18. }

注册当前启动类的根package;

注册org.springframework.boot.autoconfigure.AutoConfigurationPackages的BeanDefinition。

项目中注解

@Service用于标注业务层组件

@Controller用于标注控制层组件,项目中使用@RestController

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

  1. @Service
  2. public class VentorServiceImpl implements iVentorService {
  3. }
  1. @Repository
  2. public class DaoSupport implements Dao {
  3. }

4.以下是easyloan-system的启动

  1. @EnableSwagger2
  2. @EnableEurekaClient
  3. @EnableFeignClients(basePackages = {"com.git.easyloan"})
  4. @EnableTransactionManagement
  5. @ImportResource(value = "rules/ruleEngine\_beans.xml")
  6. @ComponentScan(basePackages = {"com.git.easyloan", "javacommon.coreframe"})
  7. @EnableAsync
  8. @SpringBootApplication
  9. @EnableCaching
  10. public class Application {
  11. public static void main(String\[\] args) {
  12. Class<?>\[\] classes = {Application.class, ApplicationContextHolder.class, SecurityContextHolder.class};
  13. SpringApplication springApplication = new SpringApplication(classes);
  14. springApplication.addListeners(new StartUpListener());
  15. springApplication.run(args);
  16. //初始化线程池
  17. PoolHelpUtil poolHelpUtil = new PoolHelpUtilImpl();
  18. try {
  19. ThreadPool pool = ThreadPool.getInstance(poolHelpUtil);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }

平台介绍

1.平台架构

架构培训文档 - 图2

2.应用架构

架构培训文档 - 图3

3.技术架构

架构培训文档 - 图4

子系统介绍

架构培训文档 - 图5

架构培训文档 - 图6

架构培训文档 - 图7

项目依赖结构

架构培训文档 - 图8

  1. Easyloan-dependencies:系统依赖包模块

system-dependencies…… pricing-dependencies:各个子系统的依赖,都统一依赖easyloan-dependencies

  1. Easyloan-config:配置中心模块,和dependencies类似。System-config……pricing-configg各个子系统的配置模块,管理存放各自系统的启动参数。统一依赖easyloan-config
  2. Easyloan-commons:公共依赖包,存放公共业务程序
  3. Easyloan-coreframe-core: 数据库dao封装层
  4. Easy-commons:底层公共包,pageData就在此包
  5. Easytools-all:三方工具包,文件操作,io操作,日期操作等工具类。
  6. Easy-etl:多线程封装
  7. Easy-job-core:日终调度

Config中的yml配置文件

  1. spring:
  2. datasource:
  3. druid:
  4. stat-view-servlet:
  5. url-pattern: /druid/\*
  6. login-username: admin
  7. login-password: admin
  8. enabled: true
  9. reset-enable: true
  10. allow:
  11. web-stat-filter:
  12. url-pattern: /\*
  13. exclusions: "\*.js,\*.gif,\*.jpg,\*.png,\*.css,\*.ico,/druid/\*"
  14. enabled: true
  15. filter:
  16. stat:
  17. slow-sql-millis: 1000
  18. log-slow-sql: true
  19. enabled: true
  20. connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  21. master:
  22. DbType: com.alibaba.druid.pool.DruidDataSource
  23. driver-class-name: oracle.jdbc.driver.OracleDriver
  24. url: jdbc:oracle:thin:@dbIP:dbPort/loandb
  25. username: username
  26. password: password
  27. initialSize: 50
  28. maxActive: 100
  29. filters: stat
  30. minIdle: 5
  31. maxWait: 60000
  32. poolPreparedStatements: true
  33. maxPoolPreparedStatementPerConnectionSize: 20
  34. ValidationQuery: select 'x' from dual
  35. ValidationQueryTimeout: 3000
  36. testOnBorrow: false
  37. testOnReturn: false
  38. testWhileIdle: true
  39. timeBetweenEvictionRunsMillis: 60000
  40. minEvictableIdleTimeMillis: 300000
  41. maxOpenPreparedStatements: 20
  42. removeAbandoned: true
  43. removeAbandonedTimeout: 1800
  44. logAbandoned: true
  45. slave:
  46. DbType: com.alibaba.druid.pool.DruidDataSource
  47. driver-class-name: oracle.jdbc.driver.OracleDriver
  48. url: jdbc:oracle:thin:@dbIP:dbPort/loandb
  49. username: username
  50. password: password
  51. initialSize: 50
  52. maxActive: 100
  53. filters: stat
  54. minIdle: 5
  55. maxWait: 60000
  56. poolPreparedStatements: true
  57. maxPoolPreparedStatementPerConnectionSize: 20
  58. ValidationQuery: select 'x' from dual
  59. ValidationQueryTimeout: 3000
  60. testOnBorrow: false
  61. testOnReturn: false
  62. testWhileIdle: true
  63. timeBetweenEvictionRunsMillis: 60000
  64. minEvictableIdleTimeMillis: 300000
  65. maxOpenPreparedStatements: 20
  66. removeAbandoned: true
  67. removeAbandonedTimeout: 1800
  68. logAbandoned: true
  69. servlet:
  70. multipart:
  71. max-file-size: 50MB
  72. max-request-size: 100MB
  73. redis:
  74. cluster:
  75. nodes:ip1:port1,ip2:port2,ip3:port3,ip4:port4,ip5:port5,ip6:port6
  76. maxAttempts: 20
  77. max-redirects: 3
  78. timeout: 2500
  79. password:
  80. lettuce:
  81. pool:
  82. max-active: 2000
  83. max-idle: 2000
  84. min-idle: 0
  85. zipkin:
  86. base-url: http://zipkinIp:9411
  87. mapper:
  88. mappers:
  89. \- tk.mybatis.mapper.common.Mapper
  90. notEmpty: true
  91. logging:
  92. config: classpath:my-logback-spring.xml
  93. log:
  94. path: /app/tomcat/scms/logs
  95. level: INFO
  96. size: 5GB
  97. sql: debug
  98. init:
  99. params: params-cms-graylist@TbCmnGraylistMapper.initAll,params-cms-AftTipsConfig@TbCmnAftTipsConfigMapper.listAll
  100. ribbon:
  101. ReadTimeout: 70000
  102. ConnectTimeout: 70000
  103. eureka:
  104. enabled: true
  105. #jwt
  106. jwt:
  107. header: Authorization
  108. secret: mySecret
  109. \# token 过期时间 2个小时
  110. expiration: 360000000
  111. auth:
  112. \# 授权路径
  113. path: /api/v1/system/login
  114. \# 获取用户信息
  115. account: /api/v1/system/login
  116. feign:
  117. hystrix:
  118. enabled: false
  119. httpclient:
  120. connection-timeout: 70000
  121. max-connections-per-route: 1000
  122. max-connections: 3000
  123. enabled: true
  124. client:
  125. default-config: default
  126. gateway:
  127. url:
  128. system:
  129. credit:
  130. loan:
  131. customer:
  132. riskwarn:
  133. aplus:
  134. archives:
  135. asset:
  136. badassets:
  137. billSystem:
  138. classify:
  139. dataApplication:
  140. rating:
  141. mobile:
  142. afterloan:
  143. server:
  144. name:
  145. system: EASYLOAN-SYSTEM
  146. credit: EASYLOAN-CREDIT
  147. loan: EASYLOAN-LOAN
  148. customer: EASYLOAN-CUSTOMER
  149. riskwarn: EASYLOAN-RISKWARN
  150. aplus: EASYLOAN-APLUS
  151. archives: EASYLOAN-ARCHIVES
  152. asset: EASYLOAN-ASSET
  153. badassets: EASYLOAN-BADASSETS
  154. billSystem: EASYLOAN-BILLSYSTEM
  155. classify: EASYLOAN-CLASSIFY
  156. dataApplication: EASYLOAN-DATAAPPLICATION
  157. rating: EASYLOAN-RATING
  158. mobile: EASYLOAN-MOBILE
  159. afterloan: EASYLOAN-AFTERLOAN
  160. tomcat:
  161. max-threads: 2000
  162. max-connections: 2000
  163. min-spare-threads: 10
  164. connection-timeout: 70000
  165. compression:
  166. mime-types: application/json,application/xml,text/html,text/xml,text/plain
  167. enabled: true
  168. min-response-size: 512
  169. \# 规则引擎外置目录配置
  170. rule:
  171. basePath: /app/tomcat/scms/easyloan/rules/system
  172. #upload:
  173. \# epaaPath: /app/scms/datafile/recv/epaa/
  174. snowflake:
  175. data: 0
  176. bizType: order\_id\_
  177. easy:
  178. job:
  179. admin:
  180. addresses: http://jobIP:9090
  181. \### easy-job executor address
  182. executor:
  183. appname: easy-job-executor-system
  184. ip:
  185. port: 9980
  186. \### easy-job log path
  187. logpath: /app/tomcat/deploy/log/jobhandler
  188. \### easy-job log retention days
  189. logretentiondays: -1
  190. \### easy-job, access token
  191. accessToken:
  192. writeList: /app/tomcat/scms/easyloan/writeList.properties
  193. properties:
  194. path: /app/tomcat/scms/easyloan/dataSource.properties
  195. \# resTemplate
  196. resT:
  197. maxtotal: 5000
  198. maxper: 2000
  199. readTimeOut: 55000
  200. connectTimeout: 55000
  201. connectionRequestTimeout: 30000
  202. validateAfterInactivity: 100000
  203. transule:
  204. rulePath: /app/tomcat/scms/easyloan/rule.properties
  205. repeatFlag: 3
  206. service:
  207. list:
  208. map:
  209. jwtWriteUrl: /tbPubOperaLog/saveTransLog