SpringBoot(2.1.6) + dubbo(apache 2.7.5) + zookeeper 集成并测试

树莓派集群 同时被 2 个专栏收录
5 篇文章 0 订阅
2 篇文章 0 订阅

Demo地址:https://github.com/bestksl/DubboDemo02
由于嫌麻烦 依赖基本都放到模块外部的project pom里了, 大家可以选择导入相应的子模块。

consumer 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">
    <parent>
        <artifactId>DubboDemo02</artifactId>
        <groupId>com.bestksl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>user-consumer</artifactId>
    <dependencies>
        <!--接口模块依赖-->
        <dependency>
            <groupId>com.bestksl</groupId>
            <artifactId>user-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

provider 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">
         
    <parent>
        <artifactId>DubboDemo02</artifactId>
        <groupId>com.bestksl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-provider</artifactId>

    <dependencies>
        <!--接口模块依赖-->
        <dependency>
            <groupId>com.bestksl</groupId>
            <artifactId>user-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

模块外部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.bestksl</groupId>
    <artifactId>DubboDemo02</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>


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

    <modules>
        <module>user-api</module>
        <module>UserProvider</module>
        <module>user-consumer</module>
    </modules>

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

        <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-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.5</version>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>apache.snapshots.https</id>
            <name>Apache Development Snapshot Repository</name>
            <url>https://repository.apache.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>


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

        </plugins>
    </build>
</project>

provider: application.properties

# user-provider配置文件
#端口配置
server.port=8080
## Dubbo 服务提供者配置
# provider应用名称
spring.application.name=UserProvider
# Dubbo组件扫描的基础包
# dubbo.scan.base-packages=com.bestksl.service
# Dubbo应用程序名称,的默认值是$ {spring.application.name}
## dubbo.application.name=${spring.application.name}
# Dubbo 协议与端口
dubbo.protocol.name=dubbo
#dubbo.protocol.port=12345
## Dubbo 注册地址 N/A表示直连方式
#dubbo.registry.address=N/A
embedded.zookeeper.port=2181
dubbo.registry.address=zookeeper://pi02:${embedded.zookeeper.port}

consumer: application.properties

# demo-provider配置文件
#端口配置,为防止端口冲突,该模块端口使用18080
server.port=18080
#dubbo 消费者配置
# 应用名称,配置模块项目名称即可
spring.application.name=demo-provider
## Dubbo 注册地址 N/A表示直连方式
#dubbo.registry.address=N/A
embedded.zookeeper.port=2181
dubbo.registry.address=zookeeper://pi02:${embedded.zookeeper.port}
dubbo.consumer.check=false

Consumer main

package com.bestksl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author HaoxuanLi
 * @version 1.0
 * @date 2020/3/17 20:13
 */
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

provider main

注意@EnableDubbo

package com.bestksl.service;

import com.bestksl.util.EmbeddedZooKeeper;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

/**
 * @author HaoxuanLi
 * @version 1.0
 * @date 2020/3/17 17:38
 */

@EnableDubbo
@SpringBootApplication
public class DemoProvider {
    public static void main(String[] args) {
        SpringApplication.run(DemoProvider.class, args);
//        new SpringApplicationBuilder(DemoProvider.class)
//                .listeners((ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> {
//                    Environment environment = event.getEnvironment();
//                    int port = environment.getProperty("embedded.zookeeper.port", int.class);
//                    new EmbeddedZooKeeper(port, false).start();
//                }).run(args);
    }
}

api模块定义接口

package com.bestksl.service;

/**
 * @author HaoxuanLi
 * @version 1.0
 * @date 2020/3/17 17:32
 */
public interface UserServiceApi {
    String sayHello(String name);
}

provider实现接口

package com.bestksl.service;


import org.apache.dubbo.config.annotation.Service;

/**
 * @author HaoxuanLi
 * @version 1.0
 * @date 2020/3/17 17:36
 */
@Service(version = "1.0")
public class UserService implements UserServiceApi {
    @Override
    public String sayHello(String name) {
        return "hello" + name + System.currentTimeMillis() / 1000;
    }
}

consumer调用接口

package com.bestksl;

import com.bestksl.service.UserServiceApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author HaoxuanLi
 * @version 1.0
 * @date 2020/3/17 18:52
 */

@RestController
public class userInfoController {

    @Reference(version = "1.0")
    UserServiceApi userServiceApi;

    @GetMapping("/testUserApi")
    public String testUser() {
        return userServiceApi.sayHello("ksl666");
    }
}

成功!

在这里插入图片描述

  • 3
    点赞
  • 3
    评论
  • 6
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值