Dubbo之入门例子HelloWorld
1、项目的目录结构 2、关于Zookeeper的安装 关于Zookeeper的安装,详情见前面的博客 http://blog.csdn.net/zbw18297786698/article/details/530114493、关于项目的jar的版本,统一在mydubbo-parent中管理 1.在引用dubbo时,应该排除其对Spring的依赖
1、项目的目录结构
2、关于Zookeeper的安装
关于Zookeeper的安装,详情见前面的博客 http://blog.csdn.net/zbw18297786698/article/details/53011449
3、关于项目的jar的版本,统一在mydubbo-parent中管理
1.在引用dubbo时,应该排除其对Spring的依赖
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
2.完整的用于统一管理jar版本的pom的配置文件
<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>
<groupId>com.baowei.mydubbo</groupId>
<artifactId>mydubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.10</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<slf4j.version>1.6.4</slf4j.version>
<dubbo.version>2.5.3</dubbo.version>
<zkclient.version>0.4</zkclient.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- sla4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
4、mydubbo-facade的目录结构
5、mydubbo-provider
1.对于服务的实现
import org.springframework.stereotype.Component;
import com.baowei.entity.People;
import com.baowei.rpc.SpeakInterface;
@Component("speakInterface")
public class SpeakInterfaceImpl implements SpeakInterface {
public String speak(People people) {
return "dubbo return " + people.getAge();
}
}
2.关于Spring配置文件的放置和Dubbo服务的提供的第一种方式
2.1 Spring配置文件的放置,放在spring的文件夹下面
2.2 Dubbo服务提供的方式,自己编写的用于调试的服务启动方式
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboProvider {
public static void main(String[] args) {
try {
// 用于加载Spring的配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/spring/spring-*.xml");
context.start();
} catch (Exception exception) {
throw new RuntimeException("DubboProvider context start error: "
+ exception);
}
synchronized (DubboProvider.class) {
while (true) {
try {
DubboProvider.class.wait();
} catch (InterruptedException e) {
throw new RuntimeException("synchronized error: " + e);
}
}
}
}
}
3.关于Spring配置文件的放置和Dubbo服务的提供的第二种方式
3.1 Spring配置文件的放置,放在META-INF文件夹下的spring的文件夹下
3.2 Dubbo服务提供的方式,使用Dubbo提供的Main来启动服务
4.关于dubbo的配置文件,spring-dubbo-provider.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--服务提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="mydubbo-provider" />
<!--使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="192.168.2.118:2181" protocol="zookeeper" />
<!--用Dubbo协议在20886端口,暴露服务 -->
<dubbo:protocol id="mydubbo" name="dubbo" port="20886" />
<!--要提供的服务接口 -->
<dubbo:service interface="com.baowei.rpc.SpeakInterface"
ref="speakInterface" />
</beans>
5.关于Spring的配置文件,spring-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.baowei.rpc.impl" />
</beans>
6、mydubbo-consumer
6.1 配置文件的放置
6.2 dubbo消费者的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--服务消费方应用名,不是匹配条件,不要和服务提供方一样 -->
<dubbo:application name="mydubbo-consumer" />
<!--使用zookeeper注册中心获取服务 -->
<dubbo:registry address="192.168.2.118:2181" protocol="zookeeper" />
<!--服务的接口 -->
<dubbo:reference id="speakInterface" interface="com.baowei.rpc.SpeakInterface" />
</beans>
6.3 获取Dubbo服务并调用
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baowei.entity.People;
import com.baowei.rpc.SpeakInterface;
public class DubboConsumer {
public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "/spring/spring-*.xml" });
SpeakInterface speakInterface = context.getBean("speakInterface",
SpeakInterface.class); // 获取远程服务代理
People people = new People();
people.setAge(7);
System.out.println(speakInterface.speak(people));
// 用于观察zookeeper上节点信息的变化
Thread.sleep(115000);
}
}
7、源码下载
更多推荐
所有评论(0)