码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合dubbo

时间:2019-12-14 15:31:42      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:导入   bin   相同   pen   font   ref   frame   scan   iba   

生产者

  结构

      技术图片

  导入依赖

    <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

标签:导入   bin   相同   pen   font   ref   frame   scan   iba   

原文地址:https://www.cnblogs.com/wnwn/p/12039159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!