IT狗

玩转Spring Boot 集成Dubbo

玩转Spring Boot 集成Dubbo


      运用Spring Boot 与Dubbo集成,这里我之前测验考试了运用注解的方法,简洁的运用注解注册办事事实上是没有成绩的,可是当你触及到运用注解的时间在办事内里引用事件,注入其他对象的时间,会有一些成绩。因而我就武断废弃了注解了,运用的是XML,这里大概引见的是Dubbo,可是假如运用Dubbox的话,基本上是兼容的。接下来,将谈谈运用XML的方法与Spring Boot在一路开发。

1.创立项目在pom.xml中插手依靠

      创立项目名为:
      (1)springboot-dubbo-provide
      (2)springboot-dubbo-api
      (3)springboot-dubbo-consume
        springboot-dubbo-api项目重要是放一些service接口,用于供给给消费者运用 。springboot-dubbo-provide项目用于供给办事。  springboot-dubbo-consume项目为消费者。在springboot-dubbo-provide项目中翻开pom.xml插手以下依靠,完备代码以下:
[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.chengli</groupId>  
  5.     <artifactId>springboot-dubbo-provide</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>jar</packaging>  
  8.     <name>springboot-dubbo-provide</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <parent>  
  11.         <groupId>org.springframework.boot</groupId>  
  12.         <artifactId>spring-boot-starter-parent</artifactId>  
  13.         <version>1.4.3.RELEASE</version>  
  14.     </parent>  
  15.     <properties>  
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  17.         <java.version>1.8</java.version>  
  18.         <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version>  
  19.         <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version>  
  20.         <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version>  
  21.     </properties>  
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>com.chengli</groupId>  
  25.             <artifactId>springboot-dubbo-api</artifactId>  
  26.             <version>0.0.1-SNAPSHOT</version>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>org.springframework.boot</groupId>  
  30.             <artifactId>spring-boot-starter</artifactId>  
  31.         </dependency>  
  32.         <!-- dubbo -->  
  33.         <dependency>  
  34.             <groupId>com.alibaba</groupId>  
  35.             <artifactId>dubbo</artifactId>  
  36.             <exclusions>  
  37.                 <exclusion>  
  38.                     <groupId>org.springframework</groupId>  
  39.                     <artifactId>spring</artifactId>  
  40.                 </exclusion>  
  41.             </exclusions>  
  42.             <version>${com.alibaba.dubbo.version}</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>org.apache.zookeeper</groupId>  
  46.             <artifactId>zookeeper</artifactId>  
  47.             <version>${org.apache.zookeeper.version}</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>com.github.sgroschupf</groupId>  
  51.             <artifactId>zkclient</artifactId>  
  52.             <version>${com.github.sgroschupf.zkclient.version}</version>  
  53.         </dependency>  
  54.     </dependencies>  
  55.     <build>  
  56.         <plugins>  
  57.             <plugin>  
  58.                 <groupId>org.springframework.boot</groupId>  
  59.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  60.             </plugin>  
  61.         </plugins>  
  62.     </build>  
  63. </project>  

翻开springboot-dubbo-consume项目,在pom.xml中插手以下依靠,完备代码以下:
[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.chengli</groupId>  
  5.     <artifactId>springboot-dubbo-consume</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>jar</packaging>  
  8.     <name>springboot-dubbo-consume</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <parent>  
  11.         <groupId>org.springframework.boot</groupId>  
  12.         <artifactId>spring-boot-starter-parent</artifactId>  
  13.         <version>1.4.3.RELEASE</version>  
  14.     </parent>  
  15.     <properties>  
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  17.         <java.version>1.8</java.version>  
  18.         <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version>  
  19.         <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version>  
  20.         <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version>  
  21.     </properties>  
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>com.chengli</groupId>  
  25.             <artifactId>springboot-dubbo-api</artifactId>  
  26.             <version>0.0.1-SNAPSHOT</version>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>org.springframework.boot</groupId>  
  30.             <artifactId>spring-boot-starter-web</artifactId>  
  31.         </dependency>  
  32.         <!-- dubbo -->  
  33.         <dependency>  
  34.             <groupId>com.alibaba</groupId>  
  35.             <artifactId>dubbo</artifactId>  
  36.             <exclusions>  
  37.                 <exclusion>  
  38.                     <groupId>org.springframework</groupId>  
  39.                     <artifactId>spring</artifactId>  
  40.                 </exclusion>  
  41.             </exclusions>  
  42.             <version>${com.alibaba.dubbo.version}</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>org.apache.zookeeper</groupId>  
  46.             <artifactId>zookeeper</artifactId>  
  47.             <version>${org.apache.zookeeper.version}</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>com.github.sgroschupf</groupId>  
  51.             <artifactId>zkclient</artifactId>  
  52.             <version>${com.github.sgroschupf.zkclient.version}</version>  
  53.         </dependency>  
  54.         <dependency>  
  55.             <groupId>org.springframework.boot</groupId>  
  56.             <artifactId>spring-boot-configuration-processor</artifactId>  
  57.             <optional>true</optional>  
  58.         </dependency>  
  59.     </dependencies>  
  60.     <build>  
  61.         <plugins>  
  62.             <plugin>  
  63.                 <groupId>org.springframework.boot</groupId>  
  64.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  65.             </plugin>  
  66.         </plugins>  
  67.     </build>  
  68. </project>  

2.Dubbo设置

2.1springboot-dubbo-provide办事供给者

      (1)在springboot-dubbo-provide项目中创立出口启动类MainConfig,完备代码以下:
[java] view plain copy
  1. package com.chengli.springboot;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6. @SpringBootApplication  
  7. public class MainConfig {  
  8.     public static void main(String[] args) {  
  9.         SpringApplication.run(MainConfig.class, args);  
  10.         try {  
  11.             System.in.read();  
  12.         } catch (Exception e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  
  16. }  

      (2)创立Dubbo设置类
[java] view plain copy
  1. package com.chengli.springboot.dubbo;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.context.annotation.ImportResource;  
  5. import org.springframework.context.annotation.PropertySource;  
  6.   
  7. @Configuration  
  8. @PropertySource("classpath:dubbo/dubbo.properties")  
  9. @ImportResource({ "classpath:dubbo/*.xml" })  
  10. public class DubboConfig {  
  11.   
  12. }  

     (3)创立Dubbo设置文件
在src/main/resources下新建文件夹dubbo,并插手以下设置:
dubbo-provider.xml内容以下:
[html] view plain copy
  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.     <!-- 供给方运用信息,用于计较依靠干系 -->  
  10.     <dubbo:application name="${dubbo.application.name}" />  
  11.   
  12.     <!-- 注册中央露出办事地点 -->  
  13.     <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->  
  14.       
  15.    <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> -->  
  16.    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />  
  17.   
  18.     <!-- 露出办事 -->  
  19.     <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />  
  20.       
  21.     <dubbo:service interface="com.chengli.springboot.example.service.ExampleService"  
  22.         ref="exampleServiceImpl" retries="0" timeout="6000" />  
  23. </beans>  
    留意:这里我公布的example办事是示例,详细的按照实践修正

    (4)创立dubbo.properties
[html] view plain copy
  1. #运用称号  
  2. dubbo.application.name=example-provider  
  3. #注册中央范例  
  4. dubbo.registry.protocol=zookeeper  
  5. #注册中央地点  
  6. dubbo.registry.address=127.0.0.1:2181  
  7. #露出办事方法  
  8. dubbo.protocol.name=dubbo  
  9. #露出办事端口  
  10. dubbo.protocol.port=20880  

2.2springboot-dubbo-consume办事消费者

    (1)创立出口启动类MainConfig
[java] view plain copy
  1. package com.chengli.springboot;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6. @SpringBootApplication  
  7. public class MainConfig {  
  8.     public static void main(String[] args) {  
  9.         SpringApplication.run(MainConfig.class, args);  
  10.     }  
  11. }  

    (2)创立Dubbo设置类
[java] view plain copy
  1. package com.chengli.springboot.dubbo;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.context.annotation.ImportResource;  
  5. import org.springframework.context.annotation.PropertySource;  
  6.   
  7. @Configuration  
  8. @PropertySource("classpath:dubbo/dubbo.properties")  
  9. @ImportResource({ "classpath:dubbo/*.xml" })  
  10. public class DubboConfig {  
  11.   
  12. }  

    (3)创立Dubbo设置文件
在src/main/resources下新建文件夹dubbo,并插手以下设置:
dubbo-consume.xml内容以下:
[html] view plain copy
  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.     <!-- 供给方运用信息,用于计较依靠干系 -->  
  10.     <dubbo:application name="${dubbo.application.name}" />  
  11.   
  12.     <!-- 注册中央露出办事地点 -->  
  13.    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />  
  14.       
  15.     <dubbo:reference id="axempleService" interface="com.chengli.springboot.example.service.ExampleService" />  
  16. </beans>  
    
    (4)创立dubbo.properties
[html] view plain copy
  1. #运用称号  
  2. dubbo.application.name=example-consume  
  3. #注册中央范例  
  4. dubbo.registry.protocol=zookeeper  
  5. #注册中央地点  
  6. dubbo.registry.address=127.0.0.1:2181  

到这里基本上就曾经可以了,不外测试类的代码我就不贴下去了。只需在API中界说接口完成就可以。运用Spring Boot 与Dubbo集成的时间,须要留意的是,不要运用Spring Boot供给的devtools热启动,由于devtools供给了两个ClassLoader,加载计谋成绩引发呈现毛病,没法启动。假如开发中须要热加载,那末运用Spring 供给的springloaded。

有兴味的朋侪可以加群探究彼此学习:

Spring Boot QQ交流群:599546061

此文由 IT狗 编辑,本网站所发布展示的作品/文章版权归原作者所有,任何商业用途均须联系作者!

相关推荐

评论 暂无评论