Dubbo helloword
首先,开始编写服务提供者的api接口,
SampleService 接口
 package bhz.dubbo.sample.provider;
 import java.util.List;
 public interface SampleService {
     String sayHello(String name);
     public List getUsers();
 }
实现类
package bhz.dubbo.sample.provider.impl; import java.util.ArrayList;
import java.util.List; import bhz.dubbo.sample.provider.SampleService; public class SampleServiceImpl implements SampleService { public String sayHello(String name) {
return "Hello " + name;
} public List getUsers() {
List list = new ArrayList();
User u1 = new User();
u1.setName("jack");
u1.setAge(20);
u1.setSex("m"); User u2 = new User();
u2.setName("tom");
u2.setAge(21);
u2.setSex("m"); User u3 = new User();
u3.setName("rose");
u3.setAge(19);
u3.setSex("w"); list.add(u1);
list.add(u2);
list.add(u3);
return list;
}
}
User 对象
 package bhz.dubbo.sample.provider.impl;
 import java.io.Serializable;
 public class User implements Serializable {
     private static final long serialVersionUID = 1L;
     private int age;
     private String name;
     private String sex;
     public User() {
         super();
     }
     public User(int age, String name, String sex) {
         super();
         this.age = age;
         this.name = name;
         this.sex = sex;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getSex() {
         return sex;
     }
     public void setSex(String sex) {
         this.sex = sex;
     }
 }
启动类
 package bhz.dubbo.sample.test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class Provider {
     public static void main(String[] args) throws Exception {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                 new String[] { "sample-provider.xml" });
         context.start();
         System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
     }
 }
看一下配置文件
<?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
"> <!-- 具体的实现bean -->
<bean id="directService" class="bhz.dubbo.direct.provider.impl.DirectServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="direct-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 只订阅的方式:register="false"-->
<dubbo:registry address="zookeeper://192.168.1.111:2181?backup=192.168.1.112:2181,192.168.1.113:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 直连服务提供者:是在消费端进行配置的,而不是在服务提供端,所以这里不需要任何配置 -->
<dubbo:service retries="0" interface="bhz.dubbo.direct.provider.DirectService" ref="directService" /> </beans>
下面看一下消费者:
因为在两个项目中,所以接口copy一下
 package bhz.dubbo.sample.provider;
 import java.util.List;
 public interface SampleService {
     String sayHello(String name);
     public List getUsers();
 }
消费类
 package bhz.dubbo.sample.test;
 import java.util.List;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import bhz.dubbo.sample.provider.SampleService;
 public class Consumer {
     public static void main(String[] args) throws Exception {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                 new String[] { "sample-consumer.xml" });
         context.start();
         SampleService sampleService = (SampleService) context.getBean("sampleService");
         String hello = sampleService.sayHello("tom");
         System.out.println(hello);
 //        List list = sampleService.getUsers();
 //        if (list != null && list.size() > 0) {
 //            for (int i = 0; i < list.size(); i++) {
 //                System.out.println(list.get(i));
 //            }
 //        }
         System.in.read();
     }
 }
看一下配置文件
<?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="sample-consumer" /> <dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="false"
interface="bhz.dubbo.sample.provider.SampleService" /> </beans>
将项目运行,可以发现,consumer项目,可以直接调用provider中的实现类,
dubbo 提供自带的监控系统,可以对服务进行查看,可以配置路由,权重等跟多功能,
dubbo 的管控台,需要自己去部署,可以参考官方文档
http://dubbo.apache.org/books/dubbo-admin-book/install/admin-console.html
将对应的源码下载下来,进行打包,部署。

访问ip加端口,如上图的管理后台。官网也提供了相应的运维手册,可以操控操作。
A服务依赖于B服务,A 消费者调用A服务。看一下配置
 package bhz.dubbo.dependency.provider;
 public interface DependencyService {
     public String dependency() throws Exception;
 }
