首先,开始编写服务提供者的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. python------面向对象进阶 异常处理

    一. 异常处理 try: pass except KeyError as e : #注3.x用as ,except KeyError, e ,2.x 用逗号. print("No this ...

  2. robot framework下载文件操作

    不同的浏览器点击[下载]按钮之后,需要点击保存,还是确定,或者直接默认直接下载都是不一样的 1.chrome:点击[下载]之后,会自动执行下载操作,直到下载结束 A)点击下载,等待下载结束(sleep ...

  3. container and Injection

    1.容器的历史 容器概念始于 1979 年提出的 UNIX chroot,它是一个 UNIX 操作系统的系统调用,将一个进程及其子进程的根目录改变到文件系统中的一个新位置,让这些进程只能访问到这个新的 ...

  4. android ListView 可缩放,支持左右上下手势

    public class ZoomListView extends ListView implements SwipeListener { public static enum Action { Le ...

  5. 使用mysqlproxy实现mysql读写分离

    先说一下什么是读写分离吧. 以三台虚拟机为例,搭建一主一从一代理,全部配置好之后,当从proxy插入数据时,该数据会同时插入主数据库,因为主从关系,从数据库也会有数据.当把从数据库执行slave st ...

  6. 推荐一些关于学习Html Css和Js的书吗?

    前端易学易懂,随着移动互联网的日益兴起,it行业对于前端的需求也在不断的提高,那么从前端小白修炼成为前端大神的这个过程之中,一些必备的枕边书也是必不可少的. 第一本,入门<Head first ...

  7. Synchronized 有几种用法?

    我们都知道 Synchronized 是线程安全同步用的,大部分程序可能只会用到同步方法上面.其实 Synchronized 可以用到更多的场合. 1.同步普通方法(锁实例对象) 这个也是我们用得最多 ...

  8. Ansible 快速部署 Zabbix 4

    阅读本文章需要具有Ansible.Zabbix基础.本次教程基于如下环境: CentOS 7.x Zabbix 4.0 Ansible 2.5 服务器初始化 关闭防火墙.selinux,添加epel常 ...

  9. 各CF-based tracker中output_sigma_factor取值

    现有的各CF-Based tracker中理想高斯响应中output_sigma_factor的取值情况 默认output_sigma = target_sz*output_sigma_factor; ...

  10. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...