SpringBoot 整合 dubbo xml实现代码示例

时间:2021-08-31 10:09:40

昨天发布了注解方式,有人给我发了邮件希望能出一版本xml格式的,本来12点前能搞定的但是电脑稍微出了问题,导致idea 疯狂奔溃,搞了很久废话不多说了,有错误之处望大家指出发我邮箱.

dubbo肯定是多模块化了 所以我们先创建一个聚合项目

SpringBoot 整合 dubbo xml实现代码示例

这是项目结构

SpringBoot 整合 dubbo xml实现代码示例

dubbo_demo 的pom 这个主要用来聚合业务模块用不做任何业务处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.hzb</groupid>
  <artifactid>dubbo_demo</artifactid>
  <version>0.0.1-snapshot</version>
  <!--我们这是多模块开发 所以这边要 变成 pom-->
  <packaging>pom</packaging>
  <!--系统模块-->
  <modules>
    <module>hzbdubbo-consumer</module>
    <module>hzbdubbo-provider</module>
  </modules>
  <!--配置文件属性-->
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <java.version>1.8</java.version>
  </properties>
</project>

然后按照上面的方法创建两个项目一个是 服务的提供者 和服务的消费者

提供者和消费者不懂的话点击这个连接了解一下 http://dubbo.io/

SpringBoot 整合 dubbo xml实现代码示例

服务提供者代码片段:

生成基本项目按第一个图片的去生成

代码结构: 由于是demo不按规范结构来看得懂就行

SpringBoot 整合 dubbo xml实现代码示例

服务提供者pom :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 
xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
 
<groupid>com.hzb.dubbo.provider</groupid>
<artifactid>hzbdubbo-provider</artifactid>
<packaging>jar</packaging>
<name>hzbdubbo-provider</name>
<description>demo project for spring boot</description>
 
<parent>
  <groupid>com.hzb</groupid>
   <artifactid>dubbo_demo</artifactid>
   <version>0.0.1-snapshot</version>
</parent>
 
<properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
</properties>
 
<dependencies>
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifactid>
    <version>1.5.2.release</version>
  </dependency>
 
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter</artifactid>
    <version>1.5.2.release</version>
  </dependency>
 
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-test</artifactid>
    <scope>test</scope>
    <version>1.5.2.release</version>
  </dependency>
  
  <dependency>
    <groupid>com.alibaba</groupid>
    <artifactid>dubbo</artifactid>
    <version>2.4.10</version>
    <exclusions>
      <exclusion>
        <artifactid>spring</artifactid>
        <groupid>org.springframework</groupid>
      </exclusion>
    </exclusions>
  </dependency>
  
  <dependency>
    <groupid>org.apache.zookeeper</groupid>
    <artifactid>zookeeper</artifactid>
    <version>3.4.6</version>
    <exclusions>
      <exclusion>
        <artifactid>slf4j-log4j12</artifactid>
        <groupid>org.slf4j</groupid>
      </exclusion>
    </exclusions>
  </dependency>
  
  <dependency>
    <groupid>com.github.sgroschupf</groupid>
    <artifactid>zkclient</artifactid>
    <version>0.1</version>
  </dependency>
  
</dependencies>
 
<build>
  <plugins>
    <plugin>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-maven-plugin</artifactid>
      <version>1.5.2.release</version>
    </plugin>
  </plugins>
</build>
 
</project>

注意pom中 parent 对应的是什么和聚合pom进行关联

dubbo.xml中配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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://code.alibabatech.com/schema/dubbo"
  xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
  <!-- 配置可参考 http://dubbo.io/user+guide-zh.htm -->
  <!-- 服务提供方应用名,用于计算依赖关系 -->
  <dubbo:application name="dubbo-provider" owner="dubbo-provider" />
  
  <!-- 定义 zookeeper 注册中心地址及协议 -->
  <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" />
    
  <!-- 定义 dubbo 协议名称及使用的端口,dubbo 协议缺省端口为 20880,如果配置为 -1 或者没有配置 port,则会分配一个没有被占用的端口 -->
  <dubbo:protocol name="dubbo" port="-1" />
  
  <!-- 声明需要暴露的服务接口 -->
  <dubbo:service interface="com.hzb.dubbo.provider.demoservice"
    ref="demoservice" timeout="10000" />
    
  <!-- 和本地 bean 一样实现服务 -->
  <bean id="demoservice" class="com.hzb.dubbo.provider.demoserviceimpl" />
  
</beans>

这个想要了解配置的意思点击这个连接:http://dubbo.io/books/dubbo-user-book-en/英文版https://help.github.com/categories/github-pages-basics/

服务提供者启动类代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.hzb.dubbo.provider;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.importresource;
 
@springbootapplication
// 使用 dubbo.xml 配置
@importresource(value = {"classpath:dubbo.xml"})
public class dubboproviderapplication {
 
  public static void main(string[] args) {
 
    springapplication.run(dubboproviderapplication.class, args);
    system.out.println("服务提供者启动成功");
  }
}

服务 service接口代码:

SpringBoot 整合 dubbo xml实现代码示例
业务实现代码:“

SpringBoot 整合 dubbo xml实现代码示例

接下来就是服务消费者了:

项目结构:

SpringBoot 整合 dubbo xml实现代码示例

消费者pom 也是要注意pom中和上面有哪些不一样 和 多了那个依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.hzb.dubbo.consumer</groupid>
  <artifactid>hzbdubbo-consumer</artifactid>
  <packaging>jar</packaging>
  <name>hzbdubbo-consumer</name>
  <description>demo project for spring boot</description>
 
  <parent>
    <groupid>com.hzb</groupid>
     <artifactid>dubbo_demo</artifactid>
     <version>0.0.1-snapshot</version>
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
      <version>1.5.2.release</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter</artifactid>
      <version>1.5.2.release</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <version>1.5.2.release</version>
      <scope>test</scope>
    </dependency>
 
    <!-- 阿里巴巴提供的格式化依赖 -->
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>fastjson</artifactid>
      <version>1.1.41</version>
    </dependency>
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>dubbo</artifactid>
      <version>2.4.10</version>
      <exclusions>
        <exclusion>
          <artifactid>spring</artifactid>
          <groupid>org.springframework</groupid>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupid>org.apache.zookeeper</groupid>
      <artifactid>zookeeper</artifactid>
      <version>3.4.6</version>
      <exclusions>
        <exclusion>
          <artifactid>slf4j-log4j12</artifactid>
          <groupid>org.slf4j</groupid>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupid>com.github.sgroschupf</groupid>
      <artifactid>zkclient</artifactid>
      <version>0.1</version>
    </dependency>
    <dependency>
      <groupid>com.hzb.dubbo.provider</groupid>
      <artifactid>hzbdubbo-provider</artifactid>
      <version>0.0.1-snapshot</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
        <version>1.5.2.release</version>
      </plugin>
    </plugins>
  </build>
</project>

dubbo.xml配置:

SpringBoot 整合 dubbo xml实现代码示例

启动类:

SpringBoot 整合 dubbo xml实现代码示例

测试调用服务controller

SpringBoot 整合 dubbo xml实现代码示例

接下来就是先把服务提供者启动起来 然后在启动消费者

运行结果

SpringBoot 整合 dubbo xml实现代码示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000013591862