package bhz.dubbo.dependency.provider.impl; import org.springframework.beans.factory.annotation.Autowired; import bhz.dubbo.dependency.provider.DependencyService;
import bhz.dubbo.sample.provider.SampleService; //@Service("dependencyServiceImpl")
//@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
public class DependencyServiceImpl implements DependencyService { // 注入SampleService
@Autowired
private SampleService sampleService; public String dependency() throws Exception {
// 这里 我们可能需要调用SampleService,也可能不需要...
System.out.println(sampleService.sayHello("jack"));
return "dependency exec";
} }
 package bhz.dubbo.dependency.test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class Provider {
     public static void main(String[] args) {
         try {
             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                     new String[] { "dependency-provider.xml" });
             context.start();
             System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }
<?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:annotation package="bhz" /> <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dependency-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20890" /> <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="true"
interface="bhz.dubbo.sample.provider.SampleService" /> <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> </beans>
这边有一个字段,“check="true"”,代表着如果sampleService不先启动,则会报错。
这边引用了sampleService,上面已经写过了,
下面看一下consumer,soncumer 还是跟helloword的一样,只关心直接调用的A服务,A服务依赖的都不关心,
 package bhz.dubbo.dependency.provider;
 public interface DependencyService {
     public String dependency() throws Exception;
 }
package bhz.dubbo.dependency.provider.impl; import org.springframework.beans.factory.annotation.Autowired; import bhz.dubbo.dependency.provider.DependencyService;
import bhz.dubbo.sample.provider.SampleService; //@Service("dependencyServiceImpl")
//@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
public class DependencyServiceImpl implements DependencyService { // 注入SampleService
@Autowired
private SampleService sampleService; public String dependency() throws Exception {
// 这里 我们可能需要调用SampleService,也可能不需要...
System.out.println(sampleService.sayHello("jack"));
return "dependency exec";
} }
 package bhz.dubbo.dependency.test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class Provider {
     public static void main(String[] args) {
         try {
             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                     new String[] { "dependency-provider.xml" });
             context.start();
             System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }
<?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:annotation package="bhz" /> <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dependency-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20890" /> <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="true"
interface="bhz.dubbo.sample.provider.SampleService" /> <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> </beans>
Dubbo helloword的更多相关文章
- Dubbo HelloWord 与 Spring Boot 整合
		
实现消费者项目代码调用提供者项目代码,使用 zookeeper 做为注册中心 interface 项目 pom.xml <?xml version="1.0" encodin ...
 - 异构SOA系统架构之Asp.net实现(兼容dubbo)
		
我们公司技术部门情况比较复杂,分到多个集团,每个集团又可能分为几个部门,每个部门又可能分为多个小组,组织架构比较复杂,开发人员比较多. 使用的编程语言也有点复杂,主流语言有.net(C#).Java. ...
 - 服务化实战之 dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型
		
转自: http://blog.csdn.net/liubenlong007/article/details/54692241 概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺 ...
 - dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型
		
概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...
 - Dubbo 节点telnet测试
		
Dubbo 节点telnet测试 本地安装telnet客户端 Telnet 服务地址 端口 如telnet 127.0.0.1 1234 出现此对话框表示连接成功 输入status –l 会显 ...
 - Dubbo Configuration
		
可配置参数 http://dubbo.apache.org/zh-cn/docs/user/references/xml/introduction.html 与 spring 整合的几种方式 Spri ...
 - java使用netty模拟实现一个类dubbo的分布式服务调用框架
		
本文较长,如果想直接看代码可以查看项目源码地址: https://github.com/hetutu5238/rpc-demo.git 要想实现分布式服务调用框架,我们需要了解分布式服务一般需要的功能 ...
 - 用dubbo时遇到的一个序列化的坑
		
首先,这是标题党,问题并不是出现在序列化上,这是报错的一部分: Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to s ...
 - dubbo服务提供与消费
		
一.前言 项目中用到了Dubbo,临时抱大腿,学习了dubbo的简单实用方法.现在就来总结一下dubbo如何提供服务,如何消费服务,并做了一个简单的demo作为参考. 二.Dubbo是什么 Dubbo ...
 
随机推荐
- CodeForces - 13E
			
Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...
 - [CSL 的字符串][栈,模拟]
			
链接:https://ac.nowcoder.com/acm/contest/551/D来源:牛客网题目描述 CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助. 给定一个字 ...
 - lucas  模板  洛古模板题
			
#include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int quick(in ...
 - web driver下载地址(selenium-3.141_浏览器版本对应)
			
web driver: (Windows环境下) Firefox(上一目录可以找其他浏览器的driver) : 1)http://npm.taobao.org/mirrors/geckodriver/ ...
 - 使用vsftp服务传输文件
 - 浅析rune数据类型
			
学习golang基础的时候,发现有个叫rune的的数据类型,当时不理解这个类型的意义. 查询,官方的解释如下: // rune is an alias for int32 and is equival ...
 - C# 调用打印机 打印 Excel
			
打印 Excel 模板 大体思路,通过NPOI操作Excel文件,通过Spire将Excel转成图片,将图片传给系统打印. Spire是收费工具,在微软库中下载Free版本. #region 打印所用 ...
 - 基于MATLAB System Generator 搭建Display Enhancement模型
			
基于MATLAB System Generator 搭建Display Enhancement模型
 - 使用Intellij搭建Servlet开发环境
			
https://blog.csdn.net/yhao2014/article/details/45740111 使用Tomcat 9时,必须使用jre 1.8,否则会出现Unable to ping ...
 - 【rocketMQ】之centos6.9安装RocketMQ4.2
			
基于最新的RocketMQ4.2版本,搭建2Master模式. 硬件环境:centos6.9_x64(两台虚拟机) IP1:192.168.150.128 IP2:192.168.150.129 1. ...