一、总体说明
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,用于读取配置文件
package org.netsharp.application; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import org.netsharp.core.NetsharpException;
import org.netsharp.core.convertor.ITypeConvertor;
import org.netsharp.core.convertor.TypeConvertorFactory;
import org.netsharp.util.PropertyConfigurer;
import org.netsharp.util.ReflectManager;
import org.netsharp.util.StringManager; public class ApplicationConfiguration { public void deserialize() { Class<?> type = this.getClass(); Configuration confile = type.getAnnotation(Configuration.class);
if (confile == null) {
throw new NetsharpException("类上必须标注@Configuration:" + type.getName());
} PropertyConfigurer conf = PropertyConfigurer.newInstance(confile.file()); Field[] fields = ReflectManager.getDeclaredFields(type);
for (Field field : fields) {
this.parseField(conf, field);
}
} private void parseField(PropertyConfigurer conf, Field field) { ConfigurationItem item = field.getAnnotation(ConfigurationItem.class);
if (item == null) {
return;
} String value = conf.get(item.key());
if (StringManager.isNullOrEmpty(value)) {
value = item.defaultValue();
} ITypeConvertor convertor = TypeConvertorFactory.create(field.getType());
if (convertor == null) {
throw new NetsharpException("can't read " + item.key());
}
Object propertyValue = convertor.fromString(value); try {
field.setAccessible(true);
field.set(this, propertyValue);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new NetsharpException("can't read " + item.key(), e);
} if (field.getType().isEnum() && propertyValue == null) {
throw new NetsharpException(item.key() + "配置不正确,配置的值为:" + value);
}
} @Override
public String toString() { Class<?> type = this.getClass();
List<String> ss = new ArrayList<String>(); Field[] fields = ReflectManager.getDeclaredFields(type);
for (Field field : fields) {
ConfigurationItem item = field.getAnnotation(ConfigurationItem.class);
if (item == null) {
continue;
} try {
ss.add(field.getName() + " : " + field.get(this).toString());
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
} return StringManager.join(StringManager.NewLine, ss);
}
}
以configuration.properties为例子,看缓存类型(org.netsharp.panda.session.cache)的读取方式,他在PandaConfiguration类中
package org.netsharp.panda.core; import org.netsharp.application.ApplicationConfiguration;
import org.netsharp.application.Configuration;
import org.netsharp.application.ConfigurationItem;
import org.netsharp.cache.base.dict.CacheType; @Configuration(file = "conf/configuration.properties")
public class PandaConfiguration extends ApplicationConfiguration { private static PandaConfiguration instance; /*panda是否启用权限控制*/
@ConfigurationItem(key="org.netsharp.panda.ispermission",defaultValue="true")
private Boolean isPermission; /*panda的session缓存类型*/
@ConfigurationItem(key="org.netsharp.panda.session.cache",defaultValue="none")
private CacheType sessionCacheType; //panda的session时间(分钟)
@ConfigurationItem(key="org.netsharp.panda.session.timeout",defaultValue="30")
private Integer sessionTimtout; public static PandaConfiguration getInstance() { if(instance==null) {
instance = new PandaConfiguration();
instance.deserialize();
} return instance; } public Boolean getIsPermission() {
return isPermission;
} public CacheType getSessionCacheType() {
return sessionCacheType;
} public Integer getSessionTimtout() {
return sessionTimtout;
}
}
一般配置文件读取使用一个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