首页 > 代码库 > Spring Boot和Dubbo整合

Spring Boot和Dubbo整合

创建3个项目

dubbo-api

新建一个maven项目,这个项目只有接口(或实体类)

dubbo-service和dubbo-web这两个依赖于dubbo-api

<groupId>com.zhang</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0</version>

 

package com.zhang.service;

public interface UserService {

    public String login(String username);
}

 

dubbo-service

 dubbo服务provider

<groupId>com.zhang</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

 Maven依赖

<dependencies>
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.zhang</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

 service实现类,用@Service注解

package com.zhang.service;

import com.alibaba.dubbo.config.annotation.Service;

@Service(version="1.0.0")
public class UserServiceImpl implements UserService{

    @Override
    public String login(String username) {
        System.out.println("username:" + username);
        return "my name is " + username;
    }
    
}

application.properties配置,spring.dubbo.scan=服务的package

spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.zhang.service

dubbo-web

dubbo服务consumer

<groupId>com.zhang</groupId>
<artifactId>dubbo-web</artifactId>
<version>1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

 

maven依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.zhang</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

新建一个Controller类

package com.zhang.service;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;

@Controller
public class Person {

    @Reference(version = "1.0.0")
    private UserService userService;
    
    @RequestMapping("/login")
    @ResponseBody
    public String run() {
        return userService.login("abcd");
    }
}

springboot入口类

package com.zhang.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

application.properties

spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.zhang.service

启动顺序和测试

1、先自动zookeeper

2、启动dubbo-service

3、启动dubbo-web

测试方式,直接在浏览器地址里输入http://localhost:8080/login

Spring Boot和Dubbo整合