springboot整合dubbo

了解dubbo

在这里插入图片描述
后面被捐赠给了apache基金会,已经毕业了好像…官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
官网介绍的很详细了,具体见官网上面的信息.

引入依赖

可以参考官方文档:
https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md

   <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
           <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.1</version>
        </dependency>
         <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.13.0</version>
        </dependency>

编写服务提供模块

我是在自己以前搭建的一个springboot模块项目里面搭建的…
首先建一个接口模块,因为,服务的提供者和消费者都需要引入这个接口包,当然dubbo也可以通过泛化调用就不需要引入接口包了,那一块没过多了解,有兴趣的时候在弄好了.
在这里插入图片描述
随便建个方法,然后编写个provider,引入interface模块:写个TestDubboService接口的实现类:注意引入的包是dubbo的包.
在这里插入图片描述

import com.mr.interfaces.test.TestDubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;



/**
 * @ClassName TestDubboServiceImpl
 * @Description dubbo测试接口的实现类
 * @Author stack
 * @Version 1.0
 * @since 2019/6/21 10:20
 */
@Component
@Service(version = "1.0",timeout = 10000,interfaceClass = TestDubboService.class)
public class TestDubboServiceImpl implements TestDubboService {

    @Override
    public String testDubbo(String s) {
        return "dubbo测试接口的实现类";
    }
}

一般的话,privider里面依赖dao然后连接数据库等等,这里的话,就不这么干了,就按照官方demo写个简单的测试好了…然后启动报错:可能原因The bean ‘dubboConfigConfiguration.Single’, defined in null
然后编写main方法:


import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.time.Duration;
import java.time.Instant;

/**
 * 启动程序
 */
@SpringBootApplication
@EnableDubbo
public class MyProjectApplication {

    private static final Logger log = LoggerFactory.getLogger(MyProjectApplication.class);

    public static void main(String[] args) {
        Instant inst1 = Instant.now();
        SpringApplication.run(MyProjectApplication.class, args);
        log.info("基于 Spring Boot {} ", SpringBootVersion.getVersion());
        log.info("启动成功!耗时:{}秒 ", Duration.between(inst1, Instant.now()).getSeconds());
    }

}

zk记得打开,启动成功

编写服务消费模块

原来的项目里面有web,我就直接写在web模块里面好了
首先在web的pom里面引入dubbo接口包
在这里插入图片描述
然后在controller里面注入接口里面类:
在这里插入图片描述
服务消费者配置跟提供者配置差不多,注意我们使用的是注解,@Reference,引入的包import org.apache.dubbo.config.annotation.Reference;
然后请求方法,成功了,注意,dubbo调用可以debug的.
在这里插入图片描述
不知道为啥使用apache-dubbo包总是要多导入curator-recipes这个包,这个包大概就是apache官方操作zk使用的包,然后dubbo如果使用zk作为注册中心,就需要引入这个包.
记得引入:

 <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.13.0</version>
        </dependency>

dubbo-admin安装

参考我这篇博客:https://blog.csdn.net/qq_38366063/article/details/93302261
然后访问,就可以看到对应的提供者消费者啦…
在这里插入图片描述

源码github上

其实我喜欢引入springboot阿里巴巴dubbo…配置都差不多,省去一些错误好像.
https://github.com/stackXu/springboot-study

参考

官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
官方demo:https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples
博客:https://www.cnblogs.com/geekdc/p/9267341.html

posted @ 2019-06-20 17:33  你就像甜甜的益达  阅读(99)  评论(0编辑  收藏  举报