首先声明,这个框架是华为公司开发的,原理是服务端发布服务去zookeeper注册,客户端去zookeeper注册中心调用服务端发布的服务,小弟才疏学浅,只是研究了一个demo出来,供大家互相学习


框架搭建完成就是这个样子的

1、首先我们导入jar包


一个57个jar包

2、dsf.properties

############################################
# DSF Core Configuration
############################################
# dsf app name, keep unique in distributed system
dsf.appname=dsf
# dsf global service timeout in millis
dsf.service.timeout=3000
# core size for dsf handle service request thread pool 
dsf.worker.core.size=20
# max size for dsf handle service request thread pool 
dsf.worker.max.size=100
# queue size for dsf handler service request thread pool 
dsf.worker.queues=1000000


############################################
# DSF Fail Policy Configuration
############################################
# dsf request fail policy
dsf.service.failpolicy=failfast
############################################
# DSF Fail Over Policy Configuration
############################################
dsf.resend.times=3


# dsf timeout configuraion
dsf.client.request.resend.interval=1
dsf.client.request.resend.times=0


############################################
# DSF Registry Configuration
############################################
dsf.registry.name=zk
dsf.registry.default=zk
############################################
# DSF ZK Registry Configuration
############################################
dsf.registry.zk.type=zookeeper
dsf.registry.zk.connector=DsfZKClient


############################################
# DSF Protocol Configuration
############################################
dsf.protocol.name=dsf
dsf.protocol.default=dsf
############################################
# DSF TCP Protocol Configuration
############################################
# dsf tcp protocol type
dsf.protocol.tcp.type=dsf
# dsf tcp protocol listen address, this address must be server address
dsf.protocol.tcp.address=192.168.99.85:20111
# max size for dsf tcp protocol acceptor thread pool
dsf.protocol.tcp.threads=1
# queue size for dsf tcp protocol acceptor thread pool
dsf.protocol.tcp.accepts=100
# max size for dsf tcp protocol io thread pool
dsf.protocol.tcp.iothreads=20
# queue size for dsf tcp protocol io thread pool
dsf.protocol.tcp.queues=1000000
# async request waiting quque size for dsf tcp protocol
dsf.protocol.tcp.async.queues=800000
# determine whether dsf tcp protocol iplimit enabled
dsf.protocol.tcp.iplimit.enable=false
# dsf tcp protocol iplimit whiltelist 
#dsf.protocol.tcp.iplimit.whitelist=*
# dsf tcp protocol iplimit blacklist
#dsf.protocol.tcp.iplimit.blacklist=
# dsf tcp protocol heartbeat interval in seconds
dsf.protocol.tcp.heartbeat.interval=5
# dsf tcp protocol heartbeat lost count
dsf.protocol.tcp.heartbeat.lostcount=5
# max size for dsf tcp protocol client io thread pool
dsf.protocol.tcp.client.iothreads=1
# queue size for dsf tcp protocol client io thread pool
dsf.protocol.tcp.client.queues=100000
# dsf tcp protocol client reconnect interval in seconds
dsf.protocol.tcp.client.reconnect.interval=3
# dsf tcp protocol client first connect wait time  in millis
dsf.protocol.tcp.client.connect.firstwait=2000

############################################
# DSF Statistic Configuration
############################################
dsf.statistic.delay.enable=false

############################################
# DSF Monitor Configuration
############################################
# dsf time delay monitor time interval in seconds
dsf.monitor.time.interval=30

############################################
# DSF Default Route Policy
############################################
# dsf global route policy, available: poll, random, serviceDelayTimeRouter
dsf.router.default=poll
dsf.router.poll.localpriority=false
############################################
# DSF Default trace switch
############################################
dsf.trace.switch=true
dsf.sampleRate=1


dsf.logTraceSwitch=false

dsf.worker.group.size=4

dsf.protocol.tcp.client.group.size=6
dsf.log.time.queue.threshold=200
dsf.log.time.msg2buffer.threshold=300
dsf.log.time.threshold=500


#zk config
zk.server.url=192.168.99.115:2181
#usf port
rpc.address=192.168.99.115:7115


############################################
# DSF threadpool monitor config
############################################
threadpool.monitor.switch=true
# unit(s)
threadpool.monitor.interval=30
zk.auth.switch=off
authentication.enable=false


#dsf.root.dir=/dsf/wxy


3、server.dsf.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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:dsf="http://www.huawei.com/schema/dsf"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.4.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.huawei.com/schema/dsf
        http://www.huawei.com/schema/dsf/spring-dsf-1.0.xsd">


    <!-- 将服务A的实现声明为一个Spring Bean -->
    <bean id="ServiceAimpl" class="com.dsf.service.SeriviceAimpl" />


    <!-- 通过dsf:exporter 标签发布服务 -->
    <!-- name:服务名,必选。    -->
    <!-- interface:先前定义的服务接口 -->
    <!-- ref:引用上面由服务实现声明的Spring Bean-->
    <dsf:exporter name="ServiceA"
                  interface="com.dsf.service.ServiceA" ref="ServiceAimpl" />


</beans>


