java框架之SpringBoot(16)-分佈式及整合Dubbo

前言

分佈式應用

在分佈式系統中,國內經常使用 Zookeeper + Dubbo 組合,而 SpringBoot 推薦使用 Spring 提供的分佈式一站式解決方案 Spring + SpringBoot + SpringCloud。html

圖 1:分佈式架構圖

Zookeeper和Dubbo

  • Zookeeper 是一個分佈式的,開放源碼的分佈式應用程序協調服務。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名維護、分佈式同步、組服務等。
  • Dubbo 是 Alibaba 開源的分佈式服務框架,它最大的特色是按照分層的方式來架構,使用這種方式能夠使各個層之間解耦合(或者最大限度地鬆耦合)。從服務模型的角度來看,Dubbo 採用的是一種很是簡單的模型,要麼是提供方提供服務,要麼是消費方消費服務,因此基於這一點能夠抽象出服務提供方(Provider)和服務消費方(Consumer)兩個角色。

圖 2:Dubbo 架構圖java

整合Dubbo

安裝Zookeeper

參考【Docker 中安裝 Zookeeper】。git

Provider工程

一、新建 Provider 工程,引入 Web 場景啓動器和 Dubbo 及 Zookeeper 相關依賴:github

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>zze.springboot.dubbo</groupId>
    <artifactId>provider-ticket</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider-ticket</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <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 整合 SpringBoot 的場景啓動器-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!--引入 Zookeeper 客戶端工具依賴-->
        <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>
pom.xml

二、配置 Provider 工程註冊到 Zookeeper:web

dubbo:
  application:
    name: provider-ticket # 指定服務應用名稱
  registry:
    address: zookeeper://192.168.202.136:2181 # Zookeeper 服務地址
  scan:
    base-packages: zze.springboot.dubbo.ticket.service # 掃描指定包下服務註冊到 Zookeeper
application.yml

三、編寫要註冊的服務接口及其實現類:spring

package zze.springboot.dubbo.ticket.service;

public interface TicketService {
    public String getTicket();
}
zze.springboot.dubbo.ticket.service.TicketService
package zze.springboot.dubbo.ticket.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import zze.springboot.dubbo.ticket.service.TicketService;

@Component // 註冊到 IoC 容器
@Service // 使用 Dubbo 的 @Service 註解標註這是一個要發佈的服務
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "《大話西遊》";
    }
}
zze.springboot.dubbo.ticket.service.impl.TicketServiceImpl

四、啓動工程,看到 Dubbo 服務註冊信息以下:apache

Consumer工程

一、新建 Consumer 工程,引入 Web 場景啓動器和 Dubbo 及 Zookeeper 相關依賴:springboot

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>zze.springboot.dubbo</groupId>
    <artifactId>consumer-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer-user</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <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 整合 SpringBoot 的場景啓動器-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!--引入 Zookeeper 客戶端工具依賴-->
        <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>
pom.xml

二、配置 Zookeeper 地址:架構

dubbo:
  application:
    name: consumer-user # 配置消費者服務應用名稱
  registry:
    address: zookeeper://192.168.202.136:2181 # Zookeeper 服務地址
application.yml

三、拷貝 Provider 工程中服務接口到 Consumer 工程:app

package zze.springboot.dubbo.ticket.service;

public interface TicketService {
    public String getTicket();
}
zze.springboot.dubbo.ticket.service.TicketService

四、編寫 Service 類,遠程引用 Provider 提供的 Service 實現:

package zze.springboot.dubbo.user.service;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import zze.springboot.dubbo.ticket.service.TicketService;

@Service
public class UserService {
    @Reference // 遠程引用 Provider 提供的實現
    private TicketService ticketService;

    public String buyTicket(){
        return ticketService.getTicket();
    }
}
zze.springboot.dubbo.user.service.UserService

五、測試:

package zze.springboot.dubbo.user;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import zze.springboot.dubbo.user.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerUserApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    public void contextLoads() {
        String s = userService.buyTicket();
        System.out.println(s);
        /*
        《大話西遊》
         */
    }

}
test
相關文章
相關標籤/搜索