(六) 构建dubbo分布式平台-maven构建config配置项目
上一篇我们介绍《构建dubbo分布式平台-maven构建ant-parent项目》,框架使用maven进行构建,根据我们的规划,要将子项目全部构建出来,今天重点讲解的是ant-config配置文件项目的构建过程。
导语: 将ant-config项目独立出来的目的是将所有的配置文件进行统一项目管理,其中包括:spring相关文件配置、mybatis相关文件配置、数据源相关文件配置、基础环境文件配置(短信、消息、oss存储、第三方登陆、邮件等)、redis或者ehcache缓存相关配置、log4j日志文件相关配置、统一error异常配置、spring-shiro权限的相关配置、spring和redis缓存集成相关配置等。
1. 创建ant-config子项目,继承ant-parent项目,eclipse的创建过程我这边省略了。pom.xml文件配置如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.sml.sz</groupId> <artifactId>ant-project</artifactId> <version>1.0.0</version> </parent> <artifactId>ant-config</artifactId> <name>ant-config</name> <url>http://maven.apache.org</url> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <!-- maven 打包后的包名 --> <finalName>ant-config</finalName> <resources> <!-- 指定 src/main/resources下所有文件及文件夹为资源文件 --> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/classes</targetPath> <includes> <include>**/*</include> </includes> <filtering>true</filtering> </resource> <!-- 根据env部署环境值,把对应环境的配置文件拷贝到classes目录 --> <resource> <directory>deployEnv/${env}</directory> <targetPath>${project.build.directory}/classes</targetPath> <filtering>true</filtering> </resource> </resources> </build> </project>
2. 创建log4j.properties文件,配置如下:
# Output pattern : date [thread] priority category - message FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7 log4j.rootLogger=WARN, Console, RollingFile #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n #RollingFile log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.RollingFile.File=/logs/ant/ant.log log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n #log4j.logger.java.sql=DEBUG #Project defalult level log4j.logger.com.sml.sz=DEBUG log4j.logger.com.sml.sz.common.security.shiro=WARN log4j.logger.com.sml.sz.JedisUtils=WARN
3. 创建ant.properties文件,内容如下:
#--------------Database sttings-------------- #mysql database setting jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/ant-dubbo?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root #pool settings jdbc.pool.init=10 jdbc.pool.minIdle=30 jdbc.pool.maxActive=60 #--------------redis settings-------------- redis.keyPrefix=ant redis.host=127.0.0.1 redis.port=6379 #-------------- System settings -------------- #\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6e logoName=ant productName=ant \u5206\u5E03\u5F0F\u4F01\u4E1A\u67B6\u6784 copyrightYear=2017 version=V1.0.0 #\u662f\u5426\u5141\u8bb8\u591a\u8d26\u53f7\u540c\u65f6\u767b\u5f55 user.multiAccountLogin=true #\u5206\u9875\u914d\u7f6e page.pageSize=10 #-------------- Framework settings -------------- #\u4f1a\u8bdd\u8d85\u65f6\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c 20m=1200000ms, 30m=1800000ms, 60m=3600000ms session.sessionTimeout=1800000 #\u4f1a\u8bdd\u6e05\u7406\u95f4\u9694\u65f6\u95f4\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c2m=120000ms\u3002 session.sessionTimeoutClean=120000 #\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84 web.view.prefix=/WEB-INF/views/ web.view.suffix=.jsp web.maxUploadSize=10485760 #\u9759\u6001\u6587\u4ef6\u540e\u7f00 web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk #--------------Email SMTP -------------- mail.host=smtp.163.com mail.port=25 [email protected] mail.password=test mail.smtp.auth=true mail.smtp.timeout=30000 [email protected] #-------------- JMS -------------- mq.brokerURL=failover\:(tcp\://127.0.0.1\:61616)?randomize\=false&initialReconnectDelay\=1000&maxReconnectDelay\=30000 mq.userName=ant mq.password=ant mq.pool.maxConnections=20 #queueName queueName.task=task_queue_1
4. 创建mybatis-config.xml配置文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否允许单条sql 返回多个数据集 --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 允许JDBC 生成主键。 --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定 MyBatis 如何自动映射 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 这是默认的执行类型 --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 设置本地缓存范围 session:就会有数据的共享 --> <setting name="localCacheScope" value="SESSION"/> <!-- 设置但JDBC类型为空时,某些驱动程序 要指定值--> <setting name="jdbcTypeForNull" value="NULL"/> </settings> <!-- 类型别名 --> <!--分页 --> <typeAliases> <typeAlias alias="Page" type="com.sml.sz.common.persistence.Page" /> </typeAliases> <!-- 插件配置 --> <plugins> <plugin interceptor="com.sml.sz.common.persistence.interceptor.PaginationInterceptor" /> </plugins> </configuration>
5. 创建spring-context.xml配置文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="true"> <description>Spring Configuration</description> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ant.properties" /> <!-- 加载应用属性实例--> <util:properties id="APP_PROP" location="classpath:ant.properties" local-override="true"/> <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 --> <context:component-scan base-package="com.sml.sz"><!-- base-package 如果多个,用“,”分隔 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- MyBatis begin --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.sml.sz"/> <property name="typeAliasesSuperType" value="com.sml.sz.common.persistence.BaseEntity"/> <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.sml.sz.*"/> <property name="annotationClass" value="com.sml.sz.common.persistence.annotation.MyBatisDao"/> </bean> <!-- 定义事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- MyBatis end --> <!-- 配置 JSR303 Bean Validator 定义 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> <!-- 缓存配置 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:${ehcache.configFile}" /> </bean> <!-- 数据源配置, 使用 BoneCP 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.init}" /> <property name="minIdle" value="${jdbc.pool.minIdle}" /> <property name="maxActive" value="${jdbc.pool.maxActive}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="${jdbc.testSql}" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat" /> </bean> </beans>
6 创建spring-context-shiro.xml文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="true"> <description>Shiro Configuration</description> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ant.properties" /> <!-- Shiro权限过滤过滤器定义 --> <bean name="shiroFilterChainDefinitions" class="java.lang.String"> <constructor-arg> <value> /static/** = anon /userfiles/** = anon ${adminPath}/login = authc ${adminPath}/logout = logout ${adminPath}/** = user /act/rest/service/** = user /ReportServer/** = user </value> </constructor-arg> </bean> <!-- 安全认证过滤器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="${adminPath}/login" /> <property name="successUrl" value="${adminPath}?login" /> <property name="filters"> <map> <entry key="authc" value-ref="formAuthenticationFilter"/> </map> </property> <property name="filterChainDefinitions"> <ref bean="shiroFilterChainDefinitions"/> </property> </bean> <!-- 定义Shiro安全管理配置 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="systemAuthorizingRealm" /> <property name="sessionManager" ref="sessionManager" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 自定义会话管理配置 --> <bean id="sessionManager" class="com.sml.sz.common.security.shiro.session.SessionManager"> <property name="sessionDAO" ref="sessionDAO"/> <!-- 会话超时时间,单位:毫秒 --> <property name="globalSessionTimeout" value="${session.sessionTimeout}"/> <!-- 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话 --> <property name="sessionValidationInterval" value="${session.sessionTimeoutClean}"/> <property name="sessionValidationSchedulerEnabled" value="true"/> <property name="sessionIdCookie" ref="sessionIdCookie"/> <property name="sessionIdCookieEnabled" value="true"/> </bean> <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg name="name" value="ant.session.id"/> </bean> <!-- 自定义Session存储容器 --> <!-- <bean id="sessionDAO" class="com.sml.sz.common.security.shiro.session.JedisSessionDAO"> <property name="sessionIdGenerator" ref="idGen" /> <property name="sessionKeyPrefix" value="${redis.keyPrefix}_session_" /> </bean> --> <bean id="sessionDAO" class="com.sml.sz.common.security.shiro.session.CacheSessionDAO"> <property name="sessionIdGenerator" ref="idGen" /> <property name="activeSessionsCacheName" value="activeSessionsCache" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 定义授权缓存管理器 --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="cacheManager"/> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- AOP式方法级权限检查 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> </beans>
7. 代码结构如下:
欢迎大家跟我一起学习《构建dubbo分布式平台》,希望大家持续关注后面的文章!