spring-boot系列:(一)整合dubbo

spring-boot-2整合dubbo

新框架学习,必须上手干。书读百遍,其义自见。

本文主要介绍spring-boot-2整合dubbo,使用xml配置实现一个provider和consumer。

1.创建Maven项目:

spring-boot-parent:父工程,统一管理jar依赖,版本

    spring-boot-service:子工程,dubbo服务提供方

    spring-boot-web:子工程,dubbo服务消费方

2.spring-boot-parent: 父工程,统一管理jar依赖,版本

pom文件

    <!--集中定义依赖版本号-->
    <properties>
        <maven_javadoc_version>3.0.1</maven_javadoc_version>
        <maven_surefire_version>2.19.1</maven_surefire_version>
        <spring-boot.version>2.1.2.RELEASE</spring-boot.version>
        <jedis.version>2.7.2</jedis.version>
    </properties>

    <!-- 就是管理版本的标签   其下的dependencies标签只是版本的管理 并不依赖jar-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Redis客户端 -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

3.spring-boot-service :子工程,dubbo服务提供方

3.1 pom文件

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

    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--Spring Boot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Dubbo  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!-- Spring Context Extras -->
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!--netty-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.30.Final</version>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- zkClient -->
        <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>
            </plugin>
        </plugins>
    </build>

3.2 新建HelloWorldService服务(接口和实现类)

public interface HelloWorldService {

    String getHello(String msg);
}
public class HelloWorldServiceImpl implements HelloWorldService {

    public String getHello(String msg) {
        return "spring boot dubbo start:" + msg;
    }
}

3.3 配置dubbo-provider.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">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="spring-boot-manager-service"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

    <!-- 用dubbo协议在30001 -->
    <dubbo:protocol name="dubbo" port="28080" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.hao.persistence.service.HelloWorldService" ref="helloWorldService"
                   registry="zookeeper"/>

    <!-- 具体服务接口的实现 -->
    <bean id="helloWorldService" class="com.hao.persistence.service.impl.HelloWorldServiceImpl" />

</beans>

3.4 配置application.properties

#ZooKeeper地址
dubbo.registry.address={ip}:{port}

logging.level.root=INFO

3.5  设置启动类Application 

@SpringBootApplication
@ImportResource("classpath:dubbo-provider.xml")
public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
//        logger.info("项目启动!");
    }
}

运行Application类main方法后,在dubbo控制台查看到我们注册的接口,如下所示,代表服务提供者Provider已经配置完成

 

 4.spring-boot-web:子工程,dubbo服务消费方

4.1  pom文件

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

    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--Spring Boot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Dubbo  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Spring Context Extras -->
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!--netty-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.30.Final</version>
        </dependency>

        <dependency>
            <groupId>com.hao.persistence</groupId>
            <artifactId>spring-boot-manager-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- zkClient -->
        <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>
            </plugin>
        </plugins>
    </build>

4.2 新建HelloWorldController

@RestController
public class HelloWorldController {

    @Autowired
    private HelloWorldService helloWorldService;
    @RequestMapping("getHello")
    public String getHello(){
        String msg = helloWorldService.getHello("haoprogrammer");
        return msg;
    }
}

4.3 dubbo配置文件 dubbo-consumer.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">

    <!-- 消费方服务名 -->
    <dubbo:application name="spring-boot-manager-web"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

    <!-- 引用helloWorldService 服务-->
    <dubbo:reference id="helloWorldService" interface="com.hao.persistence.service.HelloWorldService" check="false"
                   registry="zookeeper" protocol="dubbo" timeout="15000"/>

</beans>

 

4.4 application.properties

这里将web的默认端口改为8081,防止端口冲突

server.port=8081

## Dubbo 服务消费者配置
#ZooKeeper
dubbo.registry.address=10.10.55.69:2181

logging.level.root=INFO

4.5 启动类Application

@SpringBootApplication
@ImportResource("classpath:dubbo-consumer.xml")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

点击运行web项目的main方法,运行成功后如下所示,访问页面url,http://localhost:8081/getHello

 

 

到此,简单的spring-boot-2整合dubbo服务完成。

 

附上 github项目地址:https://github.com/haoprogrammer/spring-boot-parent  

感谢各位老铁关注!

关于最新的  dubbo-spring-boot-stater    的jar包,整合spring-boot自动装配(不用再写xml),健康检查特性,会在后续研究。

dubbo-spring-boot-stater:https://github.com/apache/incubator-dubbo-spring-boot-project

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.1-SNAPSHOT</version>
</dependency>
posted @ 2019-01-20 19:16  haoprogrammer  阅读(926)  评论(4编辑  收藏  举报