Dubbo 基础篇 (一) - hello world

开发环境
  • eclipse 4.7.3a
  • jdk 8
  • zookeeper-3.5.4-beta
  • maven 3.5.2
Dubbo 架构
  • 服务提供方
  • 服务消费方
  • 注册中心 (使用zookeeper作为本例注册中心)
  • 监控中心
准备工作

为了更好的理解Dubbo的工作流程,我们先下载dubbo源码和管理后台源码

# 下载dubbo源码
git clone [email protected]:freeseawind/incubator-dubbo.git

# 下载管理后台源码
git clone https://github.com/dubbo/dubbo-ops.git 

使用以下命令分别做一次构建,然后我们可以和好基友happy的去玩两把游戏等待编译结束

mvn clean install -Dmaven.test.skip

N分钟后,自从Dubbo “高富帅“ 以后,这个构建流程就越来越麻麻dei(慢)了。好吧,这咱都能忍,但是what竟然编译失败,这简直不能忍。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
6.0:testCompile (default-testCompile) on project dubbo-config-spring: Compilatio
n failure -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command

[ERROR]   mvn  -rf :dubbo-config-spring

嗯、啊、唉,研究了半天还是无果,决定先跳过dubbo-config-spring的编译,如果有知道的朋友欢迎告知本菜 (T T)。

编码时刻

我们使用spring boot 2.x + dubbo 来作为本次学习的例子

使用xml配置方式见官方DEMO, 这里演示如何在spring boot 2中运行Dubbo 的 hello world

1. 创建Maven项目
├── copycat-dubbo
│   ├── pom.xml
│   └── copycat-dubbo-example
│       └── src/main/java
│       └── src/main/resource
│           └── dubbo.properties
│           └── log4j.properties
│           └── application-customer.properties
│           └── application.properties
│       └──  pom.xml

copycat-dubbo项目pom文件部分配置如下:

  
    
        
            
                
                org.springframework.boot
                spring-boot-dependencies
                2.0.2.RELEASE
                pom
                import
            
            
                com.alibaba
                dubbo
                2.6.2
            
                        
            
                org.apache.curator
                curator-client
                4.0.1
            
            
                org.apache.curator
                curator-framework
                4.0.1
            
        
    

copycat-dubbo-example项目pom文件部分配置如下:

    
        
            com.alibaba
            dubbo
        
        
            org.apache.curator
            curator-client
        
        
            org.apache.curator
            curator-framework
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
2. 定义服务接口和实现
定义服务接口
public interface DemoService
{
    String sayHello(String name);
}
定义接口实现
@Service(timeout = 5000)
public class DemoServiceImpl implements DemoService
{
    @Override
    public String sayHello(String name)
    {
        return "Hello " + name;
    }
}

细心的朋友会发现在接口的实现上,增加了@Service(timeout = 5000)的注解,这个注解的主要作用是暴露和注册服务。

3. 服务消费者
服务消费者配置
@Configuration
public class ConsumerConf
{
    @Bean
    public ApplicationConfig applicationConfig()
    {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer-test");
        return applicationConfig;
    }

    /**
     * 服务消费者配置
     * 
     * @author freeseawind
     * @return
     */
    @Bean
    public ConsumerConfig consumerConfig()
    {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

    /**
     * 注册中心配置
     * 
     * @author freeseawind
     * @return
     */
    @Bean
    public RegistryConfig registryConfig()
    {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

这里的配置信息只是把xml替换成API的方式,通过Spring的IOC容器对Bean进行管理。详见API 参考手册 和schema 配置参考手册

消费者启动类
@SpringBootApplication
@Controller
@DubboComponentScan(basePackages = "github.freeseawind.springboot.helloworld.consumer")
public class ConsumerApplication
{
    @Reference
    private DemoService demoService;
    
    public static void main(String[] args)
    {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    
    @RequestMapping("/a")
    @ResponseBody
    public String sayHello()
    {
        return demoService.sayHello("freeseawind");
    }
}

@DubboComponentScan 是Dubbo自定义bean加载的扩展。它在消费方主要的作用是,扫描需要注入的服务接口(该服务接口以@Reference注解标识),同时向注册中心发起注册消费者服务请求。

3. 服务提供者
服务提供者配置
@Configuration
public class ProviderConf
{
    @Bean
    public ApplicationConfig applicationConfig()
    {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig()
    {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
    
    @Bean
    public ProtocolConfig protocolConfig()
    {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(20881);
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }
}
服务提供者启动类
@SpringBootApplication
@EnableAutoConfiguration
@DubboComponentScan(basePackages = "github.freeseawind.service.impl")
public class ProviderApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

@DubboComponentScan 扫描的是服务接口的实现(该实现以@Service注解标识),同时向注册中心发起注册服务提供者请求。

顺序启动服务
  • 注册中心
  • 服务提供者
  • 服务消费者
  • 管理后台
1. 启动注册中心

2. 启动服务提供者

截取部分启动日志如下

[DUBBO] Export dubbo service github.freeseawind.service.DemoService to url dubbo://169.254.63.31:20881/
[DUBBO] Start NettyServer bind /0.0.0.0:20881, export /169.254.63.31:20881
[DUBBO] Subscribe: provider://169.254.63.31:20881/github.freeseawind.service.DemoService?
3. 启动服务消费者

配置启动参数


Dubbo 基础篇 (一) - hello world_第1张图片
启动参数配置

截取部分启动日志如下

[DUBBO] Subscribe: consumer://169.254.63.31/github.freeseawind.service.DemoService?
[DUBBO] Successed connect to server /169.254.63.31:20881 from NettyClient 169.254.63.31
[DUBBO] Start NettyClient xiaolong-PC/169.254.63.31 connect to the server /169.254.63.31:20881

从启动日志中我们可以看到消费者和服务提供者建立了连接

4. 启动管理后台
Dubbo 基础篇 (一) - hello world_第2张图片
管理后台工程结构

访问 http://localhost:7001/
用户名:root 密码:root

相关配置在application.properties中可找到

我们可以看到服务提供者和消费者均已经注册成功,并可在管理后台查看

服务提供者
Dubbo 基础篇 (一) - hello world_第3张图片
服务消费者
验证
请求服务

下一篇:Dubbo 基础篇 - 配置和目录

Github工程地址

你可能感兴趣的:(Dubbo 基础篇 (一) - hello world)