idea dubbo-helloworld

假如某個電商系統(tǒng),訂單服務需要調用用戶服務獲取某個用戶的所有地址,

所以現(xiàn)在需要創(chuàng)建兩個服務模塊:

  • 訂單服務web模塊 用來創(chuàng)建訂單
  • 用戶服務service模塊 查詢用戶地址

這里的訂單服務 web 模塊一般指的是對外 HTTP 服務,用戶服務 service 模塊一般指的是對外提供 RPC 服務,訂單服務也可稱為服務消費者,用戶服務也可以稱為服務提供者

預期結果:

訂單服務 web 模塊在 A 服務器,用戶服務模塊在 B 服務器,A 可以遠程調用 B 功能

一 創(chuàng)建服務提供者
  1. 使用 idea 創(chuàng)建一個新的 Maven Project
  1. 設置 GroupId 和 ArtifactId,GroupId 一般為反轉域名來定義,ArtifactId 則是項目的具體名稱
  1. 直接 finish


4.在 gmall 下常見 module,創(chuàng)建 maven 項目 next

5.GroupId 是創(chuàng)建 project 時加過的,ArtifactId 是當前項目 module 的名稱,點擊next

  1. Module name 盡量和第二步 ArtifactId 加的一樣,沒問題后 Finsh,Module 就創(chuàng)建成功了,在提供者的目錄下創(chuàng)建具體接口實現(xiàn)

目錄結構

  • user-service-provider用戶模塊:用戶服務接口的實現(xiàn)
    • UserServiceImpl:服務方提供實現(xiàn)接口(UserService)
    • provider.xml:用 Spring 配置聲明暴露服務
    • MainApplication:加載 Spring 配置

代碼

// UserServiceImpl:服務方提供實現(xiàn)接口(UserService)
public class UserServiceImpl implements UserService {
    @Override
    public List<UserAddress> getUserAddressList(String userId){
        UserAddress address1 = new UserAddress(1,"北京","1","beijing","1234567891","Y");
        UserAddress address2 = new UserAddress(1,"上海","2","shanghai","1234567892","N");
        return Arrays.asList(address2,address1);
    }
}
// MainApplication:加載 Spring 配置
public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();
        System.in.read();
    }
}
// pom.xml 
// 引入dubbo依賴
// 因為注冊中心使用的是zookeeper,所以引入zookeeper依賴
// 引入gmall-interface依賴(使用bean和接口)
<?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>gmall-test-test</artifactId>
        <groupId>com.sangyu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sangyu</groupId>
    <artifactId>user-service-provider</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sangyu</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 注冊中心使用的是zookeeper,引入操作zookeeper的客戶端-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>
// 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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方應用信息,用于計算依賴關系 -->
    <dubbo:application name="user-service-provider"  />

    <!-- 使用multicast廣播注冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo協(xié)議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明需要暴露的服務接口 -->
    <dubbo:service interface="com.sangyu.gmall.service.UserService" ref="UserServiceImpl" />

    <!-- 和本地bean一樣實現(xiàn)服務 -->
    <bean id="UserServiceImpl" class="com.sangyu.gmall.service.impl.UserServiceImpl" />
</beans>

二 創(chuàng)建服務消費者

創(chuàng)建 module 過程和服務提供者相同,在 gmall 項目下創(chuàng)建 module,并 ArtifactId 設置為 order-service-consumer

目錄結構

  • order-service-consumer訂單模塊:調用用戶模塊
    • OrderServiceImpl:服務方提供實現(xiàn)接口(UserService)
    • consumer.xml:用 Spring 配置聲明暴露服務
    • MainApplication:加載 Spring 配置

代碼

// OrderServiceImpl:服務方提供實現(xiàn)接口(UserService)
@Service // 使用spring的注解將OrderServiceImpl注入到容器中
public class OrderServiceImpl implements OrderService {

    @Autowired //使用Autowired將UserService注入進來,UserService已經在consumer.xml配置后已經在容器中了
    UserService userService;

    @Override
    public void initOrder(String userId) {
        System.out.println("用戶id:" + userId);

        List<UserAddress> addressList = userService.getUserAddressList(userId);

        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }

    }
}

// MainApplication:加載 Spring 配置
public class MainApplication {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("consumer.xml");
        OrderService orderService = applicationContext.getBean(OrderService.class);
        orderService.initOrder("1");
        System.out.println("調用完成");
        System.in.read();
    }
}
// consumer.xml:用 Spring 配置聲明暴露服務
<?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://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.sangyu.gmall.service.impl"></context:component-scan>

    <!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="order-service-consumer"  />

    <!-- 使用zookeeper廣播注冊中心暴露發(fā)現(xiàn)服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!--聲明需要調用的遠程服務的接口;生成遠程服務代理  -->
    <dubbo:reference id="UserService" interface="com.sangyu.gmall.service.UserService" />
</beans>
// pom.xml
// 引入dubbo依賴
// 因為注冊中心使用的是zookeeper,所以引入zookeeper依賴
// 引入gmall-interface依賴(使用bean和接口)
<?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>gmall-test-test</artifactId>
        <groupId>com.sangyu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sangyu</groupId>
    <artifactId>order-service-consumer</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sangyu</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

三 創(chuàng)建公共接口層

創(chuàng)建 module 過程和服務提供者不同,在 gmall 項目下創(chuàng)建 module ,并ArtifactId 設置為 gmall-interface,提供 javabean 和服務接口

作用:定義公共接口,也可以導入公共依賴

目錄結構

代碼

// UserAddress類(javabean)
public class UserAddress {
    private Integer id;
    private String userAddress; // 用戶地址
    private String userId; // 用戶id
    private String consignee; // 收貨人
    private String phoneNum; // 電話號碼
    private String isDefault; // 是否為默認地址 Y-是 N-否
    public UserAddress() {
        super();
    }
    public UserAddress(Integer id, String userAddress, String userId, String consignee,
                       String phoneNum, String isDefault) {
        this.id = id;
        this.userAddress = userAddress;
        this.userId = userId;
        this.consignee = consignee;
        this.phoneNum = phoneNum;
        this.isDefault = isDefault;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserAddress() {
        return userAddress;
    }
    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public String getIsDefault() {
        return isDefault;
    }
    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }
}
// 用戶服務接口
public interface UserService {
    /**
     * 按照用戶id返回所有的收獲地址
     * @param userId
     * @return
     */
    public List<UserAddress> getUserAddressList(String userId);
}
// OrderService接口初始化訂單
public interface OrderService {

    /**
     * 初始化訂單
     * @param userId
     */
    public void initOrder(String userId);
}
四 測試運行

先運行服務提供者 user-service-provider,再運行服務調用者 order-service-provider,兩者運行前要的保證 zookeeper 是運行狀態(tài)

// 運行結果
用戶id:1
上海
北京
調用完成
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容