首先,开始编写服务提供者的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的更多相关文章

  1. Dubbo HelloWord 与 Spring Boot 整合

    实现消费者项目代码调用提供者项目代码,使用 zookeeper 做为注册中心 interface 项目 pom.xml <?xml version="1.0" encodin ...

  2. 异构SOA系统架构之Asp.net实现(兼容dubbo)

    我们公司技术部门情况比较复杂,分到多个集团,每个集团又可能分为几个部门,每个部门又可能分为多个小组,组织架构比较复杂,开发人员比较多. 使用的编程语言也有点复杂,主流语言有.net(C#).Java. ...

  3. 服务化实战之 dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    转自: http://blog.csdn.net/liubenlong007/article/details/54692241 概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺 ...

  4. dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...

  5. Dubbo 节点telnet测试

        Dubbo 节点telnet测试 本地安装telnet客户端 Telnet 服务地址 端口 如telnet 127.0.0.1 1234 出现此对话框表示连接成功 输入status –l 会显 ...

  6. Dubbo Configuration

    可配置参数 http://dubbo.apache.org/zh-cn/docs/user/references/xml/introduction.html 与 spring 整合的几种方式 Spri ...

  7. java使用netty模拟实现一个类dubbo的分布式服务调用框架

    本文较长,如果想直接看代码可以查看项目源码地址: https://github.com/hetutu5238/rpc-demo.git 要想实现分布式服务调用框架,我们需要了解分布式服务一般需要的功能 ...

  8. 用dubbo时遇到的一个序列化的坑

    首先,这是标题党,问题并不是出现在序列化上,这是报错的一部分: Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to s ...

  9. dubbo服务提供与消费

    一.前言 项目中用到了Dubbo,临时抱大腿,学习了dubbo的简单实用方法.现在就来总结一下dubbo如何提供服务,如何消费服务,并做了一个简单的demo作为参考. 二.Dubbo是什么 Dubbo ...

随机推荐

  1. linux的基本操作1

    文件系统 ext4 d: 进入d盘dir /w 以友好的方式列出目录cd     目录名 进入目录cd\      退出目录cd ..    退出到上级目录ipconfig /all 显示本机网络信息 ...

  2. Java StringBuffer和StringBuilder类

    Java StringBuffer和StringBuilder类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. 和String类不同的是,StringBu ...

  3. java_字段声明

    多字段继承,为避免混淆,simple name与qualified name的使用 package java20180129_1; interface Frob { float v=2.0f; } c ...

  4. 深入Session2

    一.分布式环境Session的处理方法 分布式环境下要保持会话跟踪最简单的方式是只依靠客户端Cookie保存,不过大多数情况下还需要用到Session,一般的处理方式如下: 1.Session复制 每 ...

  5. java volatile

    volatile可以保证变量的可见性 当一个变量定义为volatile后,此变量对所有的线程具有可见性.这里的可见性是指当一个线程修改了这个变量的值,新值对于其他线程来说是可以立即得知的. 每次使用v ...

  6. Caused by: io.protostuff.ProtobufException: Protocol message contained an invalid tag (zero).

    [ERROR] com.xxxx.redis.RedisClientTemplate.getOject(RedisClientTemplate.java:60):http-bio-8080-exec- ...

  7. 当通过Nuget包管理器获取还原组时,出现 提示 “xxxxx”已拥有为“xxxxx”定义的依赖项

    当通过Nuget包管理器获取还原组件时,出现  提示 “xxxxx”已拥有为“xxxxx”定义的依赖项 时 解决方法: 工具---扩展和更新,把Nuget包管理器卸载后,重启VS,再安装,现打开VS项 ...

  8. 配置gitlab自动备份

    在gitlab机器的root用户执行 首先,假设有2台机器. gitlab 1.1.1.1 backup 2.2.2.2 做秘钥信任 gitlab root 生成 ssh-key copy密钥到bac ...

  9. python3中的urllib.parse的常用方法

    将URL按一定的格式进行拆分 使用 urllib.parse.urlparse将url分为6个部分,返回一个包含6个字符串项目的元组:协议.位置.路径.参数.查询.片段 参照官方地址:https:// ...

  10. C#设计模式(1)——单例模式(Singleton)

    单例模式即所谓的一个类只能有一个实例, 也就是类只能在内部实例一次,然后提供这一实例,外部无法对此类实例化. 单例模式的特点: 1.只能有一个实例: 2.只能自己创建自己的唯一实例: 3.必须给所有其 ...