SpringBoot dubbo

dubbo git官网

https://github.com/apache/dubbo

 

需要注意的是,根据jdk和Spring Boot版本的不同,dubbo-spring-boot-starter的版本需要有根据的选择

 

docker启动zookeeper

docker的zookeeper默认使用 2181 2888 3888这三个端口

docker pull zookeeper:3.4.14

docker run --name myzookeeper -p 2181:2181 -p 20800:20800 --restart always -d zookeeper:3.4.14

2181为java和zookeeper获取服务地址接口.

 

SpringbootDubboExample项目

内含两个模块

springboot模块 dubbo-provider

springboot模块 dubbo-consumer

 

springboot模块 dubbo-provider

pom.xml依赖

        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

 

application.properties

server.port=8080

#当前服务/应用的名字
dubbo.application.name=dubbo-provider

#注册中心的协议和地址
#dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://centos:2181

#连接监控中心
#dubbo.monitor.protocol=registry

#开启包扫描,可替代 @EnableDubbo 注解
dubbo.scan.base-packages=com.example.dubbo.provider.service

 

DubboProviderApplication.java启动入口类

package com.example.dubbo.provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 开启基于注解的dubbo功能(主要是包扫描@DubboComponentScan)
// 也可以在配置文件中使用dubbo.scan.base-package来替代@EnableDubbo
//@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }

}

 

DubboProviderService.java接口类

package com.example.dubbo.provider.service;

/**
 * 服务端相同的接口名
 */
public interface DubboProviderService {
    public String sayHello(String name);
}

 

DubboProviderServiceImpl.java 接口实现类

package com.example.dubbo.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * 注意dubbo的服务提供者使用的Service类的包名,不是springboot提供的,
 * 而@Component是springboot提供的,为了将dubbo的bean组装到容器中
 */
@Component
@com.alibaba.dubbo.config.annotation.Service(timeout = 3000)
public class DubboProviderServiceImpl implements DubboProviderService {
    public String sayHello(String name) {
        String retData = "hello " + name;
        System.out.println(retData);
        return retData;
    }
}

 

 

 

 

springboot模块 dubbo-consumer

pom.xml依赖

        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

 

application.properties

#避免和监控中心端口冲突,设为8081端口访问
server.port=8081

#当前服务/应用的名字
dubbo.application.name=dubbo-consumer

#注册中心的协议和地址
#dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://centos:2181
#check为false意为,服务提供者还没有启动的时候,服务消费者会在对象初始化的时候,创建相应的代理对象,不会产生一个null对象
dubbo.consumer.check=false

 

DubboProviderService.java接口

直接复制provider模块的java代码到consumer模块中, 以后需要使用maven依赖

package com.example.dubbo.provider.service;

/**
 * 服务端相同的接口名
 */
public interface DubboProviderService {
    public String sayHello(String name);
}

 

DubboConsumerApplication.java 入口类

package com.example.dubbo.consumer;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


//@EnableDubbo
//@DubboComponentScan(value = "com")
@SpringBootApplication
public class DubboConsumerApplication {

    public static void main(String[] args) {//http://localhost:8081/consumer?name=bobo
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

}

 

DubboConsumerService.java

package com.example.dubbo.consumer.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.dubbo.provider.service.DubboProviderService;
import org.springframework.stereotype.Service;

@Service
public class DubboConsumerService {

    // 千万不要错引成 jdk.nashorn.internal.ir.annotations.Reference; 当初被这个类坑了2小时,导致该dubboProviderService一直是null
    @com.alibaba.dubbo.config.annotation.Reference(lazy = true)
    DubboProviderService dubboProviderService;

    public String sayHello(String name) {
        String retData = dubboProviderService.sayHello(name);
        return retData;
    }
}

 

DubboConsumerController .java

controller入口类,使用http://localhost:8081/consumer?name=bobo 访问

package com.example.dubbo.consumer.controller;

import com.example.dubbo.consumer.service.DubboConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DubboConsumerController {

    @Autowired
    DubboConsumerService dubboConsumerService;

    //http://localhost:8081/consumer?name=bobo
    @ResponseBody   //以json格式返回
    @RequestMapping("/consumer")
    public String sayHello(String name){
        String retData = dubboConsumerService.sayHello(name);
        System.err.println(retData);
        return retData;
    }
}

 

 

异常参考

spring-boot框架dubbo注解@Reference注入service,但是调用方法时候,service报null空指针异常...==>https://blog.csdn.net/weixin_34413065/article/details/88175876

 

我的git项目地址

https://gitee.com/KingBoBo/springboot-06-dubbo

posted @ 2019-05-20 14:47  苦涩泪滴  阅读(311)  评论(0编辑  收藏  举报