基于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 ...
随机推荐
- USACO 好题汇总
背景 这里主要是用来针对USACO上的题目的二次汇总,因为我在刷题的过程中,有的题目我是可以很快想到解决方案的,对于这种题目,就没有必要深究了.但是有一些题目对于我来说还是有一些挑战的,可能用朴素的算 ...
- Android学习系列(17)--App列表之圆角ListView(续)
http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html 本来这篇文章想并到上篇Android学习系列(16)- ...
- CF724F Uniformly Branched Trees
CF724F Uniformly Branched Trees 有根树可以统计.无根树难以统计.因为可以换根. 所以不让换根:只要两个无根树在重心位置不同构,就一定不同构 每个本质不同的树在重心位置统 ...
- A1058. A+B in Hogwarts
If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- a ...
- .net跨防火墙链接oracle连接池链接长时间无通讯数据被断开后报错问题解决
环境: .net 4.0以上使用Oracle.ManagedDataAccess组件链接oracle数据库,应用程序与数据库之间存在硬件防火墙. 症状:应用程序启动后正常访问,如果出现长时间无数据库请 ...
- (reverse) Text Reverse hdu1062
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- H5新特性之canvas
canvas无疑是H5之中最受欢迎的新特性了,它可以让浏览毫无费力的画出各种图案,动画. canvas的性能不会因为画布上的图案多少而改变,因此做动画用canvas性能也相当优秀. canvas最基本 ...
- P5002 专心OI - 找祖先
P5002 专心OI - 找祖先 给定一棵有根树(\(n \leq 10000\)),\(M \leq 50000\) 次询问, 求以 \(x\) 为 \(LCA\) 的点对个数 错误日志: 看下面 ...
- PLSQL Developer 连接Linux 下Oracle的安装与配置
一.下载 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 这是Ora ...
- 函数和常用模块【day06】:configparser模块(七)
本节内容 1.简述 2.配置文件格式 3.创建配置文件 4.读取配置文件 5.增删该查语法 一.简述 在很多情况下,我们都需要修改配置文件,但是,有些配置文件,如mysql数据库的配置文件怎么修改呢? ...