(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项目的更多相关文章

  1. 基于XML搭建SpringMVC项目

    *如果你需要将应用部署到不支持Servlet3.0容器中 或者 你只是对web.xml情有独钟,那我们只能按照传统的方式,通过web.xml来配置SpringMVC. *搭建SpringMVC需要在w ...

  2. 基于docker搭建laravel项目

    基于docker搭建laravel项目 公司PHP项目是Laravel框架写的,目前环境需要通过docker来部署一下.网上学习了一下相关知识.整理后做一个笔记.用到定时任务crontab与进程管理s ...

  3. 搭建DUBBO项目解决DUBBO.XML标签报错的问题(转载)

    https://www.cnblogs.com/ajax-li/p/7856393.html 报错内容: Multiple annotations found at this line: - cvc- ...

  4. 基于Homestead搭建PHP项目开发环境(适合Zend Framework,Laravel,Yii,thinkphp等)

    参考: https://framework.zend.com/bl...参考: https://laravel.com/docs/5.5/... 第一步:软件的下载和安装 软件1:VirtualBox ...

  5. [python]基于windows搭建django项目

    1.首先我的环境用到的库版本如下,若下载直接pip即可 pip3 install Django==2.0.6pip3 install djangorestframework==3.8.2pip3 in ...

  6. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  7. dubbo入门教程-从零搭建dubbo服务

    [原创 转载请注明出处] 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章. 另外dubbo也有官方文档,但是比较官方,也可以多看看dubbo的 ...

  8. 02.基于IDEA+Spring+Maven搭建测试项目--详细过程

    一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...

  9. SpringMvc+JavaConfig+Idea 基于JavaConfig搭建项目

    1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于Jav ...

随机推荐

  1. hdu 4897 Little Devil I (树链剖分+线段树)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...

  2. 【Gym - 101124A】The Baguette Master (数学,几何)

    BUPT2017 wintertraining(15) #4F Gym - 101124A 题意 给定画框宽度,画的四边和一个对角线长度,求画框外沿周长. 题解 过顶点做画框的垂线,每个角都得到两个全 ...

  3. 【BZOJ4000】【LOJ2104】【TJOI2015】棋盘 (状压dp + 矩阵快速幂)

    Description ​ 有一个\(~n~\)行\(~m~\)列的棋盘,棋盘上可以放很多棋子,每个棋子的攻击范围有\(~3~\)行\(~p~\)列.用一个\(~3 \times p~\)的矩阵给出了 ...

  4. 【BZOJ2424】[HAOI2010]订货(费用流)

    [BZOJ2424][HAOI2010]订货(费用流) 题面 BZOJ 洛谷 题解 傻逼费用流吧... 一开始理解错意思了,仓库大小为\(m\)的含义是留到下个月最多为\(m\),而不是任意时刻的容量 ...

  5. 利用spring boot+vue做的一个博客项目

    技术栈: 后端 Springboot druid Spring security 数据库 MySQL 前端 vue elementUI 项目演示: GitHub地址: 后端:https://githu ...

  6. Codeforces-gym-101020 problem C. Rectangles

    题目链接:http://codeforces.com/gym/101020/problem/C C. Rectangles time limit per test 2.0 s memory limit ...

  7. CrossFire Round #481 div.3 978 打后感

    虚拟赛,头一次打div.3感觉好TM水啊...... 一共7道题,我A了6道,第7题有思路但是没时间了. 结果还是排在700多名,可能其他人也觉得太水了吧. 逐一解析题目: A好简单,因为不想离散化我 ...

  8. A1070. Mooncake

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  9. win32 窗口缩放时出现闪屏

    今天无意发现之前写的一个小工具在缩放窗口的时候,出现闪屏,主要有两个位置: 工具栏出一闪屏 右侧的控制面板出现闪屏 (这个控制面板与多层元件组合而成) 之前真没注意到这个问题,平时都是最大化/恢复窗口 ...

  10. XML:特殊字符转换

    <     < 小于号 >     > 大于号 &    & 和 &apos;   ' 单引号 "   " 双引号 实体必须以符号& ...