Dubbo(三)------Dubbo入门示例(基于XML配置)

阳光穿透心脏的1/2处 2023-05-30 09:08 184阅读 0赞

前言

本篇将通过搭建简单demo来讲解dubbo的使用。
官方文档

在这里插入图片描述

环境要求:

apache-zookeeper-3.5.6 :dubbo的注册中心

dubbo-2.4.9

dubbo-admin :查看服务的提供方与消费方

项目结构图

在这里插入图片描述

dubbo-api :接口定义模块
dubbo-provider :服务提供方模块
dubbo-consumer:服务消费者模块

一、搭建项目

1、dubbo 工程

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.yi</groupId>
  6. <artifactId>dubbo</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <properties>
  10. <dubbo.version>2.6.6</dubbo.version>
  11. <spring.version>4.3.6.RELEASE</spring.version>
  12. <java.version>1.8</java.version>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>3.8.1</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  23. <dependency>
  24. <groupId>com.alibaba</groupId>
  25. <artifactId>dubbo</artifactId>
  26. <version>${dubbo.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.apache.zookeeper</groupId>
  30. <artifactId>zookeeper</artifactId>
  31. <version>3.4.10</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.101tec</groupId>
  35. <artifactId>zkclient</artifactId>
  36. <version>0.5</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>io.netty</groupId>
  40. <artifactId>netty-all</artifactId>
  41. <version>4.1.32.Final</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.apache.curator</groupId>
  45. <artifactId>curator-framework</artifactId>
  46. <version>2.8.0</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.curator</groupId>
  50. <artifactId>curator-recipes</artifactId>
  51. <version>2.8.0</version>
  52. </dependency>
  53. <!-- spring相关 -->
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-core</artifactId>
  57. <version>${spring.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-beans</artifactId>
  62. <version>${spring.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-context</artifactId>
  67. <version>${spring.version}</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-jdbc</artifactId>
  72. <version>${spring.version}</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-web</artifactId>
  77. <version>${spring.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-webmvc</artifactId>
  82. <version>${spring.version}</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.springframework</groupId>
  86. <artifactId>spring-aop</artifactId>
  87. <version>${spring.version}</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>org.springframework</groupId>
  91. <artifactId>spring-tx</artifactId>
  92. <version>${spring.version}</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.springframework</groupId>
  96. <artifactId>spring-orm</artifactId>
  97. <version>${spring.version}</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>org.springframework</groupId>
  101. <artifactId>spring-context-support</artifactId>
  102. <version>${spring.version}</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.springframework</groupId>
  106. <artifactId>spring-test</artifactId>
  107. <version>${spring.version}</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.springframework</groupId>
  111. <artifactId>spring-jms</artifactId>
  112. <version>${spring.version}</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>org.aspectj</groupId>
  116. <artifactId>aspectjrt</artifactId>
  117. <version>1.6.11</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>org.aspectj</groupId>
  121. <artifactId>aspectjweaver</artifactId>
  122. <version>1.6.11</version>
  123. </dependency>
  124. </dependencies>
  125. <build>
  126. <plugins>
  127. <plugin>
  128. <groupId>org.apache.maven.plugins</groupId>
  129. <artifactId>maven-compiler-plugin</artifactId>
  130. <version>3.8.0</version>
  131. <configuration>
  132. <source>1.8</source>
  133. <target>1.8</target>
  134. </configuration>
  135. </plugin>
  136. </plugins>
  137. </build>
  138. <modules>
  139. <module>dubbo-api</module>
  140. <module>dubbo-consumer</module>
  141. <module>dubbo-provider</module>
  142. </modules>
  143. </project>

2、dubbo-api模块

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.yi</groupId>
  6. <artifactId>dubbo-api</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <dependencies>
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactId>junit</artifactId>
  12. <version>4.12</version>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. </properties>
  19. <build>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-compiler-plugin</artifactId>
  24. <version>3.8.0</version>
  25. <configuration>
  26. <source>1.8</source>
  27. <target>1.8</target>
  28. </configuration>
  29. </plugin>
  30. </plugins>
  31. </build>
  32. </project>

建立TestService接口类
在这里插入图片描述

  1. package com.yi;
  2. /**
  3. * 接口
  4. */
  5. public interface TestService {
  6. String SayHello(String word);
  7. }

3、dubbo-provider 模块(服务提供方)

pom依赖同父工程,并且引入dubbo-api模块

  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.yi</groupId>
  6. <artifactId>dubbo-provider</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <properties>
  9. <dubbo.version>2.6.6</dubbo.version>
  10. <spring.version>4.3.6.RELEASE</spring.version>
  11. <java.version>1.8</java.version>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>3.8.1</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.yi</groupId>
  23. <artifactId>dubbo-api</artifactId>
  24. <version>1.0-SNAPSHOT</version>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  27. <dependency>
  28. <groupId>com.alibaba</groupId>
  29. <artifactId>dubbo</artifactId>
  30. <version>${dubbo.version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.zookeeper</groupId>
  34. <artifactId>zookeeper</artifactId>
  35. <version>3.4.10</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.101tec</groupId>
  39. <artifactId>zkclient</artifactId>
  40. <version>0.5</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>io.netty</groupId>
  44. <artifactId>netty-all</artifactId>
  45. <version>4.1.32.Final</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.apache.curator</groupId>
  49. <artifactId>curator-framework</artifactId>
  50. <version>2.8.0</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.apache.curator</groupId>
  54. <artifactId>curator-recipes</artifactId>
  55. <version>2.8.0</version>
  56. </dependency>
  57. <!-- spring相关 -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-core</artifactId>
  61. <version>${spring.version}</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-beans</artifactId>
  66. <version>${spring.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-context</artifactId>
  71. <version>${spring.version}</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework</groupId>
  75. <artifactId>spring-jdbc</artifactId>
  76. <version>${spring.version}</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.springframework</groupId>
  80. <artifactId>spring-web</artifactId>
  81. <version>${spring.version}</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring-webmvc</artifactId>
  86. <version>${spring.version}</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.springframework</groupId>
  90. <artifactId>spring-aop</artifactId>
  91. <version>${spring.version}</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.springframework</groupId>
  95. <artifactId>spring-tx</artifactId>
  96. <version>${spring.version}</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.springframework</groupId>
  100. <artifactId>spring-orm</artifactId>
  101. <version>${spring.version}</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.springframework</groupId>
  105. <artifactId>spring-context-support</artifactId>
  106. <version>${spring.version}</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>org.springframework</groupId>
  110. <artifactId>spring-test</artifactId>
  111. <version>${spring.version}</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-jms</artifactId>
  116. <version>${spring.version}</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.aspectj</groupId>
  120. <artifactId>aspectjrt</artifactId>
  121. <version>1.6.11</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>org.aspectj</groupId>
  125. <artifactId>aspectjweaver</artifactId>
  126. <version>1.6.11</version>
  127. </dependency>
  128. </dependencies>
  129. <build>
  130. <plugins>
  131. <plugin>
  132. <groupId>org.apache.maven.plugins</groupId>
  133. <artifactId>maven-compiler-plugin</artifactId>
  134. <version>3.8.0</version>
  135. <configuration>
  136. <source>1.8</source>
  137. <target>1.8</target>
  138. </configuration>
  139. </plugin>
  140. </plugins>
  141. </build>
  142. </project>

在这里插入图片描述

建立TestServiceImpl实现类,对dubbo-api中接口进行实现。

  1. package com.yi;
  2. import org.springframework.stereotype.Service;
  3. @Service("testService")
  4. public class TestServiceImpl implements TestService {
  5. @Override
  6. public String SayHello(String word) {
  7. return word;
  8. }
  9. }

在这里插入图片描述

dubbo-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"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  9. <!-- 提供方应用信息,当前项目在整个分布式架构里面的唯一名称,用于计算依赖关系 -->
  10. <dubbo:application name="dubbo_provider" owner="yi">
  11. <dubbo:parameter key="qos.enable" value="true"/>
  12. <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
  13. <dubbo:parameter key="qos.port" value="55555"/>
  14. </dubbo:application>
  15. <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
  16. <!-- 使用zookeeper注册中心暴露服务地址 -->
  17. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  18. <!--N/A 表示由 dubbo 自动分配地址 ,这里也可以通过直连的方式,不通过注册中心-->
  19. <!--<dubbo:registry address="N/A" />-->
  20. <!--当前服务发布所依赖的协议;支持多种协议:webserovice、Thrift、Hessain、http-->
  21. <!-- 这里采用dubbo协议在20880端口暴露服务 -->
  22. <dubbo:protocol name="dubbo" port="20880" />
  23. <!-- 声明需要暴露的服务接口 -->
  24. <dubbo:service interface="com.yi.TestService" ref="testService" />
  25. </beans>

① dubbo配置文件类型spring配置

② 节点:dubbo:application 就是整个项目在分布式架构中的唯一名称,可以在 name 属性中配置,另外还可以配置 owner 字段,表示属于谁。 下面的参数是可以不配置的,这里配置是因为出现了端口的冲突,所以配置。

③ 节点:dubbo:monitor 监控中心配置, 用于配置连接监控中心相关信息,可以不配置,不是必须的参数。

④ 节点:dubbo:registry 配置注册中心的信息,比如,这里我们可以配置 zookeeper 作为我们的注册中心。 address 是注册中心的地址,这里我们配置的是 N/A 表示由 dubbo 自动分配地址。或者说是一种直连的方式,不通过注册中心。

⑤ 节点:dubbo:protocol 服务发布的时候 dubbo 依赖什么协议,可以配置 dubbo、webserovice、Thrift、Hessain、http等协议。

⑥ 节点:dubbo:service 这个节点就是我们的重点了,当我们服务发布的时候,我们就是通过这个配置将我们的服务发布出去的。 interface 是接口的包路径, ref 是第 ⑦ 点配置的接口的 bean。

⑦ 最后,我们需要像配置 spring 的接口一样,配置接口的 bean。

在这里插入图片描述

springmvc.mxl

  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  10. <aop:aspectj-autoproxy />
  11. <context:component-scan base-package="com.yi" />
  12. <import resource="classpath:dubbo-provider.xml" />
  13. </beans>

启动服务之前(先启动zookeeper,先启动zookeeper,先启动zookeeper)

在这里插入图片描述

  1. package com.yi;
  2. import java.io.IOException;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class App {
  5. public static void main(String[] args) throws Exception {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
  7. context.start();
  8. System.out.println("dubbo_provider 已启动!");
  9. try {
  10. System.in.read(); // 按任意键退出
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }

启动dubbo-admin
访问地址:http://127.0.0.1:8088/dubbo-admin-2.5.9/

不清楚的话,参会 Dubbo(一)———Dubbo-admin管理平台的安装
在这里插入图片描述

4、dubbo-consumer (服务消费者模块)

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.yi</groupId>
  6. <artifactId>dubbo-consumer</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <properties>
  9. <dubbo.version>2.6.6</dubbo.version>
  10. <spring.version>4.3.6.RELEASE</spring.version>
  11. <java.version>1.8</java.version>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>3.8.1</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.yi</groupId>
  23. <artifactId>dubbo-api</artifactId>
  24. <version>1.0-SNAPSHOT</version>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  27. <dependency>
  28. <groupId>com.alibaba</groupId>
  29. <artifactId>dubbo</artifactId>
  30. <version>${dubbo.version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.zookeeper</groupId>
  34. <artifactId>zookeeper</artifactId>
  35. <version>3.4.10</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.101tec</groupId>
  39. <artifactId>zkclient</artifactId>
  40. <version>0.5</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>io.netty</groupId>
  44. <artifactId>netty-all</artifactId>
  45. <version>4.1.32.Final</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.apache.curator</groupId>
  49. <artifactId>curator-framework</artifactId>
  50. <version>2.8.0</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.apache.curator</groupId>
  54. <artifactId>curator-recipes</artifactId>
  55. <version>2.8.0</version>
  56. </dependency>
  57. <!-- spring相关 -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-core</artifactId>
  61. <version>${spring.version}</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-beans</artifactId>
  66. <version>${spring.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-context</artifactId>
  71. <version>${spring.version}</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework</groupId>
  75. <artifactId>spring-jdbc</artifactId>
  76. <version>${spring.version}</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.springframework</groupId>
  80. <artifactId>spring-web</artifactId>
  81. <version>${spring.version}</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring-webmvc</artifactId>
  86. <version>${spring.version}</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.springframework</groupId>
  90. <artifactId>spring-aop</artifactId>
  91. <version>${spring.version}</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.springframework</groupId>
  95. <artifactId>spring-tx</artifactId>
  96. <version>${spring.version}</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.springframework</groupId>
  100. <artifactId>spring-orm</artifactId>
  101. <version>${spring.version}</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.springframework</groupId>
  105. <artifactId>spring-context-support</artifactId>
  106. <version>${spring.version}</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>org.springframework</groupId>
  110. <artifactId>spring-test</artifactId>
  111. <version>${spring.version}</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-jms</artifactId>
  116. <version>${spring.version}</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.aspectj</groupId>
  120. <artifactId>aspectjrt</artifactId>
  121. <version>1.6.11</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>org.aspectj</groupId>
  125. <artifactId>aspectjweaver</artifactId>
  126. <version>1.6.11</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>org.testng</groupId>
  130. <artifactId>testng</artifactId>
  131. <version>RELEASE</version>
  132. <scope>test</scope>
  133. </dependency>
  134. <dependency>
  135. <groupId>junit</groupId>
  136. <artifactId>junit</artifactId>
  137. <version>4.12</version>
  138. <scope>test</scope>
  139. </dependency>
  140. </dependencies>
  141. <build>
  142. <plugins>
  143. <plugin>
  144. <groupId>org.apache.maven.plugins</groupId>
  145. <artifactId>maven-compiler-plugin</artifactId>
  146. <version>3.8.0</version>
  147. <configuration>
  148. <source>1.8</source>
  149. <target>1.8</target>
  150. </configuration>
  151. </plugin>
  152. </plugins>
  153. </build>
  154. </project>

在这里插入图片描述

dubbo-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://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. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  9. <dubbo:application name="dubbo_consumer" owner="yi"/>
  10. <!-- 使用multicast广播注册中心暴露发现服务地址 -->
  11. <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
  12. <!-- 生成远程服务代理,可以和本地bean一样使用testService-->
  13. <dubbo:reference id="testService" interface="com.yi.TestService" />
  14. </beans>

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/aop
  6. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  11. http://www.springframework.org/schema/util
  12. http://www.springframework.org/schema/util/spring-util-4.0.xsd"
  13. default-autowire="byName">
  14. <aop:aspectj-autoproxy />
  15. <context:component-scan base-package="com.yi" />
  16. <import resource="classpath:/dubbo-consumer.xml" />
  17. </beans>

加载Spring配置,调用服务

  1. package com.yi;
  2. import java.io.IOException;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class App {
  5. public static void main(String[] args) {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
  7. context.start();
  8. TestService testService = (TestService) context.getBean("testService");
  9. System.out.println(testService.SayHello("hello world"));
  10. try {
  11. System.in.read();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }

在这里插入图片描述

项目代码实例下载


如果你觉得本篇文章对你有所帮助的话,麻烦请点击头像右边的关注按钮,谢谢!

技术在交流中进步,知识在分享中传播

发表评论

表情:
评论列表 (有 0 条评论,184人围观)

还没有评论,来说两句吧...

相关阅读