spring + dubbo 学习
新启动的项目中可能会使用到dubbo,因为之前并没有接触过,所以先小试一下
示例运行环境准备:OS X 10.10.5 + java version "1.8.0_40"
zookeeper 和 dubbo-admin运行在虚拟机上,环境为Centos7.0 + java version "1.8.0_73"
项目使用gradlle管理

其中dubbo-demo未项目,里面新建了两个module,api为服务提供方,console为服务消费方。注意,需要在console module中添加api 的module依赖
api中代码如下:
DemoService
package com.dh.demo.service;
public interface DemoService {
public String hello(String name);
}
DemoServiceImpl
package com.dh.demo.service.impl;
import com.dh.demo.service.DemoService;
public class DemoServiceImpl implements DemoService {
public String hello(String name) {
return "hello " + name;
}
}
服务端spring配置文件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:application name="dubbo-demo-api"/> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://vm:2181"/> <bean id="demoService" class="com.dh.demo.service.impl.DemoServiceImpl"/> <!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dh.demo.service.DemoService" ref="demoService"/> </beans>
启动api的测试代码如下:
package com.dh.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/api/src/resources/dubbo-provider.xml");
context.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务提供者的配置到此结束。
下面是消费者的配置和代码
spring配置文件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="dubbo-demo-console"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://vm:2181"/> <!-- 要引用的服务 -->
<dubbo:reference interface="com.dh.demo.service.DemoService" id="demoService"></dubbo:reference>
</beans>
启动消费者测试的代码如下
Consumer:
package com.dh.test; import com.dh.demo.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/console/src/resources/dubbo-consumer.xml");
context.start();
DemoService testRegistryService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = testRegistryService.hello("world");
System.out.println(hello);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
先运行提供者测试代码,再运行消费者代码。可以看到下面输出

至此测试成功
如果运行了dubbo-admin的话可以进去后可以查看和管理服务
这是提供者:

消费者:

有些童鞋可能不知道怎么运行dubbo-demo
看dubbo的github主页就行
如果再jdk8的环境下运行可能在启动的时候会遇到如下问题
Bean property 'URIType' is not writable or has 。。。。。后面省略
解决方式参考:https://github.com/alibaba/dubbo/issues/50 中 stirp 的方式即可
注:dubbo-demo的build.gradle内容如下
group 'com.dh'
version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.5 repositories { maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile(group: 'com.alibaba', name: 'dubbo', version: '2.5.3') {
exclude(module: 'spring')
}
compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.6'
compile group: 'com.google.zxing', name: 'core', version: '3.1.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
compile group: 'io.netty', name: 'netty', version: '4.0.0.Alpha8'
compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
compile group: 'org.springframework', name: 'spring-context-support', version: '4.1.3.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '4.1.3.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.3.RELEASE'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.7'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.5'
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.6.3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.3'
compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.2'
compile group: 'org.jdom', name: 'jdom', version: '1.1.3'
compile group: 'com.github.sgroschupf', name: 'zkclient', version: '0.1'
}
spring + dubbo 学习的更多相关文章
- Dubbo学习小记
前言 周一入职的新公司,到了公司第一件事自然是要熟悉新公司使用的各种技术,搭建本地的环境. 熟悉新公司技术的过程中,首先就是Maven,这个前面已经写过文章了,然后就是Dubbo----公司的服务都是 ...
- Spring Dubbo 开发笔记(一)——概述
概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...
- dubbo学习(zz)
dubbo学习 博客分类: 开源软件 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站 ...
- Dubbo学习(六) dubbo 架构图 以及调用过程
一.Dubbo结构图 duubo结构图 我们解释以下这个架构图:Consumer服务消费者,Provider服务提供者.Container服务容器.消费当然是invoke提供者了,invoke这条 ...
- Dubbo学习系列之九(Shiro+JWT权限管理)
村长让小王给村里各系统来一套SSO方案做整合,隔壁的陈家村流行使用Session+认证中心方法,但小王想尝试点新鲜的,于是想到了JWT方案,那JWT是啥呢?JavaWebToken简称JWT,就是一个 ...
- Dubbo学习系列之十五(Seata分布式事务方案TCC模式)
上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...
- Dubbo学习系列之十六(ELK海量日志分析框架)
外卖公司如何匹配骑手和订单?淘宝如何进行商品推荐?或者读者兴趣匹配?还有海量数据存储搜索.实时日志分析.应用程序监控等场景,Elasticsearch或许可以提供一些思路,作为业界最具影响力的海量搜索 ...
- 15 个优秀开源的 Spring Boot 学习项目,一网打尽!
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...
- Dubbo学习系列之十一(Dashboard+Nacos规则推送)
中国武术,门派林立,都是号称多少代的XXX传人,结果在面对现代武术时,经常被KO秒杀,为啥,光靠宣传和口号撑门面,终究是靠不住,必须得有真货 ,得经得住考验,所以不能只说Sentinel有多好,也得给 ...
随机推荐
- 12.数值的整数次方 Java
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 这道题看似简单,其实BUG重重.要注意的问题: 1 关于次幂的问题特殊的情况, ...
- 2018-2019-20175329 实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告
2018-2019-20175329 实验三敏捷开发与XP实践<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...
- NDK的环境配置
http://www.androiddevtools.cn/ 下载NDK, 最新版本. 解压压缩包,如解压后文件夹名为如android-ndk-r13,放在指定的位置 配置环境变量: 系统环境path ...
- SOAP1.1&SOAP1.2
http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationString SOAP 1.1 以下 ...
- Mybaits-Maven项目之逆向工程(代码自动生成)
1.添加maven插件,让maven环境支持mybatis-generator组件 在pom.xml里面添加如下代码: <project xmlns="http://maven.apa ...
- springboot中静态属性/静态方法从YAML(yml)读取配置属性
启动类添加注解@EnableConfigurationProperties import jnetman.session.SnmpPref; import org.springframework.bo ...
- 关于Yii的ocracle链接问题
1. http://www.yiiframework.com/extension/oci8pdo/ 2.下载extension包,根据配置可解决.
- C#-WinForm跨线程修改UI界面
待解决的问题 在我做WinForm开发的过程中,经常会遇到耗时操作或阻塞操作.他们会引发软件的卡顿甚至假死,严重影响软件的使用. 因此,这类耗时或阻塞的操作一般都会使用异步的方式去执行,不影响主线程( ...
- 7-1 shell编程基础之二
shell编程基础之二 算数运算 bash中的算术运算:help let +, -, *, /, %取模(取余), **(乘方),乘法符号有些场景中需要转义 实现算术运算: (1) let var=算 ...
- CTF攻防练习之综合训练1
主机:192.168.32.152 靶机:192.168.32.166 首先使用nmap,nikto -host ,dirb扫描,探测靶场开放了 21,22,80d端口已经发现有一下关键信息 进入lo ...