idea搭建dubbo项目(服务提供者+服务消费者)

idea搭建dubbo项目(服务提供者+服务消费者)

下一个项目要用到dubbo,自己之前没有用过这个框架,所以提前学习搭建一下,在这些个博客是为了以后学习改进之用。
啥也不说,先启动zookeerer,不会的或者没有安装的先看看别的教程哦。。。
dubbo官方文档:http://dubbo.apache.org/en-us/

在这里插入图片描述
看看官方的dubbo架构:
在这里插入图片描述
在这里插入图片描述
项目结构:
在这里插入图片描述
api存放一些共工模块的接口,pojo等等,可以建一个maven项目,或者都建为spingboot项目,我这里都是统一建了springboot项目。
右击项目名,module
在这里插入图片描述
在api里面写一个接口:

在这里插入图片描述

public interface TestService {

    String testDubbo();
}

再来写一个服务提供者,创建项目跟上面一样,也是创建一个springboot项目:
导入相关依赖(要将api模块添加进去):

<dependency>
            <groupId>com.study</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
       </dependency>

        <!-- 整合dubbo -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- zookeeper客户端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>

yml文件:

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: 127.0.0.1:2181
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 20888
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000
  scan: com.study.api.service.impl #扫描的包名

server:
  port: 8091

实现api里面的接口:
注意这里面的service注解不是spring里面那个service注解了,
而是import com.alibaba.dubbo.config.annotation.Service;

@Service
public class TestServiceImpl implements TestService {

    @Override
    public String testDubbo() {
        return "hello world";
    }
}

启动类(添加了新注解):

@EnableDubbo
@SpringBootApplication
public class ProviderApplication {

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

}

接下来我们创建一个消费者,跟创建服务提供者一样:

pom文件:

 <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.study</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

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

yml:
注意:我这边端口号加了" “,这边我感觉是springboot某个版本的一个坑在这,如果不添加” ",则一直报错。。。

dubbo:
  application:
    name: dubbo-consumer
  registry:
    address: 127.0.0.1:2181
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: "20888"
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000
server:
  port: 8092

在这里插入图片描述

public interface ProductService {

    String test();
}

注意注解哦

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.study.api.service.TestService;
import com.study.consumer.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

    @Reference
    private TestService testService;

    @Override
    public String test() {
        return testService.testDubbo();
    }
}
import com.study.consumer.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test01(){
        return productService.test();
    }
}
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {

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

}

启动服务提供者和消费者:
访问:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值