基于XML搭建Dubbo项目

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

公共Bean:

 1 package cn.coreqi.entities;
 2
 3 import java.io.Serializable;
 4
 5 public class User implements Serializable {
 6     private Integer id;
 7     private String userName;
 8     private String passWord;
 9     private Integer enabled;
10
11     public User() {
12     }
13
14     public User(Integer id, String userName, String passWord, Integer enabled) {
15         this.id = id;
16         this.userName = userName;
17         this.passWord = passWord;
18         this.enabled = enabled;
19     }
20
21     public Integer getId() {
22         return id;
23     }
24
25     public void setId(Integer id) {
26         this.id = id;
27     }
28
29     public String getUserName() {
30         return userName;
31     }
32
33     public void setUserName(String userName) {
34         this.userName = userName;
35     }
36
37     public String getPassWord() {
38         return passWord;
39     }
40
41     public void setPassWord(String passWord) {
42         this.passWord = passWord;
43     }
44
45     public Integer getEnabled() {
46         return enabled;
47     }
48
49     public void setEnabled(Integer enabled) {
50         this.enabled = enabled;
51     }
52
53     @Override
54     public String toString() {
55         return "User{" +
56                 "id=" + id +
57                 ", userName=‘" + userName + ‘\‘‘ +
58                 ", passWord=‘" + passWord + ‘\‘‘ +
59                 ", enabled=" + enabled +
60                 ‘}‘;
61     }
62 }

公共服务接口:

 1 package cn.coreqi.service;
 2
 3 import cn.coreqi.entities.User;
 4
 5 import java.util.List;
 6
 7 public interface UserService {
 8     public void addUser(User user);
 9     public void delById(Integer id);
10     public void modifyUser(User user);
11     public User getById(Integer id);
12     public List<User> getList();
13 }

(2)、新建一个普通Maven项目,用作与服务提供者

  1)、导入相关依赖

  

 1     <dependencies>
 2         <dependency>
 3             <groupId>cn.coreqi</groupId>
 4             <artifactId>DubboXmlApi</artifactId>
 5             <version>1.0-SNAPSHOT</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>com.alibaba</groupId>
 9             <artifactId>dubbo</artifactId>
10             <version>2.6.2</version>
11         </dependency>
12
13         <!--我这里使用zookeeper作为dubbo的注册中心-->
14         <!--dubbo2.6以前的版本使用zkclient操作zookeeper-->
15         <!--dubbo2.6及以后的版本使用curator操作zookeeper-->
16         <!--根据dubbo的版本二选其一-->
17
18         <dependency>
19             <groupId>org.apache.curator</groupId>
20             <artifactId>curator-framework</artifactId>
21             <version>2.12.0</version>
22         </dependency>
23
24         <!--<dependency>-->
25             <!--<groupId>com.101tec</groupId>-->
26             <!--<artifactId>zkclient</artifactId>-->
27             <!--<version>0.11</version>-->
28         <!--</dependency>-->
29
30     </dependencies>

  2)、编写服务提供者配置文件,新建provider.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 4        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">
 5     <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)-->
 6     <dubbo:application name="user-provider"/>
 7
 8     <!--2、指定注册中心的位置(注册中心不同,服务地址的写法不同)-->
 9     <!--<dubbo:registry address="redis://192.168.205.128:6379"/>-->
10     <dubbo:registry address="zookeeper://192.168.205.128:2181"/>
11
12     <!--3、指定通信规则(通信协议&通信端口)-->
13     <dubbo:protocol name="dubbo" port="20880"/>
14
15     <!--4、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象-->
16     <dubbo:service ref="userService" interface="cn.coreqi.service.UserService"/>
17
18     <bean id="userService" class="cn.coreqi.service.impl.UserServiceImpl"/>
19
20 </beans>

  3)、启动服务提供者

 1 package cn.coreqi;
 2
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4
 5 import java.io.IOException;
 6
 7 public class MainApplication {
 8     public static void main(String[] args) throws IOException {
 9         ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
10         ioc.start();
11         System.in.read();
12     }
13 }

(3)、新建一个普通Maven项目,用作与服务消费者

  1)、导入相关依赖(和服务提供者相同,此处略)

  2)、编写服务消费者配置文件,新建consumer.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 4        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">
 5
 6     <!--1、消费者应用名-->
 7     <dubbo:application name="user-consumer"/>
 8
 9     <!--2、指定注册中心的位置-->
10     <dubbo:registry address="zookeeper://192.168.205.128:2181"/>
11
12     <!--3、声明需要调用的远程服务接口,生成远程服务代理,可以和本地Bean一样使用-->
13     <dubbo:reference id="userService" interface="cn.coreqi.service.UserService"/>
14 </beans>

  3)、测试服务消费者

 1 package cn.coreqi;
 2
 3 import cn.coreqi.entities.User;
 4 import cn.coreqi.service.UserService;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6
 7 public class MainApplication {
 8     public static void main(String[] args) {
 9         ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
10         ioc.start();
11         UserService userService = (UserService) ioc.getBean("userService");
12         User user = userService.getById(1);
13         System.out.println(user.toString());
14     }
15 }

