美文网首页
SpringBoot+Dubbo+Zookeeper集群实战

SpringBoot+Dubbo+Zookeeper集群实战

作者: 长孙俊明 | 来源:发表于2019-10-11 11:48 被阅读0次

搭建环境

Idea

工程目录结构

parent 父工程

api-service 子工程
api-service-impl 子工程
web 子工程

创建父工程Parent

image.png
image.png
<?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.springboot.dubbo</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>api-service-impl</module>
        <module>api-service</module>
        <module>web</module>
    </modules>

    <!-- 编写统一的版本控制 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java-version>1.8</java-version>
        <curator-framework>4.0.1</curator-framework>
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.starter.version>0.2.0</dubbo.starter.version>
    </properties>


    <dependencies>
        <!-- SpringBoot 整合 Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.starter.version}</version>
        </dependency>

    </dependencies>
</project>

创建子工程api-service

<?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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.dubbo</groupId>
    <artifactId>api-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-service</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>
    </dependencies>

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

</project>

package com.springboot.dubbo.apiservice.api;

public interface IMember {
    public String getUser();
}

package com.springboot.dubbo.apiservice;

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

@SpringBootApplication
public class ApiServiceApplication {

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

}

创建子工程api-service-impl

server:
 port: 9002

dubbo:
 application:
 ###########注册到注册中心的名称############
   name: member
 ###########采用协议和端口号################
 protocol:
 ###########采用dubbo协议####################
   name: dubbo
 ###########发布dubbo端口号为20880###########
   port: 20881
 registry:
 ###########注册中心地址#####################
   address: 120.24.55.137:2181, 120.24.57.49:2181, 47.106.100.211:2181
   protocol: zookeeper
 scan:
   base-packages: com.springboot.dubbo.service.impl
<?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>parent</artifactId>
        <groupId>com.springboot.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api-service-impl</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.springboot.dubbo</groupId>
            <artifactId>api-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>
package com.springboot.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.springboot.dubbo.apiservice.api.IMember;

@Service
public class MemberImpl implements IMember {
    public String getUser() {
        return "我是谁啊";
    }
}

package com.springboot.dubbo;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

创建子工程 web

server:
 port: 9003

dubbo:
 application:
 ###########注册到注册中心的名称############
   name: order
 ###########采用协议和端口号################
 registry:
 ###########注册中心地址#####################
   address: 120.24.55.137:2181, 120.24.57.49:2181, 47.106.100.211:2181
   protocol: zookeeper
 scan:
   base-packages: com.springboot.dubbo.service.impl
   ## 调用服务超时时间
 consumer:
   timeout: 5000
package com.springboot.dubbo;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

<?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>parent</artifactId>
        <groupId>com.springboot.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.springboot.dubbo</groupId>
            <artifactId>api-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

结果

image.png

如何安装Zookeeper集群

相关文章

网友评论

      本文标题:SpringBoot+Dubbo+Zookeeper集群实战

      本文链接:https://www.haomeiwen.com/subject/rortmctx.html