dubbo简单示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo简单示例相关的知识,希望对你有一定的参考价值。

1. 代码结构

 

2.创建test-dubbo-api

  

  创建服务接口类qinfeng.zheng.service.DemoApiService

package qinfeng.zheng.service;

public interface DemoApiService {
String getUser(Long userId);
}

   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">
    <parent>
      <artifactId>dubbo</artifactId>
      <groupId>qinfeng.zheng</groupId>
      <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>test-dubbo-api</artifactId>
  </project>

 3. 创建test-dubbo-provider

  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">
<parent>
<artifactId>dubbo</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>test-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
  

   创建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://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="test-provider" />
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
<dubbo:registry address="zookeeper://192.168.96.100:2181" />
<!-- dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--具体实现该接口的 bean -->
<bean id="demoService" class="qinfeng.zheng.service.impl.DemoApiServiceImpl" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口 -->
<dubbo:service interface="qinfeng.zheng.service.DemoApiService" ref="demoService" protocol="dubbo" />
</beans>

   创建qinfeng.zheng.service.impl.DemoApiServiceImpl

package qinfeng.zheng.service.impl;

import qinfeng.zheng.service.DemoApiService;

public class DemoApiServiceImpl implements DemoApiService {
public String getUser(Long userId) {
System.out.println("生产者调用消费者服务接口userId:" + userId);
return "哈哈,关你啥事...";
}
}

   创建服务启动类qinfeng.zheng.ProviderApp

package qinfeng.zheng;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
* 创建时间: 21:57 2018/9/18
* 修改时间:
* 编码人员: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
public class ProviderApp {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服务已经启动...");
System.in.read(); //保持服务一直在运行
}
}

  4. 创建test-dubbo-consumer

  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">
<parent>
<artifactId>dubbo</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-dubbo-consumer</artifactId>

<dependencies>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>test-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

   创建配置文件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://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="test-consumer" />

<!--zk集群配置方式-->
<!--<dubbo:registry address="zookeeper://192.168.96.100:2181?backup=192.168.96.130:2181,192.168.96.131:2181" />-->
<dubbo:registry address="zookeeper://192.168.96.100:2181" />

<dubbo:reference id="demoApiService" interface="qinfeng.zheng.service.DemoApiService" />

</beans>

   创建消费方启动类ConsumerApp

import org.springframework.context.support.ClassPathXmlApplicationContext;
import qinfeng.zheng.service.DemoApiService;

/**
* 创建时间: 22:16 2018/9/18
* 修改时间:
* 编码人员: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
public class ConsumerApp {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
context.start();
DemoApiService demoApiService = (DemoApiService) context.getBean("demoApiService");
String ret = demoApiService.getUser(1l);
System.out.println(ret);
System.in.read();
}
}

 

 5. 启动zookeeper注册中心

 启动prodiver,consumer两个服务

 

 服务调用成功!!!

 

以上是关于dubbo简单示例的主要内容,如果未能解决你的问题,请参考以下文章

处理屏幕旋转上的片段重复(带有示例代码)

为啥这段代码会泄露? (简单的代码片段)

Dubbo实践入门示例

在android中使用带有片段的roboguice的任何简单示例?

手写dubbo-2netty实现简单群聊私聊

dubbo系列dubbo启动过程源码解析