1. 程式人生 > >springboot+dubbo 入門案例

springboot+dubbo 入門案例

   1  入門案例

     父工程的xml檔案

<?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.czxy</groupId>
    <artifactId>dubbodemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>dubboapi</module>
        <module>dubbocustomer</module>
        <module>dubboprivide</module>
    </modules>
    <packaging>pom</packaging>

    <name>dubbodemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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>


        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2  privider xml

<?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.czxy</groupId>
    <artifactId>dubboprivide</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dubboprivide</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <artifactId>dubbodemo</artifactId>
        <groupId>com.czxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </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>com.czxy</groupId>
            <artifactId>dubboapi</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

      2.1  properties配置檔案的編寫


server.port = 9090

spring.application.name = dubbo-provider
#指定當前服務/應用的名字(同樣的服務名字相同,不要和別的服務同名)
dubbo.application.name = dubbo-provider
#用dubbo協議在20880埠暴露服務
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
#指定註冊中心的位置
dubbo.registry.address = zookeeper://localhost:2181
#統一設定服務提供方的規則
dubbo.provider.timeout = 1000

   2.2  實現介面的類

package com.czxy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.czxy.api.DemoService;


/**
 * @author zhenglong
 * @Description: DemoService的實現類
 * @Date 2018/11/20 21:22
 * com.alibaba.dubbo.config.annotation.Service 不是spring的service
 */
@Service
public class DemoServiceImpl implements DemoService {


    @Override
    public String helloWorld(String name) {
        return null;
    }
}

2.3 privider啟動器

package com.czxy;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author zhenglong
 * @Date 2018/11/20 21:22
 * 開啟dubbo 相當於@DubboComponentScan與@EnableDubboConfig結合
 */
@EnableDubbo
@SpringBootApplication
public class DubboprivideApplication {

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

3 公共的介面類

package com.czxy.api;

/**
 * @author zhenglong
 * @Description: 服務demo介面類
 * @Date 2018/11/27 21:22
 */
public interface DemoService {
    /**
     * @param name
     * @return
     */
    String helloWorld(String name);
}

3 customer 消費端的pom.xml

<?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.czxy</groupId>
    <artifactId>dubbocustomer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dubbocustomer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <artifactId>dubbodemo</artifactId>
        <groupId>com.czxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </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>com.czxy</groupId>
            <artifactId>dubboapi</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

3.1  customer 消費端的controller

package com.czxy.conroller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.czxy.api.DemoService;
import com.czxy.api.UserService;
import com.czxy.api.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhenglong
 * @Description: 消費端controller
 * @Date 2018/11/20 21:22
 */
@RestController
public class DemoConsumerController {
    /**
     * @Reference來注入服務端爆露的介面
     */
    @Reference
    private DemoService demoService;
 
    /**
     * @Description:demoService 介面呼叫的方法
     */
    @RequestMapping("/HelloWorld")
    public String sayHello( String name) {

        return demoService.helloWorld(name);
    }


}

3.2 customer消費端的啟動器   

package com.czxy;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @author zhenglong
 * @Date 2018/11/20 21:22
 * 開啟dubbo 相當於@DubboComponentScan與@EnableDubboConfig結合
 */
@SpringBootApplication
@EnableDubbo
public class DubbocustomerApplication {

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

 4 搭建以後的maven專案截圖如下