美文网首页消息队列rpc
第一个 Dubbo 应用程序

第一个 Dubbo 应用程序

作者: 撸帝 | 来源:发表于2019-02-15 00:45 被阅读65次

    学习完整课程请移步 互联网 Java 全栈工程师

    本节视频

    概述

    创建服务接口项目

    创建一个名为 hello-dubbo-service-user-api 的项目,该项目只负责定义接口

    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.funtl</groupId>
        <artifactId>hello-dubbo-service-user-api</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    </project>
    

    定义服务接口

    package com.funtl.hello.dubbo.service.user.api;
    
    public interface UserService {
        String sayHi();
    }
    

    创建服务提供者项目

    创建一个名为 hello-dubbo-service-user-provider 的项目,该项目主要用于实现接口

    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.funtl</groupId>
        <artifactId>hello-dubbo-service-user-provider</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>hello-dubbo-service-user-provider</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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>
            <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.funtl</groupId>
                <artifactId>hello-dubbo-service-user-api</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.funtl.hello.dubbo.service.user.provider.HelloDubboServiceUserProviderApplication</mainClass>
                    </configuration>
                </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 的服务状态检查
    • com.funtl:hello-dubbo-service-user-api:1.0.0-SNAPSHOT:刚才创建的接口项目,如果无法依赖别忘记先 mvn clean install 到本地仓库。

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

    package com.funtl.hello.dubbo.service.user.provider.api.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.funtl.hello.dubbo.service.user.api.UserService;
    
    @Service(version = "${user.service.version}")
    public class UserServiceImpl implements UserService {
        @Override
        public String sayHi() {
            return "Hello Dubbo";
        }
    }
    

    Application

    package com.funtl.hello.dubbo.service.user.provider;
    
    import com.alibaba.dubbo.container.Main;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloDubboServiceUserProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args);
            // 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
            Main.main(args);
        }
    }
    

    application.yml

    # Spring boot application
    spring:
      application:
        name: hello-dubbo-service-user-provider
    
    # UserService service version
    user:
      service:
        version: 1.0.0
    
    # Dubbo Config properties
    dubbo:
      ## Base packages to scan Dubbo Component:@com.alibaba.dubbo.config.annotation.Service
      scan:
        basePackages: com.funtl.hello.dubbo.service.user.provider.api
      ## ApplicationConfig Bean
      application:
        id: hello-dubbo-service-user-provider
        name: hello-dubbo-service-user-provider
        qos-port: 22222
        qos-enable: true
      ## ProtocolConfig Bean
      protocol:
        id: dubbo
        name: dubbo
        port: 12345
        status: server
      ## RegistryConfig Bean
      registry:
        id: zookeeper
        address: zookeeper://192.168.10.131:2181?backup=192.168.10.131:2182,192.168.10.131:2183
    
    # 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
    

    创建服务消费者项目

    创建一个名为 hello-dubbo-service-user-consumer 的项目,该项目用于消费接口(调用接口)

    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.funtl</groupId>
        <artifactId>hello-dubbo-service-user-consumer</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>hello-dubbo-service-user-consumer</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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.funtl</groupId>
                <artifactId>hello-dubbo-service-user-api</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.funtl.hello.dubbo.service.user.consumer.HelloDubboServiceUserConsumerApplication</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    通过 @Reference 注入 UserService

    package com.funtl.hello.dubbo.service.user.consumer.controller;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.funtl.hello.dubbo.service.user.api.UserService;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
    
        @Reference(version = "${user.service.version}")
        private UserService userService;
    
        @RequestMapping(value = "hi")
        public String sayHi() {
            return userService.sayHi();
        }
    }
    

    Application

    package com.funtl.hello.dubbo.service.user.consumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloDubboServiceUserConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloDubboServiceUserConsumerApplication.class, args);
        }
    }
    

    application.yml

    # Spring boot application
    spring:
      application:
        name: hello-dubbo-service-user-consumer
    server:
      port: 9090
    
    # UserService service version
    user:
      service:
        version: 1.0.0
    
    # Dubbo Config properties
    dubbo:
      scan:
        basePackages: com.funtl.hello.dubbo.service.user.consumer.controller
      ## ApplicationConfig Bean
      application:
        id: hello-dubbo-service-user-consumer
        name: hello-dubbo-service-user-consumer
      ## RegistryConfig Bean
      registry:
        id: zookeeper
        address: zookeeper://192.168.10.131:2181?backup=192.168.10.131:2182,192.168.10.131:2183
    
    # Dubbo Endpoint (default status is disable)
    endpoints:
      dubbo:
        enabled: true
    
    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
      endpoints:
        web:
          exposure:
            include: "*"
    

    启动 Dubbo Admin 控制台

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

    相关文章

      网友评论

        本文标题:第一个 Dubbo 应用程序

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