Netsharp配置文件
一、总体说明
netsharp下需要配置的项目一般是需要独立启动的项目,主要有四个
- netsharp-web
- netsharp-test
- netsharp-elephant
- netsharp-donkey
其实每个项目都可能独立启动,一般的项目都会有自己的测试项目,所以这个时候也需要配置文件
配置文件同源代码一起管理,开始的时候配置文件直接放在上面四个项目里,会有以下几个问题
- 多个项目配置文件经常不一致,混乱
- 每个开发人员本地的配置不一样提交代码的时候经常有人把自己的配置提交到服务器,引起代码冲突
- 每个人经常修改配置文件提交之后配置文件被改的乱七八糟
针对上面几个问题,netsharp的配置文件是放在netsharp的顶级文件夹下的conf目录,代码下载之后开发人员把配置文件复制到项目下使用,同时在git的项目设置配置文件目录为ignore即可
对于生产环境、测试环境、部署运维的时候专门把配置文件放在服务器上,部署复制一下即可,这样也保证了配置账号的安全性
二、netsharp的配置文件
netsharp目前一共用到5个配置文件,他们的目录结构如下,一下以netsharp-web项目为例
|--netsharp-web
|--src/main/java
|--src/main/resources
|--log4j.properties
|--conf
|--communication.properties
|--configuration.properties
|--database.properties
|--redis.properties
三、netsharp配置文件读取
配置文件读取比较简单,首先有一个配置文件读取的基类,主要的方法是deserialize,用于读取配置文件
1 package org.netsharp.application; 2 3 import java.lang.reflect.Field; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.netsharp.core.NetsharpException; 8 import org.netsharp.core.convertor.ITypeConvertor; 9 import org.netsharp.core.convertor.TypeConvertorFactory; 10 import org.netsharp.util.PropertyConfigurer; 11 import org.netsharp.util.ReflectManager; 12 import org.netsharp.util.StringManager; 13 14 public class ApplicationConfiguration { 15 16 public void deserialize() { 17 18 Class<?> type = this.getClass(); 19 20 Configuration confile = type.getAnnotation(Configuration.class); 21 if (confile == null) { 22 throw new NetsharpException("类上必须标注@Configuration:" + type.getName()); 23 } 24 25 PropertyConfigurer conf = PropertyConfigurer.newInstance(confile.file()); 26 27 Field[] fields = ReflectManager.getDeclaredFields(type); 28 for (Field field : fields) { 29 this.parseField(conf, field); 30 } 31 } 32 33 private void parseField(PropertyConfigurer conf, Field field) { 34 35 ConfigurationItem item = field.getAnnotation(ConfigurationItem.class); 36 if (item == null) { 37 return; 38 } 39 40 String value = conf.get(item.key()); 41 if (StringManager.isNullOrEmpty(value)) { 42 value = item.defaultValue(); 43 } 44 45 ITypeConvertor convertor = TypeConvertorFactory.create(field.getType()); 46 if (convertor == null) { 47 throw new NetsharpException("can't read " + item.key()); 48 } 49 Object propertyValue = convertor.fromString(value); 50 51 try { 52 field.setAccessible(true); 53 field.set(this, propertyValue); 54 } catch (IllegalArgumentException | IllegalAccessException e) { 55 throw new NetsharpException("can't read " + item.key(), e); 56 } 57 58 if (field.getType().isEnum() && propertyValue == null) { 59 throw new NetsharpException(item.key() + "配置不正确,配置的值为:" + value); 60 } 61 } 62 63 @Override 64 public String toString() { 65 66 Class<?> type = this.getClass(); 67 List<String> ss = new ArrayList<String>(); 68 69 Field[] fields = ReflectManager.getDeclaredFields(type); 70 for (Field field : fields) { 71 ConfigurationItem item = field.getAnnotation(ConfigurationItem.class); 72 if (item == null) { 73 continue; 74 } 75 76 try { 77 ss.add(field.getName() + " : " + field.get(this).toString()); 78 } catch (IllegalArgumentException | IllegalAccessException e) { 79 e.printStackTrace(); 80 } 81 } 82 83 return StringManager.join(StringManager.NewLine, ss); 84 } 85 }
以configuration.properties为例子,看缓存类型(org.netsharp.panda.session.cache)的读取方式,他在PandaConfiguration类中
1 package org.netsharp.panda.core; 2 3 import org.netsharp.application.ApplicationConfiguration; 4 import org.netsharp.application.Configuration; 5 import org.netsharp.application.ConfigurationItem; 6 import org.netsharp.cache.base.dict.CacheType; 7 8 @Configuration(file = "conf/configuration.properties") 9 public class PandaConfiguration extends ApplicationConfiguration { 10 11 private static PandaConfiguration instance; 12 13 /*panda是否启用权限控制*/ 14 @ConfigurationItem(key="org.netsharp.panda.ispermission",defaultValue="true") 15 private Boolean isPermission; 16 17 /*panda的session缓存类型*/ 18 @ConfigurationItem(key="org.netsharp.panda.session.cache",defaultValue="none") 19 private CacheType sessionCacheType; 20 21 //panda的session时间(分钟) 22 @ConfigurationItem(key="org.netsharp.panda.session.timeout",defaultValue="30") 23 private Integer sessionTimtout; 24 25 26 public static PandaConfiguration getInstance() { 27 28 if(instance==null) { 29 instance = new PandaConfiguration(); 30 instance.deserialize(); 31 } 32 33 return instance; 34 35 } 36 37 38 public Boolean getIsPermission() { 39 return isPermission; 40 } 41 42 public CacheType getSessionCacheType() { 43 return sessionCacheType; 44 } 45 46 47 public Integer getSessionTimtout() { 48 return sessionTimtout; 49 } 50 }
一般配置文件读取使用一个Singleton,不同类型的配置对应不同的Singleton,上文中的PandaConfiguration主要是读取关于Panda相关的配置
另外,多个配置类可以读取一个配置文件的内容,这样为了减少配置文件的数量,同时有可以达到配置文件进行分类管理的目的
在netsharp的配置文件中都标注了具体的配置文件的读取类
四、日志配置
日志文件是log4j自己读取的,不是netsharp读取的内容
文件:log4j.properties
netsharp.log.dir=/home/netsharp/logs
#=======================================================================
log4j.rootLogger=DEBUG,stdout,D,E
# appender stdout
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p [%t] %l - %m%n
log4j.appender.stdout.Encoding=UTF-8
# appender D
log4j.appender.D.Threshold=DEBUG
log4j.appender.D.File=${netsharp.log.dir}/log.txt
log4j.appender.D=org.apache.log4j.RollingFileAppender
log4j.appender.D.MaxFileSize=10MB
log4j.appender.D.ImmediateFlush=true
log4j.appender.D.Append=true
log4j.appender.D.MaxBackupIndex=10
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p %l - %m%n
log4j.appender.D.Encoding=UTF-8
# appender E
log4j.appender.E.Threshold=ERROR
log4j.appender.E.File=${netsharp.log.dir}/error.txt
log4j.appender.E=org.apache.log4j.RollingFileAppender
log4j.appender.E.MaxFileSize=10MB
log4j.appender.E.ImmediateFlush=true
log4j.appender.E.Append=true
log4j.appender.E.MaxBackupIndex=10
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p %l - %m%n
log4j.appender.E.Encoding=UTF-8
五、应用程序配置
文件:configuration.properties
#-----------------------------------------------------------------
#see : org.netsharp.application.Application
org.netsharp.name=Netsharp
org.netsharp.packages.scan=
#-----------------------------------------------------------------
#see : org.netsharp.organization.entity.AuthorizationConfiguration
#员工重置密码
org.netsharp.authorization.defaultpassword=123456
#用户密码加密盐
org.netsharp.authorization.salt=xxxxxx
#-----------------------------------------------------------------
#see : org.netsharp.panda.core.PandaConfiguration
#netsharp是否启用权限控制
org.netsharp.panda.ispermission=true
#panda的session缓存类型
org.netsharp.panda.session.cache=redis
#panda的session时间(分钟)
org.netsharp.panda.session.timeout=30
#-----------------------------------------------------------------
#see : org.netsharp.application.Application
#分布式id配置
org.netsharp.id.datacenterid=1
org.netsharp.id.workid=1
六、数据库配置
文件:database.properties
#see : org.netsharp.cache.service.redis.RedisConnection jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/netsharp?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true jdbc.username=root jdbc.database=netsharp jdbc.password=123456 jdbc.idleConnectionTestPeriod=60 jdbc.connectionTestStatement=select 1 jdbc.idleMaxAge=240 jdbc.maxConnectionsPerPartition=30 jdbc.minConnectionsPerPartition=10 jdbc.partitionCount=2 jdbc.acquireIncrement=5 jdbc.statementsCacheSize=100 jdbc.releaseHelperThreads=3 jdbc.lazyInit =false
七、RPC配置
文件:communication.properties
#base
#see : org.netsharp.communication.core.ConfigurationBase
org.netsharp.communication.host=127.0.0.1
org.netsharp.communication.port=8888
#consumer
#see : org.netsharp.communication.core.CommunicationConfiguration
org.netsharp.communication.protocol=local
#server
#see : org.netsharp.communication.protocol.tcp.container.server.TcpServerConfiguration
org.netsharp.communication.pool.coreSize=200
org.netsharp.communication.pool.maxinumSize=1000
org.netsharp.communication.pool.keepAliveTime=100
八、redis配置
文件:redis.properties
#see : org.netsharp.cache.service.redis.RedisConnection
redis.pool.maxActive=200
redis.pool.maxIdle=100
redis.pool.maxWait=3000
redis.ip=127.0.0.1
redis.port=6379
redis.password=123456
redis.timeout=5000
redis.database=1
redis.disabled=false
redis.cache.first.key=netsharp
作者 :秋时
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。