4、client.dsf.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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:dsf="http://www.huawei.com/schema/dsf"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.4.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.huawei.com/schema/dsf
        http://www.huawei.com/schema/dsf/spring-dsf-1.0.xsd">




    <!-- 服务导入定义 -->
    <!-- name:本地Spring框架中的Bean名称 -->
    <!-- interface: 服务接口的名称 -->
    <!-- service-name: 服务名,需要和Server端发布服务时,dsf:exporter标签中的name属性一致 -->
    <dsf:importer name="ServiceAInDsfClientAPP"
                  interface="com.dsf.service.ServiceA" service-name="ServiceA" />
</beans>


5、log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>


<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout
class="com.huawei.bme.commons.log4j.BMEPatternLayout">
<param name="ConversionPattern"
value="%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>

<appender name="SPRING"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/spring.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout
class="com.huawei.bme.commons.log4j.BMEPatternLayout">
<param name="ConversionPattern"
value="%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}|%X{fullInfo} %n" />
</layout>
</appender>


<appender name="USF"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/usf.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>


<appender name="ZK"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/zk.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>


<appender name="THREADPOOL_MONITOR"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/threadpool_monitor.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>

<appender name="SLOW_SERVICE"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/slow_service.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d|%X{moduleName}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>


<appender name="ZOOKEEPER"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/zookeeper.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} | %p | ZOOKEEPER | [%t]%m | %l%n" />
</layout>
</appender>


<appender name="REMOTING"
class="com.huawei.bme.commons.util.BMERollingFileAppender">
<param name="File" value="logs/local/remoting.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout
class="com.huawei.bme.commons.log4j.BMEPatternLayout">
<param name="ConversionPattern"
value="%t|%d|%X{level}|%X{moduleName}|%X{traceID}|%m|%X{appendInfo}|%X{exceptionMessage}| %X{fullInfo} %n" />
</layout>
</appender>
<appender name="REMOTING_NETTY"
class="com.huawei.bme.commons.util.BMERollingFileAppender">


<param name="File" value="logs/local/remoting.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%t|%d|%p|NETTY|%m| %l%n" />
</layout>
</appender>

<appender name="TRACELINK"
class="com.huawei.bme.commons.trace.util.TraceRollingFileAppender">
<param name="File" value="logs/local/tracelink.log" />
<param name="MaxFileSize" value="100MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="com.huawei.bme.commons.trace.log4j.TracePatternLayout">
            <param name="ConversionPattern"
                value="&lt;&lt;&lt;%X{traceID}|%X{calledNodeID}|%X{calledClusterId}|%X{calledIP}|%X{callingNodeID}|%X{callingIP}|%X{flag}|%X{appName}|%X{subSysName}|%X{protocol}|%X{beginTime}|%X{endTime}|%X{reqMsgSize}|%X{rspMsgSize}|%X{resultCode}|%X{resultInfo}|%X{seqNo}|%X{CallNumber}|%X{extendInfo}|%X{PlatformExtendInfo}|%X{CodeStream}&gt;&gt;&gt; %n" />
        </layout>
</appender>


<appender name="ASYNC_TRACELINK" class="com.huawei.bme.commons.trace.AsyncTraceAppender">
<param name="BufferSize" value="2048" />
<appender-ref ref="TRACELINK" />
</appender>

    <category name="TraceLink@com.huawei.bme">
<priority value="INFO" />
<appender-ref ref="TRACELINK" />
</category>

<category name="AsyncTraceLink@com.huawei.bme">
<priority value="INFO" />
<appender-ref ref="ASYNC_TRACELINK" />
</category>




<category name="org.springframework">
<priority value="ERROR" />
<appender-ref ref="SPRING" />
</category>


<category name="org.springframework.osgi ">
<priority value="ERROR" />
<appender-ref ref="SPRING" />
</category>


<category name="com.huawei.csc.usf.framework">
<priority value="ERROR" />
<appender-ref ref="USF" />
</category>

<category name="com.huawei.csc.usf.adapter.dsf">
<priority value="ERROR" />
<appender-ref ref="USF" />
</category>

<category name="com.huawei.csc.usf.registry.zk">
<priority value="DEBUG" />
<appender-ref ref="ZK" />
</category>


<category name="default_threadpool_monitor_handler">
<priority value="ERROR" />
<appender-ref ref="THREADPOOL_MONITOR" />
</category>

<category name="slowService">
<priority value="ERROR" />
<appender-ref ref="SLOW_SERVICE" />
</category>

<category name="org.apache.zookeeper">
<priority value="DEBUG" />
<appender-ref ref="ZOOKEEPER" />
</category>


<category name="com.huawei.csc.remoting" additivity="false">
<priority value="ERROR" />
<appender-ref ref="REMOTING" />
</category>


<category name="io.netty" additivity="false">
<priority value="ERROR" />
<appender-ref ref="REMOTING_NETTY" />
</category>


<root>
<priority value="INFO" />
<appender-ref ref="STDOUT" />
</root>

</log4j:configuration>

6、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           classpath*:META-INF/spring/server.dsf.xml
            classpath*:META-INF/spring/*.service.xml
        </param-value>
    </context-param>




    <listener>
        <listener-class>com.huawei.bme.container.control.ContainerContextLoaderListener</listener-class>
    </listener>




    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>


    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>
</web-app>

完整demo下载地址:http://download.csdn.net/detail/qq_34413570/9825042

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