dubbo 用戶指南: http://dubbo.io/User+Guide-zh.htm 開發指南:http://dubbo.io/Developer+Guide-zh.htm#DeveloperGuide-zh-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86java
由於dubbo和zkclient的日誌默認使用的是log4j,與咱們系統使用的logback不一致, dubbo使用的spring2.5.6與咱們系統使用的sping4也不一致,因此須要去掉他們默認的,改爲咱們本身系統一致的。spring
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>
2.5
.
3
</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>
0.9
</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>
1.7
.
25
</version>
</dependency>
|
原game-service子模塊中service全部接口類放入新game-service子模塊中,原game-service子模塊中service.impl和component等包放入game-provider子模塊中mvc
修改後的game-service做爲接口的定義,game-provider做爲提供者實現接口。app
項目拆分以後消費者模塊須要修改爲只引入game-service子模塊maven
一、在game-provider子模塊pom.xml引入game-service子模塊和原game-service項目maven包ide
二、在game-provider子模塊src/main/resource下建立 dubbo_provider.properties 文件ui
# 提供方應用信息,用於計算依賴關係
dubbo.application.name=game-provider
dubbo.application.logger=slf4j
# 聲明須要暴露的服務接口所在的包
dubbo.annotation.
package
=com.xunleijr.game
# 用dubbo協議在
20880
端口暴露服務
dubbo.protocol.name=dubbo
dubbo.protocol.port=
20880
dubbo.protocol.accessLog=
true
dubbo.provider.timeout=
3000
dubbo.provider.retries=
1
dubbo.provider.delay=-
1
# 使用zookeeper註冊中心暴露服務地址
dubbo.registr.protocol=zookeeper
dubbo.registry.address.production=
dubbo.registry.address.quasi_production=
10.26
.
91.214
:
2181
dubbo.registry.address.test=
192.168
.
20.36
:
2181
dubbo.registry.register=
true
dubbo.registry.subscribe=
true
|
三、建立Dubbo提供者配置類this
package
com.xunleijr.game.config;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.PropertySource;
import
org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import
com.alibaba.dubbo.config.ApplicationConfig;
import
com.alibaba.dubbo.config.ProtocolConfig;
import
com.alibaba.dubbo.config.ProviderConfig;
import
com.alibaba.dubbo.config.RegistryConfig;
import
com.xunleijr.game.annotation.AnnotationBean;
import
com.xunleijr.game.common.AppConstant;
@Configuration
@PropertySource
(value =
"classpath:dubbo_provider.properties"
)
public
class
DubboProviderConfig {
@Value
(
"${dubbo.application.name}"
)
private
String applicationName;
@Value
(
"${dubbo.registr.protocol}"
)
private
String protocol;
@Value
(
"${dubbo.registry.address.production}"
)
private
String registryAddress_production;
@Value
(
"${dubbo.registry.address.quasi_production}"
)
private
String registryAddress_quasiProduction;
@Value
(
"${dubbo.registry.address.test}"
)
private
String registryAddress_test;
@Value
(
"${dubbo.protocol.name}"
)
private
String protocolName;
@Value
(
"${dubbo.protocol.port:20880}"
)
private
int
protocolPort;
@Value
(
"${dubbo.provider.timeout:3000}"
)
private
int
timeout;
@Value
(
"${dubbo.provider.retries:1}"
)
private
int
retries;
@Value
(
"${dubbo.provider.delay:1}"
)
private
int
delay;
@Bean
public
static
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return
new
PropertySourcesPlaceholderConfigurer();
}
/**
* 設置dubbo掃描包
*
* @param packageName
* @return
*/
@Bean
public
static
AnnotationBean annotationBean(
@Value
(
"${dubbo.annotation.package}"
) String packageName) {
AnnotationBean annotationBean =
new
AnnotationBean();
annotationBean.setPackage(packageName);
return
annotationBean;
}
/**
* 注入dubbo上下文
*
* @return
*/
@Bean
public
ApplicationConfig applicationConfig() {
// 當前應用配置
ApplicationConfig applicationConfig =
new
ApplicationConfig();
applicationConfig.setName(
this
.applicationName);
return
applicationConfig;
}
/**
* 注入dubbo註冊中心配置,基於zookeeper
*
* @return
*/
@Bean
public
RegistryConfig registryConfig() {
// 鏈接註冊中心配置
RegistryConfig registry =
new
RegistryConfig();
registry.setProtocol(protocol);
String registryAddress =
null
;
switch
(AppConstant.CURR_SYSTEM_ENVIRONMENT) {
case
PRODUCTION_ENVIRONMENT:
registryAddress = registryAddress_production;
break
;
case
QUASI_PRODUCTION_ENVIRONMENT:
registryAddress = registryAddress_quasiProduction;
break
;
case
TEST_ENVIRONMENT:
registryAddress = registryAddress_test;
break
;
}
registry.setAddress(registryAddress);
System.out.println(
"########### registry Config 【"
+ protocol +
":"
+ registryAddress +
"】"
);
return
registry;
}
/**
* 默認基於dubbo協議提供服務
*
* @return
*/
@Bean
public
ProtocolConfig protocolConfig() {
// 服務提供者協議配置
ProtocolConfig protocolConfig =
new
ProtocolConfig();
protocolConfig.setName(protocolName);
protocolConfig.setPort(protocolPort);
protocolConfig.setThreads(
200
);
System.out.println(
"########### protocol config【"
+ protocolName +
":"
+ protocolPort +
"】"
);
return
protocolConfig;
}
/**
* dubbo服務提供
*
* @param applicationConfig
* @param registryConfig
* @param protocolConfig
* @return
*/
@Bean
(name =
"defaultProvider"
)
public
ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig,
ProtocolConfig protocolConfig) {
ProviderConfig providerConfig =
new
ProviderConfig();
providerConfig.setTimeout(timeout);
providerConfig.setRetries(retries);
providerConfig.setDelay(delay);
providerConfig.setApplication(applicationConfig);
providerConfig.setRegistry(registryConfig);
providerConfig.setProtocol(protocolConfig);
System.out.println(
"########### provider init..."
);
return
providerConfig;
}
}
|
四、修改接口實現類的@Service註解爲@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0")spa
五、將game-model子模塊中除protocol buffer相關類以外的類實現java.io.Serializable接口日誌
六、建立服務提供者啓動類
package
com.xunleijr.game;
import
java.io.IOException;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import
com.xunleijr.game.config.DubboProviderConfig;
import
com.xunleijr.game.config.MainConfig;
/**
* 服務提供者啓動類
* @author Yixi
*
*/
public
class
ProviderMain {
private
static
Logger logger = LoggerFactory.getLogger(ProviderMain.
class
);
@SuppressWarnings
(
"resource"
)
public
static
void
main(String[] args) {
AnnotationConfigApplicationContext ctx =
new
AnnotationConfigApplicationContext();
ctx.register(MainConfig.
class
);
ctx.register(DubboProviderConfig.
class
);
ctx.refresh();
ctx.registerShutdownHook();
// 在JVM註冊一個關閉鉤子,確保IOC容器最終會被正確關閉
ctx.start();
logger.info(
"dubbo provider start..."
);
try
{
// 輸入任意字符退出
System.in.read();
}
catch
(IOException e) {}
}
}
|
4、服務消費者實現
一、消費者src/main/resources下建立dubbo_consumer.properties文件
dubbo.application.name=game-***-consumer
dubbo.application.logger=slf4j
dubbo.annotation.
package
=com.xunleijr.game
dubbo.registr.protocol=zookeeper
dubbo.registry.address.production=
dubbo.registry.address.quasi_production=
10.26
.
91.214
:
2181
dubbo.registry.address.test=
192.168
.
20.36
:
2181
dubbo.registry.register=
true
dubbo.registry.subscribe=
true
|
二、建立Dubbo消費者配置類
package
com.xunleijr.game.config;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.PropertySource;
import
org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import
com.alibaba.dubbo.config.ApplicationConfig;
import
com.alibaba.dubbo.config.RegistryConfig;
import
com.alibaba.dubbo.config.spring.AnnotationBean;
import
com.xunleijr.game.common.AppConstant;
@Configuration
@PropertySource
(value =
"classpath:dubbo_consumer.properties"
)
public
class
DubboConsumerConfig {
@Value
(
"${dubbo.application.name}"
)
private
String applicationName;
@Value
(
"${dubbo.registr.protocol}"
)
private
String protocol;
@Value
(
"${dubbo.registry.address.production}"
)
private
String registryAddress_production;
@Value
(
"${dubbo.registry.address.quasi_production}"
)
private
String registryAddress_quasiProduction;
@Value
(
"${dubbo.registry.address.test}"
)
private
String registryAddress_test;
@Bean
public
static
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return
new
PropertySourcesPlaceholderConfigurer();
}
/**
* 設置dubbo掃描包
*
* @param packageName
* @return
*/
@Bean
public
static
AnnotationBean annotationBean(
@Value
(
"${dubbo.annotation.package}"
) String packageName) {
AnnotationBean annotationBean =
new
AnnotationBean();
annotationBean.setPackage(packageName);
return
annotationBean;
}
/**
* 注入dubbo上下文
*
* @return
*/
@Bean
public
ApplicationConfig applicationConfig() {
// 當前應用配置
ApplicationConfig applicationConfig =
new
ApplicationConfig();
applicationConfig.setName(
this
.applicationName);
return
applicationConfig;
}
/**
* 注入dubbo註冊中心配置,基於zookeeper
*
* @return
*/
@Bean
public
RegistryConfig registryConfig() {
// 鏈接註冊中心配置
RegistryConfig registry =
new
RegistryConfig();
registry.setProtocol(protocol);
String registryAddress =
null
;
switch
(AppConstant.CURR_SYSTEM_ENVIRONMENT) {
case
PRODUCTION_ENVIRONMENT:
registryAddress = registryAddress_production;
break
;
case
QUASI_PRODUCTION_ENVIRONMENT:
registryAddress = registryAddress_quasiProduction;
break
;
case
TEST_ENVIRONMENT:
registryAddress = registryAddress_test;
break
;
}
registry.setAddress(registryAddress);
return
registry;
}
}
|
三、在springmvc初始化時加載dubbo配置
修改WebInitializer.java
public
class
WebInitializer
extends
AbstractAnnotationConfigDispatcherServletInitializer {
****
@Override
protected
Class<?>[] getRootConfigClasses() {
// 加載配置文件類
return
new
Class[] { DubboConsumerConfig.
class
};
}
****
}
|
四、修改消費者類中須要遠程調用接口類上面的@Autowired註解修改成@com.alibaba.dubbo.config.annotation.Reference(version = "1.0.0")