dubbo入门使用第三篇 -- 整合springboot

一、前置准备:可参考前两篇文章

zookeeper搭建并启动

dubbo控制台启动

dubbo监控启动(可选)

三方包依赖(本项目使用到)

二、代码编写 

提供者

1. 项目结构:红色框框重点

2.代码和配置

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-springboot-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!--三方工具-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

application.properties

server.port=8081
dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://localhost:2181

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo.monitor.protocol=registry

启动类添加@EnableDubbo注解

controller

package com.example.dubbospringbootprovider.controller;

import com.example.dubboapi.service.HelloService;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam("name") String name){
        return helloService.sayHello(name);
    }

}

service

package com.example.dubbospringbootprovider.rpc;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubboapi.service.HelloService;
import org.springframework.stereotype.Component;

@Service
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "provider send :hello "+name;
    }
}

消费者

1. 项目结构

2.编写和配置

pom.xml 和提供者一样

application.properties

server.port=8082
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://localhost:2181

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo.monitor.protocol=registry

controller

package com.example.dubbospringbootconsumer.controller;

import com.example.dubbospringbootconsumer.service.impl.ConsumerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @Autowired
    private ConsumerServiceImpl consumerService;

    @ResponseBody
    @RequestMapping("/hello")
    public String sayHello(@RequestParam("name") String name){
        return consumerService.sayHello(name);
    }
}

service

package com.example.dubbospringbootconsumer.service;

public interface ConsumerService {
    String sayHello(String name);
}
package com.example.dubbospringbootconsumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.dubboapi.service.HelloService;
import com.example.dubbospringbootconsumer.service.ConsumerService;
import org.springframework.stereotype.Service;

@Service
public class ConsumerServiceImpl implements ConsumerService{

    @Reference
    private HelloService helloService;

    @Override
    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

此外主启动类不要忘记添加@EnableDubbo注解

三、结果

打开控制中心

 访问:http://localhost:8082/hello?name=菜鸟

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值