美文网首页
Dubbo实战

Dubbo实战

作者: 索伦x | 来源:发表于2019-03-10 00:04 被阅读0次

    创建服务接口项目

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

    POM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.suoron</groupId>
        <artifactId>MyFirstDubbo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <modules>
            <module>testdubbo-interface</module>
            <module>testdubbo-client</module>
            <module>testdubbo-server</module>
        </modules>
        <packaging>pom</packaging>
    
        <!-- 继承父包 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.2.RELEASE</version>
        </parent>
    
    </project>
    

    创建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

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>MyFirstDubbo</artifactId>
            <groupId>com.suoron</groupId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>testdubbo-server</artifactId>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
            <log4j.version>1.2.17</log4j.version>
        </properties>
    
        <dependencies>
            <!-- qos server依赖 -->
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.30.Final</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.8</version>
            </dependency>
            <!-- 非WEB启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- 健康检查、审计、统计和监控 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- dubbo启动器 -->
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
            </dependency>
            <!-- dubbo健康检查 -->
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-actuator</artifactId>
                <version>0.2.0</version>
            </dependency>
            <!-- 接口jar -->
            <dependency>
                <groupId>com.suoron</groupId>
                <artifactId>testdubbo-interface</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    主要增加了以下依赖:

    • 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

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>MyFirstDubbo</artifactId>
            <groupId>com.suoron</groupId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>testdubbo-client</artifactId>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
            <log4j.version>1.2.17</log4j.version>
        </properties>
    
        <dependencies>
            <!-- web启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- 健康检查 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-actuator</artifactId>
                <version>0.2.0</version>
            </dependency>
            <dependency>
                <groupId>com.suoron</groupId>
                <artifactId>testdubbo-interface</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    通过 @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控制台

    附: 参考资料

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

    相关文章

      网友评论

          本文标题:Dubbo实战

          本文链接:https://www.haomeiwen.com/subject/yylspqtx.html