查看: 643|回复: 0

[Java学习] SpringBoot 整合 dubbo xml实现代码示例

发表于 2018-4-15 08:00:01

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

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


这是项目结构

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hzb</groupId>
  6. <artifactId>Dubbo_demo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <!--我们这是多模块开发 所以这边要 变成 pom-->
  9. <packaging>pom</packaging>
  10. <!--系统模块-->
  11. <modules>
  12. <module>hzbdubbo-consumer</module>
  13. <module>hzbdubbo-provider</module>
  14. </modules>
  15. <!--配置文件属性-->
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. </project>
复制代码

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

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

服务提供者代码片段:

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

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

服务提供者pom :

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hzb.dubbo.provider</groupId>
  6. <artifactId>hzbdubbo-provider</artifactId>
  7. <packaging>jar</packaging>
  8. <name>hzbdubbo-provider</name>
  9. <description>Demo project for Spring Boot</description>
  10. <parent>
  11. <groupId>com.hzb</groupId>
  12. <artifactId>Dubbo_demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. <version>1.5.2.RELEASE</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter</artifactId>
  29. <version>1.5.2.RELEASE</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. <version>1.5.2.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.alibaba</groupId>
  39. <artifactId>dubbo</artifactId>
  40. <version>2.4.10</version>
  41. <exclusions>
  42. <exclusion>
  43. <artifactId>spring</artifactId>
  44. <groupId>org.springframework</groupId>
  45. </exclusion>
  46. </exclusions>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.zookeeper</groupId>
  50. <artifactId>zookeeper</artifactId>
  51. <version>3.4.6</version>
  52. <exclusions>
  53. <exclusion>
  54. <artifactId>slf4j-log4j12</artifactId>
  55. <groupId>org.slf4j</groupId>
  56. </exclusion>
  57. </exclusions>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.github.sgroschupf</groupId>
  61. <artifactId>zkclient</artifactId>
  62. <version>0.1</version>
  63. </dependency>
  64. </dependencies>
  65. <build>
  66. <plugins>
  67. <plugin>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-maven-plugin</artifactId>
  70. <version>1.5.2.RELEASE</version>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>
复制代码

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

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

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

服务提供者启动类代码:

  1. package com.hzb.dubbo.provider;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ImportResource;
  5. @SpringBootApplication
  6. // 使用 dubbo.xml 配置
  7. @ImportResource(value = {"classpath:dubbo.xml"})
  8. public class DubboProviderApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DubboProviderApplication.class, args);
  11. System.out.println("服务提供者启动成功");
  12. }
  13. }
复制代码

服务 service接口代码:


业务实现代码:“

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

项目结构:


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

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

dubbo.xml配置:


启动类:


测试调用服务Controller

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

运行结果

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



回复

使用道具 举报