这里先讲一下, 怎么样能快速开始. 新手可以看看,老鸟直接跳过即可.
使用maven构建项目,首先引入需要的依赖
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.10.0</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>jmxtools</artifactId>
<groupId>com.sun.jdmk</groupId>
</exclusion>
<exclusion>
<artifactId>jmxri</artifactId>
<groupId>com.sun.jmx</groupId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
</exclusions>
</dependency>
接下来 编写服务端的接口和实现类以及spring配置文件
接口HelloWorldService
package com.js.dubbo.provider;
public interface HelloWorldService {
/**
* 简单接口测试
*
* @param greet
*/
String sayHello(String greet);
}
实现类HelloWorldServiceImpl
package com.js.dubbo.provider;
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHello(String greet) {
return System.currentTimeMillis() + " Hello From Dubbo Server :" + greet;
}
}
provider配置文件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:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 当前应用信息配置 ip也可以是127.0.0.1-->
<dubbo:application name="providerDemo"/>
<dubbo:registry address="zookeeper://xx.xx.xx.xxx:2181"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.js.dubbo.provider.HelloWorldService" ref="helloWorldService"/>
<!-- 本地对象实例 -->
<bean id="helloWorldService" class="com.js.dubbo.provider.HelloWorldServiceImpl"/>
</beans>
启动类AppServerStart
package com.js.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Hello world!
*/
public class AppServerStart {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-provider.xml"});
//blocking the program
System.in.read();
}
}
此时,服务端已经编写完成,直接启动
AppServerStart
就开始提供服务了
下面开始编写客户端consumer代码
配置文件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:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 当前应用信息配置 ip也可以是127.0.0.1-->
<dubbo:application name="providerDemo"/>
<dubbo:registry address="zookeeper://xx.xx.xx.xxx:2181"/>
<!-- 像本地使用一样注册对象-->
<dubbo:reference id="helloWorldService" interface="com.js.dubbo.provider.HelloWorldService"/>
</beans>
测试类AppConsumer
package com.js.dubbo;
import com.js.dubbo.provider.HelloWorldService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Hello world!
*/
public class AppConsumer {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
HelloWorldService hw = (HelloWorldService) ac.getBean(HelloWorldService.class);
String result = hw.sayHello("dubbo");
System.err.println(result);
}
}
此时执行类
AppConsumer
即可使用helloWorldService提供的服务了.
总结:
本文比较简单 ,把服务端和消费端集成到一起了. 实际开发中, dubbo这种rpc通信基本不会再同一个进程中的(跨jvm通信). 通常的做法是 把服务端的接口文件和接口参数(dto等等) 归入一个单独的接口maven工程中,然后发布到内网,其他用到该项目接口的下游项目,会直接依赖这个发布的接口项目,然后吧接口配置到自身的配置文件中,就像本文后半部分那样 ,使用<dubbo:reference
指令就吧服务引入当前项目了 .就像使用本地接口一样使用这个接口了.
网友评论