Dubbo的Api+Provider+Customer示例(IDEA+Maven+Springboot+dubbo)


項目結構

dubbo-demo

  dubbo-api:提供api接口,一般存儲實體類和接口服務

  dubbo-provider:dubbo生產者提供服務,一般存儲接口具體實現

  dubbo-customer:dubbo消費者使用服務,分發請求

一、新建項目dubbo-demo

1、新建Maven項目,File-->new-->Project,選擇Maven

2、填充GroupId、ArtifactId和Version 

3、一路Next,最后選擇項目名稱(之前創建過dubbo的項目,這里重命名為dubbo_demo)

4、生成項目,修改pom.xml(不修改也不影響子模塊),刪除src

 

 二、添加基於SpringBoot的三個子模塊dubbo-api、dubbo-provider、dubbo-customer

1、添加子模塊dubbo-api,項目右鍵-->new-->Module

 

 

 

 

 最后生成的dubbo-api 如下:

 

2、以相同的方式創建dubbo-provider和dubbo-customer如下:

 

 

三、各個模塊添加代碼示例

1、dubbo-api

 

phone(手機實體類):

package com.mirana.api.model;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Title:手機 實體類
 *
 * @CreatedBy Mirana
 * @DateTime 2018/5/25 8:43
 */
public class Phone implements Serializable {

    // 主鍵id
    private Long       id;
    // 手機名稱
    private String     name;
    // 價格
    private BigDecimal price;

    public Long getId () {
        return id;
    }

    public void setId (Long id) {
        this.id = id;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public BigDecimal getPrice () {
        return price;
    }

    public void setPrice (BigDecimal price) {
        this.price = price;
    }

}

 IPhoneService手機接口

package com.mirana.api.service;

import com.mirana.api.model.Phone;

/**
 * Title:手機服務接口
 *
 * @CreatedBy Mirana
 * @DateTime 2018/5/25 8:45
 */
public interface IPhoneService {

    /**
     * 根據id查找手機
     *
     * @param id
     * @return
     */
    Phone findById (Long id);
}

在application.properties中定義端口

server.port=9011

 

2、dubbo-provider

 

 pom.xml添加dubbo-api(具體實現類要實現dubbo-api的IPhoneService接口)和Apache Dubbo的Maven依賴,紅色部分為新增依賴

<?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.mirana.provider</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>v1.1</version>
    <packaging>jar</packaging>

    <name>dubbo-provider</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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-test</artifactId>
            <scope>test</scope>
        </dependency>

     <!-- 新增dubbo-api依賴 --> <dependency> <groupId>com.mirana.api</groupId> <artifactId>dubbo-api</artifactId> <version>v1.1</version> </dependency>

     <!-- 新增dubbo依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>

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

 添加實現類PhoneServiceImpl,@service注解是dubbo的注解,而非Spring的注解

package com.mirana.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.mirana.api.model.Phone;
import com.mirana.api.service.IPhoneService;

import java.math.BigDecimal;

/**
 * Title:手機服務service
 *
 * @CreatedBy Mirana
 * @DateTime 2018/5/25 10:32
 */
@Service(version = "1.0.0")
public class PhoneServiceImpl implements IPhoneService {

    /**
     * 根據id查找手機
     *
     * @param id
     * @return
     */
    @Override
    public Phone findById (Long id) {
        Phone phone = new Phone();
        phone.setId(id);
        phone.setName("iphonex");
        phone.setPrice(BigDecimal.valueOf(8848.88));
        return phone;
    }
}

新增dubbo服務提供者的配置文件:dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--定義了提供方應用信息,用於計算依賴關系;在 dubbo-admin 或 dubbo-monitor 會顯示這個名字,方便辨識-->
    <dubbo:application name="demo-provider" owner="demo_mirana" organization="dubbo_mirana"/>
    <!--使用 zookeeper 注冊中心暴露服務,注意要先開啟 zookeeper-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--使用 dubbo 協議實現定義好的 Service Api 接口-->
    <dubbo:service interface="com.mirana.api.service.IPhoneService" ref="phoneService" protocol="dubbo"/>
    <!--具體實現該接口的 bean-->
    <bean id="phoneService" class="com.mirana.provider.service.PhoneServiceImpl"/>
</beans>

在SpringBoot的啟動類DubboProviderApplication上導入dubbo-provider.xml的配置

package com.mirana.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:dubbo-provider.xml"})
public class DubboProviderApplication {

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

在application.properties中定義端口

server.port=9012

3、dubbo-customer

1、pom.xml,與provider相似,添加dubbo-api和dubbo的依賴(紅色部分)

<?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.mirana.customer</groupId>
    <artifactId>dubbo-customer</artifactId>
    <version>v1.1</version>
    <packaging>jar</packaging>

    <name>dubbo-customer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 新增dubbo-api -->
        <dependency>
            <groupId>com.mirana.api</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>v1.1</version>
        </dependency>

        <!-- 新增dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

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

2、添加視圖控制器PhoneController

package com.mirana.customer.controller;

import com.mirana.api.model.Phone;
import com.mirana.api.service.IPhoneService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Title:TODO
 *
 * @CreatedBy Mirana
 * @DateTime 2018/5/25 10:59
 */
@RestController
@RequestMapping("/phone")
public class PhoneController {

    @Resource
    private IPhoneService phoneService;

    /**
     * @return
     */
    @RequestMapping("/findById")
    public Phone findById (Long id) {
        return phoneService.findById(id);
    }

}

3、添加dubbo服務消費者的配置文件dubbo-customer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer" owner="mirana" organization="dubbo_mirana"/>
    <!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時推送-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper"/>
    <!--使用 dubbo 協議調用定義好的 api 接口-->
    <dubbo:reference id="phoneService" interface="com.mirana.api.service.IPhoneService"/>

</beans>

4、在SpringBoot的啟動類DubboCustomerApplication上導入dubbo-customer.xml的配置

package com.mirana.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication @ImportResource(value = {"classpath:dubbo-customer.xml"})
public class DubboCustomerApplication {

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

在application.properties中定義端口

server.port=9013

三、請求服務消費者的地址

  查找id為3的手機:http://localhost:9013/phone/findById?id=3,返回的json數據如圖

 

三、運行程序,查看dubbo-admin

1、先運行dubbo-api

  運行DubboApiApplication的main( )方法

2、再運行dubbo-provider

  運行DubboProviderApplication的main( )方法

3、最后運行dubbo-customer

  運行DubboCustomerApplication的main( )方法

4、查看dubbo-admin的服務

  http://localhost:8080/dubbo/,輸入用戶名root密碼root

4.1提供的服務

 4.2 服務提供者

 

 4.3 服務消費者

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM