转载

SpringBoot 整合 dubbo xml实现

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

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

SpringBoot 整合 dubbo xml实现

这是项目结构

SpringBoot 整合 dubbo xml实现

Dubbo_demo 的pom 这个主要用来聚合业务模块用不做任何业务处理
<?xml version="1.0" encoding="UTF-8"?>
<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>com.hzb</groupId>
    <artifactId>Dubbo_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!--我们这是多模块开发 所以这边要 变成 pom-->
    <packaging>pom</packaging>
    <!--系统模块-->
    <modules>
        <module>hzbdubbo-consumer</module>
        <module>hzbdubbo-provider</module>
    </modules>
    <!--配置文件属性-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>



</project>
然后按照上面的方法创建两个项目一个是 服务的提供者 和服务的消费者
提供者和消费者不懂的话点击这个连接了解一下http://dubbo.io/

SpringBoot 整合 dubbo xml实现

服务提供者代码片段:
   生成基本项目按第一个图片的去生成
代码结构: 由于是demo不按规范结构来看得懂就行

SpringBoot 整合 dubbo xml实现

服务提供者pom :

<?xml version="1.0" encoding="UTF-8"?>

<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>com.hzb.dubbo.provider</groupId>
<artifactId>hzbdubbo-provider</artifactId>
<packaging>jar</packaging>
<name>hzbdubbo-provider</name>
<description>Demo project for Spring Boot</description>

<parent>  
    <groupId>com.hzb</groupId>
      <artifactId>Dubbo_demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.5.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <version>1.5.2.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.4.10</version>
        <exclusions>
            <exclusion>
                <artifactId>spring</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</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.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.2.RELEASE</version>
        </plugin>
    </plugins>
</build>

</project>

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

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

这个想要了解配置的意思点击这个连接: http://dubbo.io/books/dubbo-u... 英文版

http://dubbo.io/books/dubbo-u...

服务提供者启动类代码:

package com.hzb.dubbo.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
// 使用 dubbo.xml 配置
@ImportResource(value = {"classpath:dubbo.xml"})
public class DubboProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(DubboProviderApplication.class, args);
        System.out.println("服务提供者启动成功");
    }
}

服务 service接口代码:

SpringBoot 整合 dubbo xml实现

业务实现代码:“

SpringBoot 整合 dubbo xml实现

接下来就是服务消费者了:
项目结构:

SpringBoot 整合 dubbo xml实现

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

<?xml version="1.0" encoding="UTF-8"?>
<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>com.hzb.dubbo.consumer</groupId>
    <artifactId>hzbdubbo-consumer</artifactId>
    <packaging>jar</packaging>
    <name>hzbdubbo-consumer</name>
    <description>Demo project for Spring Boot</description>

    <parent>  
        <groupId>com.hzb</groupId>
          <artifactId>Dubbo_demo</artifactId>
          <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- 阿里巴巴提供的格式化依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</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>
        <dependency>
            <groupId>com.hzb.dubbo.provider</groupId>
            <artifactId>hzbdubbo-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>


</project>

dubbo.xml配置:

SpringBoot 整合 dubbo xml实现

启动类:

SpringBoot 整合 dubbo xml实现

测试调用服务Controller

SpringBoot 整合 dubbo xml实现

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

运行结果

SpringBoot 整合 dubbo xml实现

代码地址我过几天上传github了在发布出来 现在太累了码不动了

有问题请发我邮箱,多多指教wavesape@126.com
                                        addPerson : hzb 2018-3-8 01:44:23
原文  https://segmentfault.com/a/1190000013591862
正文到此结束
Loading...