dubbo-HelloWorld

LZC發表於2020-09-02

建立maven父工程,引入如下依賴

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

建立三個子模組,dubbo-api、dubbo-provider、dubbo-consumer

dubbo-api

定義介面

public interface DemoService {
    String sayHello(String name);
}

dubbo-provider

服務提供者需要引入dubbo-api模組。

實現DemoService介面

public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

在resources目錄下新建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="hello-world-app"  />

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

    <!-- 用dubbo協議在20880埠暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 宣告需要暴露的服務介面 -->
    <dubbo:service interface="com.lzc.dubbo.api.DemoService" ref="demoService" />

    <!-- 和本地bean一樣實現服務 -->
    <bean id="demoService" class="com.lzc.dubbo.provider.DemoServiceImpl" />
</beans>

在resources目錄下新建log4j.properties(用來列印日誌的配置檔案)檔案,內容如下

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n

啟動服務提供者

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
        context.start();
        System.in.read(); // 按任意鍵退出
    }
}

dubbo-consumer

服務消費者需要引入dubbo-api模組。。在resources目錄下新建consumer.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="consumer-of-hello-world-app"  />

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

    <!-- 生成遠端服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="demoService"
                     interface="com.lzc.dubbo.api.DemoService" />
</beans>

呼叫服務提供方

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 獲取遠端服務代理
        String hello = demoService.sayHello("world"); // 執行遠端方法
        System.out.println( hello ); // 顯示呼叫結果
    }
}

建立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 https://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>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>

    <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>

        <!-- springboot整合dubbo的起步依賴 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.7</version>
            <type>pom</type>
        </dependency>
    </dependencies>

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

</project>

建立三個子模組dubbo-interface、dubbo-provider、dubbo-consumer

dubbo-interface

編寫一個介面類

public interface UserService {
    User getUserById(Integer id);
}

dubbo-provider

服務提供者需要引入dubbo-interface模組。

實現UserService介面

@DubboService
public class UserServiceImpl implements UserService {
    @Override
    public User getUserById(Integer id) {
        return new User(id, "zhangsan");
    }
}

application.properties內容如下

server.port=8091
# spring專案名
spring.application.name=dubbo-provider-demo

# Dubbo provider configuration
# dubbo.application.name=${spring.application.name}
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181

# Dubbo Protocol
dubbo.protocol.name=dubbo
# Random port
dubbo.protocol.port=-1

# 掃描註解包透過該設定將服務註冊到zookeeper
dubbo.scan.base-packages=com.example.dubbo.provider.service

啟動類

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

dubbo-consumer

服務消費者需要引入dubbo-interface模組。

建立DemoController,用來呼叫服務提供方

@RestController
public class DemoController {

    @DubboReference
    private UserService userService;

    @GetMapping("/demo/{id}")
    public User demo(@PathVariable Integer id) {
        return userService.getUserById(id);
    }
}

application.properties內容如下

server.port=8092
# spring專案名
spring.application.name=dubbo-consumer-demo
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181

啟動類

@SpringBootApplication
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結