月眸


Dubbo入门实例

毛毛小妖 2019-01-23 788浏览 2条评论
首页/ 正文
分享到: / / / /

一、创建Maven多模块项目

项目结构如下

模块介绍:

dubbo-api            ----API接口

dubbo-consumer ----消费者

dubbo-provider    ----生产者

dubbo-api  Service 提供的接口,provider 实现了 api 接口,并对外开放http端口,consumer依赖 api 工程,但是不会直接依赖provider工程。 这种可以让消费者仅仅依赖api工程,而不需要对provider工程的依赖,也是松耦合的经典。

1、创建root项目

首先创建一个名为Dubbo_demos的项目,把一些通用的依赖全部放置到这里,pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.fendo</groupId>
	<artifactId>Dubbo_demos</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>dubbo</name>
	<description>dubbo示例</description>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<!-- spring版本号 -->
		<spring.version>4.2.5.RELEASE</spring.version>

		<!-- log4j日志包版本号 -->
		<slf4j.version>1.7.18</slf4j.version>
		<log4j.version>1.2.17</log4j.version>

	</properties>

	<dependencyManagement>
		<dependencies>
			<!-- 添加junit4依赖 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.11</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<!-- 添加日志相关jar包 -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>${log4j.version}</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-api</artifactId>
				<version>${slf4j.version}</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>${slf4j.version}</version>
			</dependency>

			<dependency>
				<groupId>commons-logging</groupId>
				<artifactId>commons-logging</artifactId>
				<version>1.2</version>
			</dependency>

			<!-- dubbo -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<version>2.5.3</version>
				<exclusions>
					<exclusion>
						<groupId>org.springframework</groupId>
						<artifactId>spring</artifactId>
					</exclusion>
				</exclusions>
			</dependency>

			<!-- Zookeeper zkclient -->
			<dependency>
				<groupId>com.github.sgroschupf</groupId>
				<artifactId>zkclient</artifactId>
				<version>0.1</version>
			</dependency>

		</dependencies>
	</dependencyManagement>
	<modules>
		<module>dubbo-api</module>
		<module>dubbo-provider</module>
		<module>dubbo-consumer</module>
	</modules>
</project>

2、创建api模块

在Dubbo_demos项目中新建一个名为dubbo-api的模块

他会自动在root目录下把dubbo-api这么module加入进来。而dubbo-api会自动继承root

3、创建provider模块(生产者)

在Dubbo_demos项目中创建dubbo-provider模块,并修改POM.XML为如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<artifactId>Dubbo_demos</artifactId>
		<groupId>com.fendo</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.fendo</groupId>
	<artifactId>dubbo-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>dubbo-provider</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.fendo</groupId>
			<artifactId>dubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

		<!-- 添加日志相关jar包 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>

		<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- Zookeeper zkclient -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
	</dependencies>
</project>

4、创建consumer模块(消费者)

在Dubbo_demos项目中创建dubbo-consumer模块,并修改POM.XML为如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<artifactId>Dubbo_demos</artifactId>
		<groupId>com.fendo</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.fendo</groupId>
	<artifactId>dubbo-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>dubbo-consumer</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>

		<dependency>
			<groupId>com.fendo</groupId>
			<artifactId>dubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

		<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- Zookeeper zkclient -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
	</dependencies>
</project>

二、定义接口API、Vo对象、公共类及方法

这些是要提供方和调用方都需要的。

在dubbo-api模块中创建一个接口对外开放

package org.dubbo.api;
import java.util.List;

public interface DemoService {
    public List getPermissions(Long id);
}

三、定义Dubbo服务service

实现dubbo-api中的api接口,书写业务逻辑及DB操作。通过spring暴露服务。

创建DemoService实现类DemoServiceImpl

package org.dubbo.provider.impl;

import java.util.ArrayList;
import java.util.List;
import org.dubbo.api.DemoService;

public class DemoServiceImpl implements DemoService {
    public List getPermissions(Long id) {
        List demo = new ArrayList();
        demo.add(String.format("Permission_%d", id - 1));
        demo.add(String.format("Permission_%d", id));
        demo.add(String.format("Permission_%d", id + 1));
        return demo;
    }
}

以及消费者启动类Provider

public class Provider {
    public static void main(String[] args) throws IOException{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("服务已经启动...");
        System.in.read();
    }
}

然后创建resources目录,在下新建两个配置文件

provider.xml是dubbo配置文件

<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
	<dubbo:application name="demotest-provider" owner="programmer"
		organization="dubbox" />
	<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
	<dubbo:registry address="zookeeper://localhost:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口 -->
	<dubbo:service interface="org.dubbo.api.DemoService" ref="demoService"
		protocol="dubbo" />
	<!--具体实现该接口的 bean -->
	<bean id="demoService" class="org.dubbo.provider.impl.DemoServiceImpl" />
</beans>

四、定义调用端

也叫消费端,实现对应的控制层。通过spring获取服务,和上面差不多。

创建消费者运行类:

public class Consumer 
{
    public static void main(String[] args) {
        //测试常规服务
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        DemoService demoService = (DemoService) context.getBean(DemoService.class);
        System.out.println("consumer");
        System.out.println(demoService.getPermissions(1L));
    }
}


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://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="demotest-consumer" owner="programmer" organization="dubbox" />
	<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送 -->
	<dubbo:registry address="zookeeper://localhost:2181" />
	<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口 -->
	<dubbo:reference id="demoService" interface="org.dubbo.api.DemoService" />

</beans>

五、使用zookeeper注册中心

运行zookeeper/bin目录下的zkServer.cmd,启动zookeeper,启动成功如下所示:

六、启动Dubbo的service端

即provider。

启动zookeeper之后,然后运行dubbo-provider中的Provider类,如果发现启动报错,应该是dubbo.xsd加载失败,我们可以手动引入Dubbo.xsd。首先百度下载Dubbo.xsd,然后按照如下图配置:

启动成功会出现如下信息:

七、启动portal端

即所谓的consumer。

然后启动dubbo-consumer中的Consumer类

会打印如下信息,表示消费成功!!!

最后修改:2019-01-23 20:00:18 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

评论列表

xq 2019-01-24 18:13:12
[挖鼻]图呢?????
回复