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 ...
随机推荐
- RESTful Loads
RESTful 一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. 概述 ...
- 为什么redis使用单线程还能这么快?
通常来讲,单线程处理能力要比多线程差,但是redis为什么就快了,这主要得益于以下几个原因: 1.纯内存访问,redis将所有数据放在内存中,内存的响应时长大约为100纳秒,这是redis达到每秒万级 ...
- node 下less无法编译的问题
vue+less的项目中,npm run dev不通过,提示以下错误: These dependencies were not found: * !!vue-style-loader!css-load ...
- 【代码问题】SiameseFC
[SiameseFC]: L Bertinetto, J Valmadre, JF Henriques, et al. Fully-convolutional siamese networks for ...
- java中的内部类详解
https://www.cnblogs.com/dolphin0520/p/3811445.html https://www.cnblogs.com/chenssy/p/3388487.html
- autoit3编辑器SCITE字体设置
选项→打开全局设置文件,就是SciTEGlobal.properties,修改下面的部分即可,保存之后立刻生效.如果不行,就打开用户设置文件SciTEUser.properties进行修改: font ...
- (转载)o(1), o(n), o(logn), o(nlogn) 时间复杂度
o(1), o(n), o(logn), o(nlogn) 时间复杂度的解释: https://blog.csdn.net/yhc166188/article/details/81162865 时间复 ...
- Python【每日一问】04
问:a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],求出列表a中所有奇数并构造新列表 答: 利用列表的元素下标遍历列表 a = [1, 2, 3, 4, 5, 6, 7, 8 ...
- 用kettle从mysql中使用存储过程读取数据写入到sqlserver数据库
1.mysql存储过程,可以实现动态表读取,满足较为复杂的业务逻辑 DROP PROCEDURE if exists p_get_car_trace; delimiter // CREATE PROC ...
- C语言数据结构基础学习笔记——基础线性表
线性表是指具有相同数据类型的n(n>=0)个数据元素的有限序列,它具有一个表头元素和一个表尾元素,并且每一个数据元素最多只有一个直接前驱和一个直接后继. 线性表的顺序存储也叫作顺序表,它的特性是 ...