Dubbo学习之简单的demo(xml版)

当前环境:zookeeper3.4.13、eclipse、jdk1.8

1.首先创建一个api接口

在这里插入图片描述

1.1 创建一个DemoService接口作为当前统一使用的接口

/**
 * @description 定义服务接口
 * @author hy
 * @date 2019-10-06
 */
public interface DemoService {
	String say(String context);
}

1.2 将当前的dubbo-api打成jar包

在这里插入图片描述
通过maven build,然后使用maven install,让当前的maven仓库出现文件

在这里插入图片描述

2.创建一个父级项目

在这里插入图片描述

2.1 添加依赖

<modelVersion>4.0.0</modelVersion>
	<groupId>DubboParentDemo</groupId>
	<artifactId>DubboParentDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
		<!-- 导入dubbo依赖 -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.3</version>
		</dependency>
		<!-- 导入junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>2.0.0-alpha1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>2.0.0-alpha1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.9.0</version>
		</dependency>

	</dependencies>

	<modules>
		<module>dubbo-start-demo-provider</module>
		<module>dubbo-start-demo-consumer</module>
	</modules>

注意如果出现连接zookeeper问题表示当前的curator版本有问题,修改版本即可(当前项目已测试成功)

3.在DubboParentDemo创建dubbo-start-demo-provider项目

3.1 添加依赖

这里需要引入dubbo-api这个jar包

<parent>
    <groupId>DubboParentDemo</groupId>
    <artifactId>DubboParentDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dubbo-start-demo</artifactId>
  <name>dubbo-start-demo</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  	<dependency>
  		<artifactId>dubbo-api</artifactId>
  		<groupId>dubbo-api</groupId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>

3.2 编写DemoServiceImpl类

import com.hy.dubbo.api.DemoService;

/**
 * @description 在服务方创建服务的实现类
 * @author hy
 * @date 2019-10-06
 */
public class DemoServiceImpl implements DemoService{

	@Override
	public String say(String context) {
		return "Hello "+context+" !";
	}
	
}

3.3 编写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-4.3.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用zookeeper注册中心暴露服务地址,这里是linux中的地址 -->
    <dubbo:registry address="zookeeper://192.168.133.129:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口,即上面打成dubbo-api中的DemoService -->
    <dubbo:service interface="com.hy.dubbo.api.DemoService" ref="demoService" />
 
    <!-- 这里是本地实现DemoService的实现类 -->
    <bean id="demoService" class="com.hy.dubbo.start.demo.dao.impl.DemoServiceImpl" />
</beans>

3.3 编写Provider类

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"classpath:provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}

3.4 启动Provider

在这里插入图片描述
出现CONNECTED表示已经连接到zookeeper

4.在DubboDemoParent中添加dubbo-start-demo-consumer项目

4.1 添加依赖

同样需要导入dubbo-api这个接口的jar包

  <parent>
    <groupId>DubboParentDemo</groupId>
    <artifactId>DubboParentDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <artifactId>dubbo-start-demo-consumer</artifactId>
  <name>dubbo-start-demo-consumer</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>dubbo-api</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

4.2 编写customer.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-4.3.xsd
	http://dubbo.apache.org/schema/dubbo
	http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://192.168.133.129:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用DemoService ,这里就是使用dubbo-api中的DemeService-->
    <dubbo:reference id="demoService" interface="com.hy.dubbo.api.DemoService" />
</beans>

4.3 编写Consumer 类

public class Consumer {
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:customer.xml" });
		context.start();

		DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
		String hello = demoService.say("hello"); // 执行远程方法
		System.out.println(hello); // 显示调用结果
	}
}

4.4 启动Consumer

在这里插入图片描述

结果成功!

5.总结

1.在使用dubbo的时候需要注意服务注册中心的问题需要启用注册中心,感觉和SpringClould一样

2.如果出现了不能连接zookeeper的问题,可能是版本的问题或者是缺少包

3.当前的dubbo中的接口需要在maven中打成jar包,然后被客户端和服务端一起引用

以上纯属个人见解,如有问题请联系本人!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值