Spring Boot配置Dubbo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bandaotixiruiqiang/article/details/72652033

首先理解一下下面依赖
配置1.

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

这个配置依赖三个maven依赖
配置2.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
</dependency>

java版本要求1.6+
我们只需要添加配置1就可以了,然后添加zk的配置
服务端配置(生产者)

spring:
  dubbo:
    application:
      name: provider
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      prot: 20880
    scan: store.bymin.user

客户端配置(消费者)

spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: zookeeper://127.0.0.1:2181
    scan: store.bymin.backend
  redis:
    host: localhost

配置完成后我们就可以使用了
生产者提供方法
在实现类上添加dubbo包中的Service(它也会把实现类送给spring管理)

import com.alibaba.dubbo.config.annotation.Service;
@Service(version="1.0.0")
public class DictServiceImpl implements DictService {
    @Override
    public void test() {
        System.out.println("test-------------");
    }
}

需要注意的是;这个类必须放到dubbo扫描的包中(在配置中写的scan配置)

消费者使用方法
需要使用dubbo包中的Reference进行自动注入

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import store.bymin.common.data.ApplicationResult;
import store.bymin.user.service.DictService;

@RestController
@RequestMapping("dict")
public class DictController {
    @Reference(version = "1.0.0")
    DictService dictService;
    @RequestMapping("getDict")
    public ApplicationResult printCity() {
        dictService.test();
        return ApplicationResult.SUCCESS;
    }
}

这个类也是必须要被dubbo扫描到的
配置完成后就可以启动测试了

再说一下:必须去掉springboot的自动部署插件,不然一直不成功,还不知道为什么

如果有问题或者BUG请发邮件到:[email protected]
感谢

猜你喜欢

转载自blog.csdn.net/bandaotixiruiqiang/article/details/72652033