标题:
基于Dubbo框架构建分布式服务(4)
[打印本页]
作者:
look_w
时间:
2018-12-16 15:28
标题:
基于Dubbo框架构建分布式服务(4)
<
bean
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
="systemPropertiesModeName"
value
="SYSTEM_PROPERTIES_MODE_OVERRIDE"
/>
<
property
name
="ignoreResourceNotFound"
value
="true"
/>
<
property
name
="locations"
>
<
list
>
<
value
>
classpath*:jedis.properties
</
value
>
</
list
>
</
property
>
</
bean
>
<
dubbo:application
name
="chatroom-cluster-provider"
/>
<
dubbo:registry
address
="zookeeper://zk1:2181?backup=zk2:2181,zk3:2181"
/>
<
dubbo:protocol
name
="dubbo"
port
="20880"
/>
<
dubbo:service
interface
="org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService"
version
="1.0.0"
cluster
="failover"
retries
="2"
timeout
="1000"
loadbalance
="random"
actives
="100"
executes
="200"
ref
="chatRoomOnlineUserCounterService"
protocol
="dubbo"
>
<
dubbo:method
name
="queryRoomUserCount"
timeout
="500"
retries
="2"
loadbalance
="roundrobin"
actives
="50"
/>
</
dubbo:service
>
<
bean
id
="chatRoomOnlineUserCounterService"
class
="org.shirdrn.dubbo.provider.service.ChatRoomOnlineUserCounterServiceImpl"
>
<
property
name
="jedisPool"
ref
="jedisPool"
/>
</
bean
>
<
bean
id
="jedisPool"
class
="redis.clients.jedis.JedisPool"
destroy-method
="destroy"
>
<
constructor-arg
index
="0"
>
<
bean
class
="org.apache.commons.pool2.impl.GenericObjectPoolConfig"
>
<
property
name
="maxTotal"
value
="${redis.pool.maxTotal}"
/>
<
property
name
="maxIdle"
value
="${redis.pool.maxIdle}"
/>
<
property
name
="minIdle"
value
="${redis.pool.minIdle}"
/>
<
property
name
="maxWaitMillis"
value
="${redis.pool.maxWaitMillis}"
/>
<
property
name
="testOnBorrow"
value
="${redis.pool.testOnBorrow}"
/>
<
property
name
="testOnReturn"
value
="${redis.pool.testOnReturn}"
/>
<
property
name
="testWhileIdle"
value
="true"
/>
</
bean
>
</
constructor-arg
>
<
constructor-arg
index
="1"
value
="${redis.host}"
/>
<
constructor-arg
index
="2"
value
="${redis.port}"
/>
<
constructor-arg
index
="3"
value
="${redis.timeout}"
/>
</
bean
>
</
beans
>
[url=]
[/url]
上面配置中,使用dubbo协议,集群容错模式为failover,服务级别负载均衡策略为random,方法级别负载均衡策略为roundrobin(它覆盖了服务级别的配置内容),其他一些配置内容可以参考Dubbo文档。我们这里是从Redis读取数据,所以使用了Redis连接池。启动服务示例代码如下所示:
[url=]
[/url]
package
org.shirdrn.dubbo.provider;
import
org.shirdrn.dubbo.provider.common.DubboServer;
public
class
ChatRoomClusterServer {
public
static
void
main(String[] args)
throws
Exception { DubboServer.startServer(
"classpath:provider-cluster.xml"
); }
上面调用了DubboServer类的静态方法startServer,如下所示:
[url=]
[/url]
public
static
void
startServer(String config) { ClassPathXmlApplicationContext context
=
new
ClassPathXmlApplicationContext(config);
try
{ context.start(); System.in.read(); }
catch
(IOException e) { e.printStackTrace(); }
finally
{ context.close(); } }
[url=]
[/url]
方法中主要是初始化Spring IoC容器,全部对象都交由容器来管理。
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0