[case31]dubbo-spring-boot-starter小试牛刀

codecraft

本文主要展示一下dubbo-spring-boot-starter的使用。

maven

        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>        

service-impl

application.yml

spring:
  application:
    name: service-impl
  dubbo:
    server: true
    application:
      name: service-impl
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      port: 20880
    scan:
      basePackages: com.example

EnableDubboConfiguration

@SpringBootApplication
@EnableDubboConfiguration
public class ServiceImplApplication {

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

EchoServiceImpl

@Service(interfaceClass = EchoService.class)
@Component
public class EchoServiceImpl implements EchoService {
    @Override
    public String echo(String content) {
        return "hello:" + Objects.toString(content,"null");
    }
}

consumer

application.yml

spring:
  application:
    name: consumer-demo
dubbo:
  application:
    name: consumer-demo
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
  scan:
    basePackages: com.example

EnableDubbo

@SpringBootApplication
@EnableDubbo
public class ConsumerDemoApplication implements CommandLineRunner {

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


    @Autowired
    ConsumerService consumerService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(consumerService.echo("world"));
    }
}

Reference

@Component
public class ConsumerService {

    @Reference
    EchoService echoService;

    public String echo(String content){
        return echoService.echo(content);
    }
}

小结

dubbo-spring-boot-starter的官方文档貌似比较粗糙,比较不符合spring boot开源项目的风格,也没有看到example工程,实践起来,稍稍费劲一点。

doc

阅读 1.9k

code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.6k 声望
1.9k 粉丝
0 条评论
你知道吗?

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.6k 声望
1.9k 粉丝
文章目录
宣传栏