您的位置:首页 > 其它

dubbo入门实例

2018-02-24 10:43 323 查看
前面一篇博客将dubbo-admin服务启动了,这个dubbo是基于上一篇的基础上
首先需要建议一个服务提供者,废话不多说,开始编码:
新建maven项目,pom文件如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end -->

<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->

<!-- 注册中心zookeeper begin -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end -->

<!-- log begin -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->

<!-- other begin -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- other end -->
</dependencies> 配置applicationProvider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:application name="dubbo-demo" />

<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:consumer timeout="5000" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.test.service.impl.DemoServiceImpl" />

<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.test.service.DemoService"
ref="demoService" executes="10" />
</beans>定义服务接口/*
* Copyright (c) 2006-2016 Trasen Ltd.
* All Rights Reserved.
*
*/

package com.test.service;

public interface DemoService {

public String sayHello(String name);
}接口实现类/*
* Copyright (c) 2006-2016 Trasen Ltd.
* All Rights Reserved.
*
*/

package com.test.service.impl;

import com.test.service.DemoService;

public class DemoServiceImpl implements DemoService {

@Override
public String sayHello(String name) {
System.out.println("init : " + name);
return "hello " + name;
}

}启动服务/*
* Copyright (c) 2006-2016 Trasen Ltd.
* All Rights Reserved.
*
*/

package com.test.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServerMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationProvider.xml" });
context.start();
System.out.println("输入任意按键退出 ~ ");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
context.close();
}
}下面开始客户端编码,我这里将服务端作为依赖,pom文件 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end -->

<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->

<!-- 注册中心zookeeper begin -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end -->

<!-- log begin -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->

<!-- other begin -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
applicationConsumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="consumer-of-dubbo-demo" />

<dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!-- 向注册中心订阅服务 -->
<dubbo:reference id="demoService" interface="com.test.service.DemoService" />
</beans> 调用服务类/*
* Copyright (c) 2006-2016 Trasen Ltd.
* All Rights Reserved.
*
*/

package com.client.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.service.DemoService;

public class ClientMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DemoService service = (DemoService) context.getBean("demoService");
System.out.println(service.sayHello("world"));
context.close();
}
}下面提供代码下载地址 http://download.csdn.net/download/marswap/10256575 http://download.csdn.net/download/marswap/10256580
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dubbo