用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

rainyday123    -  云代码空间

——

(六) 构建dubbo分布式平台-maven构建config配置项目

2018-01-23|543阅||

摘要:(六) 构建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文件配置如下:
<span style="font-size: 16px;"><?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>
</span>
2. 创建log4j.properties文件,配置如下:
<span style="font-size: 16px;"># 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</span>
3. 创建ant.properties文件,内容如下:
<span style="font-size: 16px;">#--------------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
mail.username=test@163.com
mail.password=test
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=test@163.com

#-------------- 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
</span>
4. 创建mybatis-config.xml配置文件,配置如下:
<span style="font-size: 16px;"><?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>
</span>
 5. 创建spring-context.xml配置文件,配置如下:
<span style="font-size: 16px;"><?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></span>
 6 创建spring-context-shiro.xml文件,配置如下:
<span style="font-size: 16px;"><?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></span>

 7. 代码结构如下:

 

 
 欢迎大家跟我一起学习《构建dubbo分布式平台》,希望大家持续关注后面的文章!

愿意了解框架技术或者源码的朋友直接求求交流分享技术:yuncode.net
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源
顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: rainyday123
    • 等级: 初级设计师
    • 积分: 2440
    • 代码: 0 个
    • 文章: 79 篇
    • 随想: 0 条
    • 访问: 5 次
    • 关注

    人气代码

      标签

      java(2)MVC(7)教程(6)Spring(4)J2EE分布式架构(1)分布式缓存Redis(1)+mybatis(5)+shiro+(2)Activiti(2)+(4)dubbo(1)springmvc(1)mybatis(5)java企业架构(1)(十三)(2)(十四)(2)(十五)(1)kafka集群安装(1)springmvc,(1)阿里云OSS存储(1)Centos下单节点(1)Redis分布式缓存(1)单点登录集成方案,s(1)KafKa+Zook(1)JEESZ-kafk(1)spring,spr(1)mvc,web开发,(1)FastDFS安装、(1)FastDFS分布式(1)Zookeeper集(1)RestFul服务介(1)Kafka入门经典教(1)30分钟学会如何使用(1)springmvc+(1)分布式消息Kafka(1)如何实现redis缓(1)手把手Maven搭建(1)分布式服务:spri(1)Dubbo+Zook(1)分布式架构sprin(1)Springmvc+(1)j2ee分布式架构核(1)DUBBO与ZOOK(1)springmvc整(1)spring和spr(1)J2EE分布式系统环(1)MVC原理(1)Springmvc+(1)环境搭建(一)(1)入门示例讲解(1)【分享】微服务分布式(1)Springmvc+(1)(一)构建dubbo(1)(二)构建dubbo(1)(三)构建dubbo(1)(四)构建sprin(1)(五)构建sprin(1)springmvc+(1)restful(2)redis分布式缓存(1)【分享】微服务分布式(1)Springmvc+(1)(十六)(1)(十七)(1)【企业级框架整合】S(1)springmvc+(1)redis分布式缓存(1)构建springmv(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)springmvc+(1)dubbo分布式平台(1)分布式架构sprin(1)分布式服务:spri(1)Dubbo+Zook(1)dubbo分布式平台(1)dubbo分布式平台(1)【分享】微服务分布式(1)Springmvc+(1)Springmvc+(1)j2ee分布式架构核(1)构建springmv(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)DUBBO与ZOOK(1)构建dubbo分布式(1)SpringBoot(13)企业级(6)(三)(1)B2B2C(1)shop(1)beatlsql(1)(二)Spring(1)Boot配置文件详解(1)整合JPA(1)(五)企业级java(1)b2bc商城系统开源(1)(六)(1)B2B2C多租户电子(1)(四)微服务java(1)分布式环境下自动发现(1)微服务架构(2)SpringClou(1)SpringClou(1)(一)构建第一个Sp(1)(三)SpringB(1)(四)SpringB(1)(五)springb(1)(六)springb(1)企业分布式微服务云S(1)(一)服务的注册与发(1)企业分布式微服务云S(1)(二)服务消费者(r(1)企业分布式微服务云S(1)服务消费者(Feig(1)企业分布式微服务云S(1)(四)断路器(Hys(1)企业分布式微服务云S(1)(五)路由网关(zu(1)

      最新提问

        站长推荐