Home

Awesome

spring-boot-starter-dubbo

spring-boot-start-dubbo,让你可以使用spring-boot的方式开发dubbo程序。使dubbo开发变得如此简单。

让你可以使用spring-boot的方式开发dubbo程序。使dubbo开发变得如此简单。

如何使用

1. clone代码(可选,已经发布到中央仓库,可以直接依赖中央仓库的稳定版本

git clone git@github.com:teaey/spring-boot-starter-dubbo.git

2. 编译安装(可选)

cd spring-boot-starter-dubbo
mvn clean install

3. 修改maven配置文件(可以参考样例spring-boot-starter-dubbo-sample

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

根据实际情况依赖最新版本

 <dependency>
    <groupId>io.dubbo.springboot</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
    <version>1.0.0</version>
 </dependency>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.3.6.RELEASE</version>
</plugin>

4. 发布服务

服务接口:

package cn.teaey.sprintboot.test;

public interface EchoService {
    String echo(String str);
}

application.properties添加Dubbo的版本信息和客户端超时信息,如下:

spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://192.168.99.100:32770
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=cn.teaey.sprintboot.test

Spring Applicationapplication.properties中添加spring.dubbo.scan即可支持Dubbo服务发布,其中scan表示要扫描的package目录。

package cn.teaey.sprintboot.test;

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

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

package cn.teaey.sprintboot.test;

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

@Service(version = "1.0.0")
public class EchoServerImpl implements EchoService {

    public String echo(String str) {
        System.out.println(str);
        return str;
    }
}

5. 消费Dubbo服务

spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://192.168.99.100:32770
spring.dubbo.scan=cn.teaey.sprintboot.test

Spring Applicationapplication.properties中添加spring.dubbo.scan即可支持Dubbo服务发布,其中scan表示要扫描的package目录。

package cn.teaey.sprintboot.test;

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

@SpringBootApplication
public class Client {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Client.class, args);
        AbcService bean = run.getBean(AbcService.class);
        System.out.println(bean.echoService.echo("abccc"));
    }
}

package cn.teaey.sprintboot.test;

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

@Component
public class AbcService {
    @Reference(version = "1.0.0")
    public EchoService echoService;
}

6. monitor监控中心

spring.dubbo.monitor.protocol=registry

7. 打包