(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. day9 笔记

    集合 去重 无序 元素不可变类型 可hash 命令 set() 创建可变集合 frozenset() 创建不可变集合 .add() 添加元素,只能放单个值 .update() 添加元素,可以更新多个值 ...

  2. 自学Python2.10-跳出循环(break、continue)

    自学Python之路 自学Python2.10-跳出循环(break.continue) 1.跳出循环break, 跳出同层的循环 break语句可以跳出for和while的循环体. 如果你从for或 ...

  3. 自学huawei之路-AC6005版本升级步骤

    返回自学Huawei之路 自学huawei之路-AC6005版本升级步骤 本文主要采用WEB网管界面升级,方便快捷,推荐使用此方法.     一.升级前检查 1.1 原AC/AP设备版本确认 disp ...

  4. BZOJ 4785 [Zjoi2017]树状数组 | 二维线段树

    题目链接 BZOJ 4785 题解 这道题真是令人头秃 = = 可以看出题面中的九条可怜把求前缀和写成了求后缀和,然后他求的区间和却仍然是sum[r] ^ sum[l - 1],实际上求的是闭区间[l ...

  5. SharePoint 2013 批量导入、删除帐号

    删除一个group里所有的帐号: cls ########################### # "Enter the site URL here" $SITEURL = &q ...

  6. bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)

    就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...

  7. 【翻译】go memory model

    https://studygolang.com/articles/819 原文链接 Introduction The Go memory model specifies the conditions ...

  8. $\mathcal{FFT}$·$\mathcal{Fast \ \ Fourier \ \ Transformation}$快速傅立叶变换

    \(2019.2.18upd:\) \(LINK\) 之前写的比较适合未接触FFT的人阅读--但是有几个地方出了错,大家可以找一下233 啊-本来觉得这是个比较良心的算法没想到这么抽搐这个算法真是将一 ...

  9. STM8S ------ VCAP download

    There is a specific pin called vcap in stm8s mcu. I recommend this pin connects to a 1uF capacitor w ...

  10. promise第一篇-简介

    1. 创建一个promise对象 var promise = new Promise(function(resolve, reject){ //异步处理 //处理结束后调用resolve或reject ...