Dubbo实战

创建服务接口项目

创建一个名为 TestDubboLogin 的项目,该项目只负责定义接口

POM



    4.0.0

    com.suoron
    MyFirstDubbo
    1.0.0-SNAPSHOT
    
        testdubbo-interface
        testdubbo-client
        testdubbo-server
    
    pom

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
    


创建3个模块(login-server、login-client、login-interface)


定义服务接口 interface
package com.suoron.dubbo.api.service;

/**
 *  用户相关接口
 */
public interface IUserService {
    public String login(String username,String password);
}

创建服务提供者项目

创建一个名为 login-server 的项目,该项目主要用于实现接口

POM



    
        MyFirstDubbo
        com.suoron
        1.0.0-SNAPSHOT
    
    4.0.0

    testdubbo-server

    
        UTF-8
        UTF-8
        1.8
        1.0.0
        1.2.17
    

    
        
        
            io.netty
            netty-all
            4.1.30.Final
        
        
            org.apache.commons
            commons-lang3
            3.8
        
        
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        
        
        
            com.alibaba.boot
            dubbo-spring-boot-actuator
            0.2.0
        
        
        
            com.suoron
            testdubbo-interface
            1.0.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            log4j
            log4j
            ${log4j.version}
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

主要增加了以下依赖:

  • com.alibaba.boot:dubbo-spring-boot-starter:0.2.0:Dubbo Starter,0.2.0 版本支持 Spring Boot 2.x,是一个长期维护的版本。注:0.1.0 版本已经不推荐使用了,是个短期维护的版本,如果你还在用旧版,请大家尽快升级。
  • com.alibaba.boot:dubbo-spring-boot-actuator:0.2.0:Dubbo 的服务状态检查

通过 @Service 注解实现服务提供方

package com.suoron.dubbo.api.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.suoron.dubbo.api.service.IUserService;

@Service(version = "1.0.0")
public class UserServiceImpl implements IUserService {
    @Override
    public String login(String username, String password) {
        return "success";
    }
}

Application

package com.suoron.dubbo.server.app;

import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.suoron.dubbo.api.service")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
        // 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
        Main.main(args);
    }
}

application.yml

spring:
  application:
    name: login-provider

# Dubbo 属性配置
dubbo:
  # 有@Service的包
  scan:
    basePackages: com.suoron.dubbo.api.service
  ## ApplicationConfig Bean dubbo全局唯一应用名(控制台可见)
  application:
    id: login-provider
    name: login-provider
    qosEnable: true
    qosPort: 33333
    qosAcceptForeignIp: false
  ## 服务监听接口21808
  protocol:
    id: dubbo
    name: dubbo
    port: 21808
    status: server
  ## RegistryConfig Bean
  registry:
    id: zookeeper
    #address: zookeeper://127.0.0.1:2181?backup=192.168.10.131:2182,192.168.10.131:2183
    address: zookeeper://192.168.8.105:2181
# Enables Dubbo All Endpoints
management:
  endpoint:
    dubbo:
      enabled: true
    dubbo-shutdown:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-properties:
      enabled: true
  # Dubbo Health
  health:
    dubbo:
      status:
        ## StatusChecker Name defaults (default : "memory", "load" )
        defaults: memory
        ## StatusChecker Name extras (default : empty )
        extras: load,threadpool

logging:
  level:
    root: debug

消费者项目login-client

创建一个名为 login-client 的模块,该项目用于消费接口(调用接口)

POM



    
        MyFirstDubbo
        com.suoron
        1.0.0-SNAPSHOT
    
    4.0.0

    testdubbo-client

    
        UTF-8
        UTF-8
        1.8
        1.0.0
        1.2.17
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        
        
            com.alibaba.boot
            dubbo-spring-boot-actuator
            0.2.0
        
        
            com.suoron
            testdubbo-interface
            1.0.0-SNAPSHOT
        
        
            log4j
            log4j
            ${log4j.version}
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

通过 @Reference 注入 IUserService

package com.suoron.dubbo.client.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.suoron.dubbo.api.service.IUserService;
import com.suoron.dubbo.client.service.UserService;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Reference(version = "1.0.0")
    public IUserService iUserService;

    @Override
    public String userLogin(String user, String password) {
        return iUserService.login(user,password);
    }
}

Application

package com.suoron.dubbo.client.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.suoron.dubbo.client.service")
@ComponentScan("com.suoron.dubbo.client.controller")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

application.yml

# Spring boot application
spring:
  application:
    name: login-consumer
server:
  port: 8080

# Dubbo Config properties
dubbo:
  scan:
    basePackages: com.suoron.client.service
  ## ApplicationConfig Bean
  application:
    id: login-consumer
    name: login-consumer
  ## RegistryConfig Bean
  registry:
    id: zookeeper
    address: zookeeper://192.168.8.105:2181

# Dubbo Endpoint (default status is disable)
endpoints:
  dubbo:
    enabled: true

##健康检查http://127.0.0.1:9091/actuator/health
management:
  server:
    port: 9091
  # Dubbo Health
  health:
    dubbo:
      status:
        ## StatusChecker Name defaults (default : "memory", "load" )
        defaults: memory
  # Enables Dubbo All Endpoints
  endpoint:
    dubbo:
      enabled: true
    dubbo-shutdown:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-properties:
      enabled: true
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"

启动 Dubbo Admin 控制台

查看是否成功注册服务,效果图如下:

Dubbo实战_第1张图片
Dubbo控制台

附: 参考资料

阿里中间件团队博客
Dubbo QOS服务
Spring boot 2.0 Actuator 的健康检查
dubbo 支持的9种协议

你可能感兴趣的:(Dubbo实战)