springboot2.0+dubbo整合分布式服务发布和调用

       最近项目上要对之前的老项目作分布式的整改,所以我专门花了点时间研究下当前比较热门的dubbo和springboot结合使用,之前使用过dubbo,可是没有与springboot结合过,在网上查了点资料,感受要么是springboot版本太低,要么是dubbo版本太低,反正基本是千篇一概,查考价值不大。下面咱们就直入主题,看下springboot+dubbo的简单使用:java

     首先是新建三个项目:dubbo-common:  主要存放服务发布的接口以及发布和调用端的公共对象,dubbo-provider:   服务提供方                    dubbo-consumer:  服务消费方。新建项目就不说了,直接上maven依赖,其中dubbo-provier和dubbo-consumer的依赖基本同样git

 

<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>org.dubbo</groupId>
  <artifactId>dubbo-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 引入springboot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  
  <dependencies>
  
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
        <exclusions>
             <exclusion>
                 <artifactId>spring</artifactId>
                 <groupId>org.springframework</groupId>
             </exclusion>
         </exclusions>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.20.0-GA</version>
    </dependency>
    
    <dependency>
        <groupId>org.jboss.netty</groupId>
        <artifactId>netty</artifactId>
        <version>3.2.5.Final</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.10</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.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
          </plugin>
          
          <!-- Spingboot的插件 -->
          <plugin>
              <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>
  
</project>
View Code

 

你们从pom文件中能够看到,个人项目使用的是springboot的2.0.4版本,dubbo使用的是2.6.2,重点是dubbo和springboot的结合包使用的是以下依赖:github

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>web

这个很重要,由于不一样的dubbo和springboot的版本不同,可能要选择的结合包也不同。这里我只展现使用xml来实现简单功能,首先咱们先来看xml配置使用:spring

第一:在公共模块增长一个接口代码,代码略apache

第二:在提供方实现相应的接口,代码没有任何业务逻辑,只是演示用,json

 1 package org.yongcheng.liuyang.service.impl;  2  3 import org.yongcheng.liuyang.service.TestService;  4  5 import com.alibaba.dubbo.config.annotation.Service;  6  7 //@Service(version = "1.0.0")  8 @org.springframework.stereotype.Service  9 public class TestServiceImpl implements TestService { 10 11  @Override 12 public String sayHello(String name) { 13 return name+"hello"; 14  } 15 }
View Code

第三:配置dubbo.xml文件具体以下代码:浏览器

<?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://dubbo.apache.org/schema/dubbo" 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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-provider" /> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明须要暴露的服务接口 --> <dubbo:service interface="org.yongcheng.liuyang.service.TestService" ref="demoService" /> <!-- 和本地bean同样实现服务 --> <bean id="demoService" class="org.yongcheng.liuyang.service.impl.TestServiceImpl" /> </beans>
View Code

第四:提供程序启动类springboot

package org.yongcheng.liuyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:dubbo.xml") public class ProviderApp { public static void main(String[] args) throws Exception { SpringApplication.run(ProviderApp.class, args); } }
View Code

好了提供方的代码完成,接下俩就是消费方的步骤:app

消费方的pom依赖和提供方基本同样,直接复用,可是因为消费方使用的是web工程,所以须要多一个依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

第一:pom文件:

<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>org.yongcheng.liuyang</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  
  <dependencies>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
        <exclusions>
             <exclusion>
                 <artifactId>spring</artifactId>
                 <groupId>org.springframework</groupId>
             </exclusion>
         </exclusions>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.20.0-GA</version>
    </dependency>
    
    <dependency>
        <groupId>org.jboss.netty</groupId>
        <artifactId>netty</artifactId>
        <version>3.2.5.Final</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.10</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.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
          </plugin>
          
          <!-- Spingboot的插件 -->
          <plugin>
              <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>
</project>
View Code

第二:简单业务代码实现:

package org.yongcheng.liuyang.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.yongcheng.liuyang.service.TestService; import com.alibaba.dubbo.config.annotation.Reference; @RestController @RequestMapping(produces="application/json;charset=UTF-8") public class IndexActroller { // @Reference(url="dubbo://localhost:20880")
 @Autowired private TestService testService; @GetMapping("/sayHello") public String sayHello(String name){ return testService.sayHello(name); } }
View Code

第三:配置dubbo.xml文件具体以下代码:

<?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://dubbo.apache.org/schema/dubbo" 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">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-consumer"  />
 
    <dubbo:registry address="zookeeper://localhost:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明须要暴露的服务接口 -->
    <dubbo:reference interface="org.yongcheng.liuyang.service.TestService" id="demoService" />
 
</beans>
View Code

第四:提供程序启动类

package org.yongcheng.liuyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:dubbo.xml") public class ConsumerApp { public static void main(String[] args) throws Exception { SpringApplication.run(ConsumerApp.class, args); } }
View Code

好了,这样总体的发布和调用代码已经完毕,接下来先启动提供方的代码,而后在启动调用方的程序,而后咱们在浏览器中输入咱们的url进行验证便可!

若是有朋友须要注解实现的代码,请留言!!!                     

相关文章
相关标签/搜索