Hello Dubbo
工程结构
dubbo-demo
│
├─ dubbo-demo-api
│ └─ src/main/java
│ └─ qunar.tc.dubbo.demo.api
│ └─ HelloService
│ └─ pom.xml
│
├─ dubbo-demo-provider
│ └─ src/main/java
│ └─ qunar.tc.dubbo.demo.provider
│ └─ HelloServiceImpl
│ └─ src/main/resource
│ └─ spring-dubbo-provider.xml
│ └─ pom.xml
│
├─ dubbo-demo-consumer
│ └─ src/main/java
│ └─ qunar.tc.dubbo.demo.consumer
│ └─ HelloAction
│ └─ src/main/resource
│ └─ spring-dubbo-consumer.xml
│ └─ pom.xml
│
├─ pom.xml
maven依赖
dubbo-demo/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>qunar.common</groupId> <artifactId>qunar-supom-generic</artifactId> <version>最新版本</version> </parent> <groupId>qunar.tc</groupId> <artifactId>dubbo-demo</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>dubbo-demo</name> <url>http://wiki.corp.qunar.com/display/devwiki/dubbo</url> <modules> <module>dubbo-demo-api</module> <module>dubbo-demo-provider</module> <module>dubbo-demo-consumer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <qunar.common.version>最新版本</qunar.common.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
dubbo-demo-provider/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>dubbo-demo</artifactId> <groupId>qunar.tc</groupId> <version>1.0.0</version> <relativePath>..</relativePath> </parent> <artifactId>dubbo-demo-provider</artifactId> <packaging>war</packaging> <name>dubbo demo provider</name> <url>http://wiki.corp.qunar.com/display/devwiki/dubbo</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>dubbo-demo-api</artifactId> </dependency> <dependency> <groupId>qunar.common</groupId> <artifactId>common-rpc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!-- log --> <!-- slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <!--使用slf4j适配器将log4j接管 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> <!-- 使用slf4j适配器将jcl接管 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> <!-- 使用slf4j适配器将jul接管 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> </dependency> <!-- logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <!-- test --> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
dubbo-demo-consumer/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>dubbo-demo</artifactId> <groupId>qunar.tc</groupId> <version>1.0.0</version> <relativePath>..</relativePath> </parent> <artifactId>duubo-demo-consumer</artifactId> <packaging>war</packaging> <name>dubbo demo consumer</name> <url>http://wiki.corp.qunar.com/display/devwiki/dubbo</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>dubbo-demo-api</artifactId> </dependency> <dependency> <groupId>qunar.common</groupId> <artifactId>common-rpc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!-- log --> <!-- slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <!--使用slf4j适配器将log4j接管 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> <!-- 使用slf4j适配器将JCL接管 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> <!-- logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <!-- test --> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
服务接口
定义服务接口:
public interface HelloService {
public String sayHello(String name);
}
服务提供者
在服务提供方实现接口
HelloService
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name){
return "hello:" + name;
}
}
用Spring配置声明暴露服务
spring-dubbo-provider.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:beans= "http://www.springframework.org/schema/beans" xmlns:context ="http://www.springframework.org/schema/context" xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire= "byName"> <context:property-placeholder location="classpath:dubbo-provider.properties" ignore-unresolvable="true" /> <dubbo:application name ="dubbo-demo-provider" owner="kelly.li" organization="tcdev"/> <dubbo:registry protocol ="zookeeper" address="各环境zk地址" group="/demo/group/default" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:provider> <dubbo:parameter key="qloglevel" value="10" /> </dubbo:provider> <dubbo:service interface="qunar.tc.dubbo.demo.api.HelloService" ref="helloService" version="1.0" timeout="3000"/> <bean id="helloService" class="qunar.tc.dubbo.demo.provider.HelloServceImpl"></bean> </beans>
通过 API 暴露服务
//设置应用信息,等价于<dubbo:application name="dubbo-demo-provider" owner="kelly.li" organization="tcdev" />
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-provider");
applicationConfig.setOwner("kelly.li");
applicationConfig.setOrganization("tcdev");
//设置注册中心,等价于<dubbo:registry protocol="zookeeper" address="各环境zk地址" group="/demo/group/default" />
RegistryConfig registryConfig=new RegistryConfig("各环境zk地址");
registryConfig.setProtocol("zookeeper");
registryConfig.setGroup("/demo/group/default");
//设置服务提供者协议,等价于<dubbo:protocol name="dubbo" port="20880" />
ProtocolConfig protocolConfig=new ProtocolConfig("dubbo", 20880);
//配置提供方缺省信息,等价于<dubbo:provider><dubbo:parameter key="qloglevel" value="9" /></dubbo:provider>
ProviderConfig providerConfig = new ProviderConfig();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("qloglevel", "9");
providerConfig.setParameters(parameters);
//服务实现
HelloService helloService =new HelloServiceImpl();
//设置服务提供者暴露服务,等价于<dubbo:service interface="qunar.tc.dubbo.demo.api.HelloService" ref="helloService" version="1.0" timeout="3000" />
ServiceConfig<HelloService> serviceConfig=new ServiceConfig<HelloService>();
serviceConfig.setApplication(applicationConfig);
serviceConfig.setRegistry(registryConfig);
serviceConfig.setProtocol(protocolConfig);
serviceConfig.setProvider(providerConfig);
serviceConfig.setInterface(HelloService.class);
serviceConfig.setRef(helloService);
serviceConfig.setVersion("1.0");
serviceConfig.setTimeout(3000);
serviceConfig.export();
服务消费者
通过Spring配置引用远程服务
spring-dubbo-consumer.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:beans= "http://www.springframework.org/schema/beans" xmlns:context ="http://www.springframework.org/schema/context" xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire= "byName"> <context:property-placeholder location="classpath:dubbo.properties" ignore-unresolvable="true" /> <dubbo:application name="dubbo-demo-consumer" owner="kelly.li" organization="tcdev"/> <dubbo:registry protocol="zookeeper" address="各环境zk地址" group="/demo/group/default" /> <dubbo:consumer> <dubbo:parameter key="qloglevel" value="10" /> </dubbo:consumer> <dubbo:reference id="helloService" interface="qunar.tc.dubbo.demo.api.HelloService" version="1.0" check="false" /> <bean id="helloAction" class="qunar.tc.demo.consumer.HelloAction" ></ bean> </beans>
通过API 获得远程服务
// 设置应用信息,等价于<dubbo:application name="dubbo-demo-provider" owner="kelly.li" organization="tcdev"/>
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-consumer");
applicationConfig.setOwner("kelly.li");
applicationConfig.setOrganization("tcdev");
// 设置注册中心,等价于<dubbo:registry protocol="zookeeper" address="各环境zk地址" group="/demo/group/default" />
RegistryConfig registryConfig = new RegistryConfig("各环境zk地址");
registryConfig.setProtocol("zookeeper");
registryConfig.setGroup("/demo/group/default");
// 设置消费者缺省信息,等价于<dubbo:consumer><dubbo:parameter key="qaccesslog" value="9" /></dubbo:consumer>
ConsumerConfig consumerConfig = new ConsumerConfig();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("qaccesslog", "9");
consumerConfig.setParameters(parameters);
// 设置服务消费者引用服务配置,等价于<dubbo:reference id="helloService" interface="qunar.tc.dubbo.demo.api.HelloService" version="1.0" check="false" />
ReferenceConfig<HelloService> referenceConfig = new ReferenceConfig<HelloService>();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setConsumer(consumerConfig);
referenceConfig.setInterface(HelloService.class);
referenceConfig.setVersion("1.0");
referenceConfig.setCheck(false);
HelloService helloService = referenceConfig.get();
调用远程服务
HelloAction
public class HelloAction {
private HelloService helloService;
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public String sayHello(String name) {
return helloService.sayHello(name);
}
}
测试远程调用服务
TestHelloAction
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-dubbo-consumer.xml" })
public class TestHelloAction {
@Autowired
private HelloAction helloAction;
@Test
public void testSayHello() throws Exception {
String result=helloAction.sayHello("kelly.li");
System.out.println(result);
}
}
Comments (3)
2015-04-16
王腾wteng says:
大家注意,spring-dubbo-consumer.xml文件中声明的"helloAction"的bean必须加上构造函数,不然其中的"helloServic...大家注意,spring-dubbo-consumer.xml文件中声明的"helloAction"的bean必须加上构造函数,不然其中的"helloService"回报空指针的异常
04-30
张伟zww says:
这个结构图是有错误吧! 新人刚开始学习dubbo这个结构图是有错误吧! 新人刚开始学习dubbo