最新springboot-整合dubbo研究

     最近研究springboot整合dubbo,之前学习到的是说springboot不太支持javabean配置的方式,所以之前的项目都是采用xml的方式(这种方式就不再这里介绍了。)

     Dubbo现在2018年已经进入apache的开源孵化器。就Springboot整合这块,发现不同的人有不同的处理方式。

1.先说说依赖:

第一种(未找到出处,个人觉得是谁写的一个整合依赖而已:spring-boot-starter+zkclient+dubbo)未研究不推荐:

<dependency>
    <groupId>io.dubbo.springboot</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
</dependency>

第二种(apache下的整合依赖,版本问题参考官方资料分0.2.x和0.1.x,分别支持springboot2.x和springboot1.x):

具体参考:https://github.com/apache/incubator-dubbo-spring-boot-project

官方推荐使用0.2.x

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

第三种(alibaba出的整合包,虽然进入了apache,自身也在维护dubbo。目前该项目已经停止维护了)

具体参考:https://github.com/alibaba/dubbo-spring-boot-starter

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

结论:使用第二种整合包即可以,不需要每样弄精通。

2.说说dubbo的使用;

基本的一些源码和概念参考如下资料:

https://blog.csdn.net/flashflight/article/details/43939275

https://blog.csdn.net/flashflight/article/details/44318447

https://blog.csdn.net/flashflight/article/details/44473617

https://blog.csdn.net/flashflight/article/details/44529805

3.整合步骤:

A.provider的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>springboot</groupId>
    <artifactId>springboot-dubbo-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-dubbo 服务端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</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>

        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </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>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

B.实体类


public class City implements Serializable {

    private static final long serialVersionUID = -1L;

    /**
     * 城市编号
     */
    private Long id;

    /**
     * 省份编号
     */
    private Long provinceId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 描述
     */
    private String description;

    public City() {
    }

    public City(Long id, Long provinceId, String cityName, String description) {
        this.id = id;
        this.provinceId = provinceId;
        this.cityName = cityName;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

C.服务的接口,需要暴露的API

public interface CityDubboService {

    /**
     * 根据城市名称,查询城市信息
     * @param cityName
     */
    City findCityByName(String cityName);
}

D.服务的提供者的实现类。注意@Service注解是dubbo下面的注解。使用该注解即可以达到向zookeeper注册的目的。

package org.spring.springboot.dubbo.impl;

// 注册为 Dubbo 服务
@Service(version = "1.0.0")
public class CityDubboServiceImpl implements CityDubboService {

    public City findCityByName(String cityName) {
        return new City(1L,2L,"温岭","是我的故乡");
    }
}                                                            

E.application.properties

# Spring boot application
spring.application.name = city-server
#management.port = 9091

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
# 需要扫描的包
dubbo.scan.basePackages  = org.spring.springboot.dubbo
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = city-server
dubbo.application.name = city-provider

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
dubbo.registry.id = my-registry
# 这里是zk的连接配置
dubbo.registry.address = zookeeper://192.168.25.128:2181

F.consumer的pom文件和provider一致,application.properties也没多大区别。

## 避免和 server 工程端口冲突
server.port=8088
## Dubbo 服务消费者配置
# Spring boot application
spring.application.name = city-consumer
#management.port = 9091

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
# 需要扫描的包
dubbo.scan.basePackages  = org.spring.springboot.dubbo
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = city-consumer
dubbo.application.name = city-consumer

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
dubbo.registry.id = my-registry
# 这里是zk的连接配置
dubbo.registry.address = zookeeper://192.168.25.128:2181

G.接口类一致,避免耦合,所以各自写了一份,事实上可以抽取出来单独写一个依赖API,让consumer和provider直接去依赖这个API更好,这里暂时未实现。

H.消费者类。

package org.spring.springboot.dubbo;

import com.alibaba.dubbo.config.annotation.Reference;
import org.spring.springboot.domain.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created By 夜羽 on 2018/7/8
 */
@RestController
public class CityController {


    @Reference(version = "1.0.0")
    CityDubboService cityDubboService;

    @RequestMapping("/hello")
    public City showCity() {
        String cityName="温岭";
        City city = cityDubboService.findCityByName(cityName);
        return city;
    }
}

I.测试页面省略

最后总结下:

1》配置文件application.properties参考官方,具体有些参数是否可以不配置,暂时没去研究。

2》官方Service注解里面配置的application.protocol.registry不配置也是可以(未深入研究)。

@Service(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)

3》有的文章说需要加入zkclient依赖,个人觉得不是必须的

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.2</version>
</dependency>

ZkClient是由Datameer的工程师开发的开源客户端,对Zookeeper的原生API进行了包装,实现了超时重连、Watcher反复注册等功能。ZkClient的运用本人未研究,有知道的同学可以告诉下!

PS:例子是参考的别人的例子。数据并未去查找DAO层。


没有更多推荐了,返回首页