3.Dubbo之SpringBoot篇

 

GitHub 详细介绍地址

https://github.com/apache/incubator-dubbo-spring-boot-project/blob/master/README_CN.md

https://github.com/apache/incubator-dubbo-spring-boot-project/tree/master/dubbo-spring-boot-actuator#health-checks  //端点

Dubbo Spring Boot 工程致力于简化 Dubbo RPC 框架在 Spring Boot 应用场景的开发。同时也整合了 Spring Boot 特性:

开发版本

从现在开始, dubbo-spring-boot-project 将在每个发布中发行两个版本 :

  • 0.2.x 是支持 Spring Boot 2.x 的主要版本(推荐,长期维护)

  • 0.1.x 是支持 Spring Boot 1.x 的维护版本(兼容,短期维护)

依赖关系

版本JavaSpring BootDubbo
0.2.0 1.8+ 2.0.x 2.6.2+
0.1.1 1.7+ 1.5.x 2.6.2+

已发行版本

您可以为您的工程引入最新 dubbo-spring-boot-starter 的发布,增加以下依赖到工程的 pom.xml 文件中:

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

 

如果您的工程遇到了依赖问题, 请尝试添加如下 Maven 参考到工程的 pom.xml 文件中:

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

 

通常情况 , Dubbo 应用有两种使用场景 , 其一为 Dubbo 服务提供方 , 另外一个是 Dubbo 服务消费方,当然也允许两者混合.

示例演示

新建dubbo-spring-boot maven项目,工程结构如下:

dubbo-spring-boot 

pom.xml配置如下:

springboot版本为2.0.3

<!--dependencyManagement父类工程管理包  -->
<dependencyManagement>
        <dependencies>
            <!--引入springboot  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
<modules>
      <module>dubbo-spring-boot-api</module>
      <module>dubbo-spring-boot-provider</module>
      <module>dubbo-spring-boot-consumer</module>
</modules>

 

dubbo-spring-boot-api

对外暴露RPC接口API模块

pom.xml

<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>
    <parent>
        <groupId>org.niugag</groupId>
        <artifactId>dubbo-spring-boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-spring-boot-api</artifactId>
    <description>对外暴露rpc接口</description>
</project>

 

bean

package org.niugang.bean;
/**
 * 
 * @ClassName:  DefaultBean   
 * @Description:默认Bean
 * @author: niugang
 * @date:   2018年8月22日 下午5:30:27   
 * @Copyright: 863263957@qq.com. All rights reserved. 
 *
 */
public class DefaultBean {
    private String str;
    private String methodName;
    private long timestamp;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "DefaultBean [str=" + str + ", methodName=" + methodName + ", timestamp=" + timestamp + "]";
    }
    

}

 

service

package org.niugang.service;

/**
 * 
 * @ClassName:  DefaultApiService   
 * @Description:对外暴露API接口  
 * @author: niugang
 * @date:   2018年8月22日 下午5:27:21   
 * @Copyright: 863263957@qq.com. All rights reserved. 
 *
 */
public interface DefaultApiService {

    String defaultMethod(String str);

}

 

dubbo-spring-boot-provider

需要引入:dubbo-spring-boot-api

pom.xml配置如下:

<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>
    <parent>
        <groupId>org.niugag</groupId>
        <artifactId>dubbo-spring-boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-spring-boot-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入dubbo 集成springboot starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.niugag</groupId>
            <artifactId>dubbo-spring-boot-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
    </dependencies>
</project>

 

DefaultServiceImpl.java

对外暴露服务接口实现类

package org.niugang.service;

import org.niugang.bean.DefaultBean;

import com.alibaba.dubbo.config.annotation.Service;

/**
 * 
 * @ClassName:  DefaultDemoService   
 * @Description:对外暴露接口实现类
 * @author: niugang
 * @date:   2018年8月17日 下午7:50:47   
 * @Copyright: 863263957@qq.com. All rights reserved. 
 *
 */
//demo.service.version 在application.properties中配置过了
@Service
public class DefaultServiceImpl implements DefaultApiService{

    public String defaultMethod(String str) {
        DefaultBean defaultBean = new DefaultBean();
        defaultBean.setStr(str);
        defaultBean.setMethodName("defaultMethod");
        defaultBean.setTimestamp(System.currentTimeMillis());
        return defaultBean.toString();
    }



}

 

 

application.properties

# springboot应用
spring.application.name = dubbo-provider-demo
server.port = 9090

# 对外暴露服务版本
demo.service.version = 1.0.0

# 扫描带阿里注解的的类(e.g @Service , @Reference)
dubbo.scan.basePackages  = org.niugang.service

# Dubbo Config properties
## 应用配置
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

## 协议配置
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

##注册配置  采用redis注册中心
dubbo.registry.id = my-registry
dubbo.registry.address =redis://localhost:6379

 

启动类

package org.niugang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 * @ClassName:  DubboProviderApplication   
 * @Description:启动类 
 * @author: niugang
 * @date:   2018年8月17日 下午7:52:56   
 * @Copyright: 863263957@qq.com. All rights reserved. 
 *
 */
@SpringBootApplication
public class DubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args); 
    }

}

 

dubbo-spring-boot-consumer

需要引入:dubbo-spring-boot-api

pom.xml配置如下

<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>
    <parent>
        <groupId>org.niugag</groupId>
        <artifactId>dubbo-spring-boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-spring-boot-consumer</artifactId>
    <dependencies>
        <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>
        <!--redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!--引入对外暴露的rpc -->
        <dependency>
            <groupId>org.niugag</groupId>
            <artifactId>dubbo-spring-boot-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

 

DemoConsumerController.java

web层调用服务提供者对我暴露的rpc接口

package org.niugang.controller;

import org.niugang.service.DefaultApiService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

/**
 * 
 * @ClassName: DemoConsumerController
 * @Description:web调用服务提供者对外暴露的rpc接口
 * @author: niugang
 * @date: 2018年8月18日 上午9:41:30
 * @Copyright: 863263957@qq.com. All rights reserved.
 *
 */
@RestController
public class DemoConsumerController {

    /**
     * 引入服务提供者
     */
    // com.alibaba.dubbo.config.annotation.Reference
    @Reference
    private DefaultApiService demoService;

    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return demoService.defaultMethod(name);
    }

}

 

application.properties

#springboot应用名
spring.application.name = dubbo-consumer-demo
server.port = 8080

# 服务版本
demo.service.version = 1.0.0

# Dubbo Config properties
## 应用配置
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## 协议配置  Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

#dubbo 2.6.2版本需要写注册中心配置
dubbo.registry.address =redis://localhost:6379

 

启动类

package org.niugang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 * @ClassName:  DubboConsumerApplication   
 * @Description:消费者
 * @author: niugang
 * @date:   2018年8月17日 下午8:50:33   
 * @Copyright: 863263957@qq.com. All rights reserved. 
 *
 */
@SpringBootApplication
public class DubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args); 
    }

}

 

启动服务提供者,服务消费者

访问消费者web接口。

 

源码地址:https://gitee.com/niugangxy/dubbo 

 

微信公众号

                          

                               

posted @ 2020-01-13 15:51  盲目的拾荒者  阅读(754)  评论(0编辑  收藏  举报