码迷,mamicode.com
首页 > 其他好文 > 详细

dubbo简单示例

时间:2018-09-18 23:26:09      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:project   str   gis   comm   coding   注意   生产   out   ack   

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简单示例

标签:project   str   gis   comm   coding   注意   生产   out   ack   

原文地址:https://www.cnblogs.com/z-qinfeng/p/9672046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!