zoukankan      html  css  js  c++  java
  • springboot2.0+dubbo整合分布式服务发布和调用

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

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

    <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的结合包使用的是如下依赖:

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

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

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

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

     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

    第四:提供程序启动类

    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

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

    消费方的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进行验证即可!

    如果有朋友需要注解实现的代码,请留言!!!                     

  • 相关阅读:
    【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
    【BZOJ4196】[Noi2015]软件包管理器 树链剖分
    【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ
    【BZOJ4278】[ONTAK2015]Tasowanie 后缀数组
    mysql中使用concat例子
    SAP basis 常用事物
    推和敲
    踏和走
    下一个该你啦
    长城:恐惧的纪念碑
  • 原文地址:https://www.cnblogs.com/ljy-20180122/p/9823470.html
Copyright © 2011-2022 走看看