springboot整合dubbo

生产者

  结构

      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");
    }

}

消费者

  结构

      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=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

相关推荐