1. 程式人生 > >idea&SpringBoot&Dubbo&zookeper簡單整合

idea&SpringBoot&Dubbo&zookeper簡單整合

    今天再整合SpringBoot/Dubbo/zookeper時遇到幾個麻煩,歷時兩個小時終於解決了。記錄下成果和問題,為避免今後得重蹈覆轍

一、主專案dubbo1

首先看下專案的結構

這裡分為三個子模組,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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</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>
    <modules>
        <module>provider</module>
        <module>consumer</module>
        <module>api</module>
    </modules>

</project>

接著分別介紹三個子模組

二、API模組

api種存放著SysUser實體及SysUserService介面兩個檔案,consumer和provider模組都需要依賴這個模組。

SysUser

package com.example.api.entity;

import java.io.Serializable;

public class SysUser implements Serializable {

    private static final long serialVersionUID = 5250277639999879824L;

    //使用者id
    private Long id;
    //使用者姓名
    private String username;
    //使用者密碼
    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

SysUserService

package com.example.api.service;

import com.example.api.entity.SysUser;

public interface SysUserService {
    SysUser getSysUser();
}

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.example</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>api</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>11</java.version>
    </properties>

</project>

三、provider提供者

作為提供者,可以存放service介面的實現類,如果專案中集成了mybatis,也可以放在這個模組下

SysUserServiceImpl

package com.example.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.api.entity.SysUser;
import com.example.api.service.SysUserService;
import org.springframework.stereotype.Component;

@Component
@Service(version = "1.0.0", interfaceClass = SysUserService.class)
public class SysUserServiceImpl implements SysUserService {


    @Override
    public SysUser getSysUser() {
        SysUser sysUser = new SysUser();
        sysUser.setId(1L);
        sysUser.setUsername("張三");
        sysUser.setPassword("12346");

        return sysUser;
    }
}

application.yml

server:
  port: 7002
## Dubbo 服務提供者配置
spring:
  dubbo:
    application:
      name: provider
      ## Dubbo 服務物件的註冊中心zookeeper的地址和埠
    registry:
      address: zookeeper://192.168.1.141:2181
    ## 用Dubbo協議在20880埠暴露服務
    protocol:
      name: dubbo
      port: 20880
    ## 包掃描範圍
    scan:
      com.example.provider.service.impl

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
        </dependency>
        <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>

四、consumer消費者

作為消費者的角色,可以存放controller控制類,當然,如果有其他的,比例攔截器,許可權控制,也可以放這裡。

SysUserController

package com.example.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.api.entity.SysUser;
import com.example.api.service.SysUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SysUserController {

    @Reference(version = "1.0.0",check= false)
    private SysUserService sysUserService;

    @RequestMapping("/getSysUser")
    public SysUser getSysUserList(){
        return sysUserService.getSysUser();
    }

}

application.yml

server:
  port: 7003

spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: zookeeper://192.168.1.141:2181
    scan: com.example.consumer.controller

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </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>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <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>

五、zookeper安裝

選擇合適的映象地址下載ZooKeeper

http://mirrors.hust.edu.cn/apache/zookeeper/

我在以前就有一個3.4.14,官網上沒有選擇3.4.12也一樣

下載完後進行解壓,將conf目錄下的zoo_sample.cfg檔案更名為zoo.cfg

啟動doc命令Ctrl+r => cmd  進入你zookeper的安裝目錄,執行zkCli.cmd

這樣zookeper就安裝完成了。

六、Dubbo-admin安裝

    連結:https://pan.baidu.com/s/1SXg2-IbnP8ZHWTIMideTVA 
    提取碼:lqzf 

匯入到idea

修改application.properties檔案,zookeeper改為自己電腦安裝的

啟動Dubbo-Admin輸入賬號密碼root/root

啟動提供者=>啟動消費者,檢視註冊中心結果

呼叫方法

七、注意事項