springboot整合Dubbo+zookeeper远程调用的简单使用

1.zookeeper环境
1.1下载zookeeper,修改conf目录下的zoo_sample.cfg文件名为zoo.cfg
1.2启动即可

2.springboot整合Dubbo
2.1服务提供端
在这里插入图片描述
2.1.1pom文件

<?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>org.example</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2.1.2application.yml文件

server:
  port: 8080

dubbo:
  application:
    name: boot-dubbo-service-provider
  registry:
    address: 127.0.0.1:2181
    protocol: zookeeper
  protocol:
    name: dubbo
    port: 20880

2.1.3主类
加@EnableDubbo

2.1.4service实现
引入的是alibaba的@Service注解

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

@Component
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    public String test() {
        return "666";
    }

}

2.1.5启动

2.2服务消费端
在这里插入图片描述
2.2.1pom文件
与2.1.1的pom文件内容基本相同

2.2.2application.yml文件
与2.1.2的application.yml文件基本相同,修改端口8081

2.2.3主类
加@EnableDubbo

2.2.4service实现

import com.alibaba.dubbo.config.annotation.Reference;
import org.example.interf.DemoService;
import org.example.service.ConsumerService;
import org.springframework.stereotype.Service;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Reference//引用远程提供者服务
    DemoService demoService;

    @Override
    public String consumer() {
        return demoService.test();
    }
}

2.2.5启动测试

2.3注意的问题

2.3.1版本
springboot版本与dubbo版本

2.3.2两个服务的接口的包名类名一致
一般把接口与实体类单独作为一个模块引入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值