基于XML搭建Dubbo项目
(1)、新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等。

公共Bean:
package cn.coreqi.entities;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String userName;
private String passWord;
private Integer enabled;
public User() {
}
public User(Integer id, String userName, String passWord, Integer enabled) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
this.enabled = enabled;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public Integer getEnabled() {
return enabled;
}
public void setEnabled(Integer enabled) {
this.enabled = enabled;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", enabled=" + enabled +
'}';
}
}
公共服务接口:
package cn.coreqi.service;
import cn.coreqi.entities.User;
import java.util.List;
public interface UserService {
public void addUser(User user);
public void delById(Integer id);
public void modifyUser(User user);
public User getById(Integer id);
public List<User> getList();
}
(2)、新建一个普通Maven项目,用作与服务提供者
1)、导入相关依赖
<dependencies>
<dependency>
<groupId>cn.coreqi</groupId>
<artifactId>DubboXmlApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency> <!--我这里使用zookeeper作为dubbo的注册中心-->
<!--dubbo2.6以前的版本使用zkclient操作zookeeper-->
<!--dubbo2.6及以后的版本使用curator操作zookeeper-->
<!--根据dubbo的版本二选其一--> <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency> <!--<dependency>-->
<!--<groupId>com.101tec</groupId>-->
<!--<artifactId>zkclient</artifactId>-->
<!--<version>0.11</version>-->
<!--</dependency>--> </dependencies>
2)、编写服务提供者配置文件,新建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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)-->
<dubbo:application name="user-provider"/> <!--2、指定注册中心的位置(注册中心不同,服务地址的写法不同)-->
<!--<dubbo:registry address="redis://192.168.205.128:6379"/>-->
<dubbo:registry address="zookeeper://192.168.205.128:2181"/> <!--3、指定通信规则(通信协议&通信端口)-->
<dubbo:protocol name="dubbo" port="20880"/> <!--4、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象-->
<dubbo:service ref="userService" interface="cn.coreqi.service.UserService"/> <bean id="userService" class="cn.coreqi.service.impl.UserServiceImpl"/> </beans>
3)、启动服务提供者
package cn.coreqi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
(3)、新建一个普通Maven项目,用作与服务消费者
1)、导入相关依赖(和服务提供者相同,此处略)
2)、编写服务消费者配置文件,新建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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!--1、消费者应用名-->
<dubbo:application name="user-consumer"/> <!--2、指定注册中心的位置-->
<dubbo:registry address="zookeeper://192.168.205.128:2181"/> <!--3、声明需要调用的远程服务接口,生成远程服务代理,可以和本地Bean一样使用-->
<dubbo:reference id="userService" interface="cn.coreqi.service.UserService"/>
</beans>
3)、测试服务消费者
package cn.coreqi; import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
ioc.start();
UserService userService = (UserService) ioc.getBean("userService");
User user = userService.getById(1);
System.out.println(user.toString());
}
}
基于XML搭建Dubbo项目的更多相关文章
- 基于XML搭建SpringMVC项目
*如果你需要将应用部署到不支持Servlet3.0容器中 或者 你只是对web.xml情有独钟,那我们只能按照传统的方式,通过web.xml来配置SpringMVC. *搭建SpringMVC需要在w ...
- 基于docker搭建laravel项目
基于docker搭建laravel项目 公司PHP项目是Laravel框架写的,目前环境需要通过docker来部署一下.网上学习了一下相关知识.整理后做一个笔记.用到定时任务crontab与进程管理s ...
- 搭建DUBBO项目解决DUBBO.XML标签报错的问题(转载)
https://www.cnblogs.com/ajax-li/p/7856393.html 报错内容: Multiple annotations found at this line: - cvc- ...
- 基于Homestead搭建PHP项目开发环境(适合Zend Framework,Laravel,Yii,thinkphp等)
参考: https://framework.zend.com/bl...参考: https://laravel.com/docs/5.5/... 第一步:软件的下载和安装 软件1:VirtualBox ...
- [python]基于windows搭建django项目
1.首先我的环境用到的库版本如下,若下载直接pip即可 pip3 install Django==2.0.6pip3 install djangorestframework==3.8.2pip3 in ...
- 基于springboot构建dubbo的入门demo
之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...
- dubbo入门教程-从零搭建dubbo服务
[原创 转载请注明出处] 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章. 另外dubbo也有官方文档,但是比较官方,也可以多看看dubbo的 ...
- 02.基于IDEA+Spring+Maven搭建测试项目--详细过程
一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...
- SpringMvc+JavaConfig+Idea 基于JavaConfig搭建项目
1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于Jav ...
随机推荐
- Sabotage UVA - 10480 (输出割边)
题意:....emm...就是一个最小割最大流,.,...用dinic跑一遍.. 然后让你输出割边,就是 u为能从起点到达的点, v为不能从起点到达的点 最后在残余路径中用dfs跑一遍 能到达的路 ...
- Rsync 客户端配置
安装rsync服务yum -y install rsync 创建密码文件rsync.passwordvi /etc/rsync.password只存储密码即可,无需用户名.密码为Rsync服务器端的/ ...
- 自学Aruba5.2-Aruba安全认证-有PEFNG 许可证环境的角色策略管理
点击返回:自学Aruba之路 自学Aruba5.2-Aruba安全认证- 有PEFNG 许可证环境的角色策略管理 导入许可后,可以对Role进行配置: 1. 系统自带的Role的可以修改的属性: 2. ...
- [NOI2012] 骑行川藏 | 求导 二分
一个能看的题解!预备知识只有高中数学的[导数].不用什么偏导数/拉格朗日乘子法之类的我看不懂的东西( •̀∀•́ )! 如果你不知道什么是导数,可以找本高中数学选修2-2来看一下!看第一章第1.2节就 ...
- display position 和float的作用和关系
1.传统布局由这三者构成. 2.position设为absolute,那么display一定是block,因此对于span来说,就可以设置高和宽了. 3.position为relative ,那么fl ...
- 【ATcoder】Xor Sum 2
题目大意:给定一个 N 个点的序列,求有多少个区间满足\(\oplus_{i=l}^ra[i]=\sum\limits_{i=l}^ra[i]\). 题解: 小结论:\(a\oplus b=a+b\r ...
- python(六)——基本数据类型介绍
1.数字整形 python3不管数字有多大都是int型,没有long类型 1>字符串转换为数字 s1 = " print(type(s1),s1) b = int(s1)#不加base ...
- 【BLUESKY的NOIp模拟赛】解题报告
昨天晚上熬夜熬得有点严重,今天比赛的时候状态不好,成绩爆炸... 不得不说BLUESKY007 出的题还是相当不错的,也为我提醒了几个需要补的漏洞方向,这里作一下整理. \(Task 1\):探索 ...
- TestCase / test plan / test case
s 测试计划 分析测试需求 制定测试策略 制定测试计划 评审测试计划 测试准备 编写测试案例 评审测试案例 准备测试环境 准备测试数据 准备配置项 测试执行 检查测试准入(环境.数据.配置.案例等) ...
- 【转载】关闭XenServer中挂起(hang)虚机的方法
在XenServer中,碰到VM挂起(hang)的情况,也不是那么少见,而VM长时间挂起,那么很影响心情和后续的操作. 一般情况下,为了关闭VM或者重启VM,我们推荐这样的操作顺序: 进入到VM内,使 ...