原文地址:https://www.cnblogs.com/fanqisoft/p/10358001.html

时间: 2024-08-10 02:08:41

基于XML搭建Dubbo项目的相关文章

基于XML搭建SpringMVC项目

*如果你需要将应用部署到不支持Servlet3.0容器中 或者 你只是对web.xml情有独钟,那我们只能按照传统的方式,通过web.xml来配置SpringMVC. *搭建SpringMVC需要在web.xml中注册DispatcherServlet和ContextLoaderListener,同时他们两个之间会分别通过上下文参数contextConfigLocation指定一个XML文件地址来加载Spring应用上下文,而我更倾向于使用Java配置类来加载Spring应用上下文(本文也是如此

基于Spring+IDEA+Maven搭建测试项目

一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具,同时还提供了高级项目管理工具 Jira:是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域 Dubbo:是阿里巴巴公司一个开源的分布式服务框架,致力于提供高性能和

基于Maven搭建SpringMVC+Mybatis项目(4)

从高考,到程序员      CSDN日报20170620--<找一个好工作,谈一份好薪水>      6 月书讯 | 最受欢迎的 SQL 入门书重磅升级 从头开始基于Maven搭建SpringMVC+Mybatis项目(4) 标签:               mavenmybatisspring mvc分页JAVA 2016-07-27 16:53             4598人阅读             评论(0)             收藏              举报 本文章

基于Maven搭建SpringMVC+Mybatis项目(2)

从头开始基于Maven搭建SpringMVC+Mybatis项目(2) 标签:               JAVAmavenmybatisspring mvc 版权声明:欢迎转载, 转载请保留原文链接. 接上文内容,本节介绍Maven的聚合和继承. 从头阅读传送门 互联网时代,软件正在变得越来越复杂,开发人员通常会对软件划分模块,以获得清晰的设计.良好的分工及更高的可重用性.Maven的聚合特性能把多个模块聚合在一起构建,并促进各子模块通过继承父模块的pom配置来保持配置的一致.为了演示这些特

基于Maven搭建SpringMVC+Mybatis项目(3)

| 从高考,到程序员      CSDN日报20170620--<找一个好工作,谈一份好薪水>      6 月书讯 | 最受欢迎的 SQL 入门书重磅升级 从头开始基于Maven搭建SpringMVC+Mybatis项目(3) 标签:               JAVAmavenspring mvcmybatis 2016-07-26 10:06             42087人阅读             评论(2)             收藏              举报 本文

Springmvc基础框架搭建流程(1)-基于xml配置文件

该篇文章对SpringMVC的基本使用过程做简单介绍,这里基于xml配置文件进行配置的.使用的工程为简单的系统登录过程. 1.eclipse下创建web工程,名称为SpringLogin,根目录修改为WebRoot(这样的Web工程可以在myeclipse下正常运行),该工程实现登录功能: 2.在lib中添加springmvc所需的jar包,这里使用的是3.2.9版本的jar包: 3.在src下创建2个包com.by.controller.com.by.service.com.by.manage

Spring 4与Struts 2项目基于XML的集成实战

Spring 4与Struts 2项目基于XML的集成实战 作者:chszs,版权所有,未经同意,不得转载.博主主页:http://blog.csdn.net/chszs 为什么要写这篇Hello World级别的文章.大约十年前,我开始使用Spring 2和Struts 1开发Web应用,构建工具使用的是Ant.早些年,把多个框架集成在一起并不容易,各框架间的兼容性也没有现在这么好.不管怎么样,这些基础的框架伴随我们多年.如今十年过去了,沧海桑田,Spring框架和Struts框架都经过了无数

使用idea搭建maven项目时 java目录下的xml文件没有加载的解决方法

今天在idea集成开发环境下 使用maven搭建了ssm项目,遇到了3个问题 首先我们先复习一下知识点: 第一步:在web.xml中配置spring监听器 <!-- spring监听器 加载spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <con

基于requireJS和Gulp可快速搭建前端项目的脚手架

基于requireJS和Gulp可快速搭建前端项目的脚手架. 项目地址 git clone git@github.com:perfectSymphony/Gulp-cli.git 项目目录 ├── README.md # 项目说明 |—— bin # (在gulpfile文件中使用到)解析layout中的模板html,将完整的html产出到src/html中 ├── dist # 打包生成的项目文件 |—— logs # 监听编译less文件时,打印报错信息, ├── gulpfile.js #