springboot整合dubbo

Posted wn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合dubbo相关的知识,希望对你有一定的参考价值。

生产者

  结构

      

  导入依赖

    <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

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

  application.properties文件

spring.dubbo.application.name=prodvice_dubbo01
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20881

spring.dubbo.scan=com.zk.boot_dubbo01.service

server.port=8083

  service接口

public interface BootService {

    public String getOne();

}

  service接口实现

package com.zk.boot_dubbo01.service;

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

@Service(interfaceClass = BootService.class)
@Component
public class BootServiceImpl implements BootService 

    @Override
    public String getOne() {
        return "到了---";
    }
}

  启动类

package com.zk.boot_dubbo01;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class BootDubbo01Application {

    public static void main(String[] args) {
        SpringApplication.run(BootDubbo01Application.class, args);
        System.out.println("dfghjkjhe");
    }

}

消费者

  结构

      

  导入依赖

    <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

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

  application.properties文件

spring.dubbo.application.name=consumer_dubbo01
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.zk.boot_dubbo02.controller

server.port=8084

  service接口(与生产者相同)

public interface BootService {
    public String getOne();
}

  controller层

package com.zk.boot_dubbo01.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zk.boot_dubbo01.service.BootService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    private BootService service;

    @RequestMapping("/getOne")
    @ResponseBody
    public String   getOne(){
        String user = service.getOne();
        return user;
    }


}

  启动类

package com.zk.boot_dubbo01;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class ApplicationStart {

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

结果

  消费者能调用生产者里面的方法

  

以上是关于springboot整合dubbo的主要内容,如果未能解决你的问题,请参考以下文章

dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发

dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发

Springboot 整合 Dubbo/ZooKeeper

dubbo入门学习-----dubbo整合springboot

springboot整合dubbo

SpringBoot与Dubbo整合-项目搭建