基于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 }

猜你喜欢

转载自www.cnblogs.com/fanqisoft/p/10358001.html