SpringBoot整合dubbo3.0+nacos2.0以及zookeeper

springboot2.6 + dubbo 3.0 + nacos 2.0和zookeeper ,nacos和zookeeper依赖都引入了,想用哪个都可以

1.项目结构

SpringBoot整合dubbo3.0+nacos2.0以及zookeeper_第1张图片
sg-app:消费者
sg-com-api:公共接口 (消费者和提供者需要引用)
sg-com-tool:公共工具包 (提供时间转换,字符串转换等工具转换类) 不详写也可以不加
sg-web-parent:父依赖包 (消费者和提供者需要继承) 用于管理依赖
sg-web-provider:提供者

1.创建父模块 sg-web-parent

在这里插入图片描述
注:父类只用于管理依赖,处理多个消费者和提供者的情况,所以只有pom

  <?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>cn.xwl</groupId>
    <artifactId>sg-web-parent</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <description>web页面服务父类包</description>


    <properties>
        <spring_version>2.6.1</spring_version>
        <dubbo_version>3.0.4</dubbo_version>
        <curator_version>4.2.0</curator_version>
        <nacos_version>2.0.3</nacos_version>
        <fastjson_version>2.11.2</fastjson_version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--自定义工具包-->
            <dependency>
                <groupId>cn.xwl</groupId>
                <artifactId>sg-com-tool</artifactId>
                <version>1.0</version>
            </dependency>
            <!--springboot基本依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <!--test-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <!--springboot配置文件依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>${spring_version}</version>
                <optional>true</optional>
            </dependency>
            <!--springboot web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring_version}</version>
<!--                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-json</artifactId>
                    </exclusion>
                </exclusions>-->
            </dependency>
            <!--boot整合dubbo-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo_version}</version>
            </dependency>
            <!--注册中心 zookeeper 使用-->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-x-discovery</artifactId>
                <version>${curator_version}</version>
            </dependency>
             <!--注册中心 nacos 使用-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>${dubbo_version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>${nacos_version}</version>
            </dependency>
            <!--Jackson required包 用于springboot引发的json冲突-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>${fastjson_version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${fastjson_version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>${fastjson_version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
</project>

2.服务发布者配置sg-web-provider

pom

<?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>cn.xwl</groupId>
        <artifactId>sg-web-parent</artifactId>
        <version>1.0</version>
        <relativePath/>
    </parent>

    <groupId>cn.xwl</groupId>
    <artifactId>sg-web-provider</artifactId>
    <version>1.0</version>
    <description>web端接口提供</description>

    <dependencies>
        <dependency>
            <groupId>cn.xwl</groupId>
            <artifactId>sg-com-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!--自定义工具包-->
        <dependency>
            <groupId>cn.xwl</groupId>
            <artifactId>sg-com-tool</artifactId>
        </dependency>
        <!--springboot基本依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--springboot配置文件依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--boot整合dubbo-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!--zookeeper注册中心-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
        </dependency>
        <!--nacos注册中心-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <!--nacos版本-->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>



    </dependencies>

</project>

yml

server:
  port: 8080
dubbo:
  application: #当前这个应用的信息
    name: sg-web-provider #应用名
  protocol: # 服务采用的协议 基本默认未dubbo协议(netty)
    name: dubbo
    port: 20880 #dubbo服务暴露的端口
  registry: #注册相关信息
    address: zookeeper://10.250.204.106:2181 #注册中心配置
    #address: nacos://10.250.204.106:8848 #注册中心配置
    timeout: 30000 # 到注册中心的超时时间
  provider:
    retries: -1 #重试次数 consumer
    delay: -1 #延迟到spring容器加载之后
  scan: #需要发布的服务接口实现类扫描路径
    base-packages: cn.xwl.dubbo

接口发布

package cn.xwl.dubbo.login.service;



import cn.sg.user.LoginService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class LoginServiceImpl implements LoginService {

    public String baseLogin(String username, String password) {

        if("123".equals(password)){
            return "登录成功!欢迎你"+username;
        }
        return "登陆失败,请检查密码是否正确";
    }

}

在yml中配置dubbo.scan.base-packages为扫描路径,与spring扫描类似,在扫描到的路径下创建需要发布的接口 并添加@DubboService,服务就i会自动发布到nacos中

3.服务消费者配置

pom:

<?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>sg-web-parent</artifactId>
        <groupId>cn.xwl</groupId>
        <version>1.0</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>sg-app</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.xwl</groupId>
            <artifactId>sg-com-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!--自定义工具包-->
        <dependency>
            <groupId>cn.xwl</groupId>
            <artifactId>sg-com-tool</artifactId>
        </dependency>
        <!--springboot基本依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--springboot配置文件依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--boot整合dubbo-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!--注册中心-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--nacos注册中心-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <!--nacos版本-->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <!--Jackson required包-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

    </dependencies>


</project>

yml

server:
  port: 7070
dubbo:
  application: #当前这个应用的信息
    name: sg-web-consumer #应用名
  registry: #注册相关信息
    #address: zookeeper://10.250.204.106:2181 #注册中心配置
    address: nacos://10.250.200.103:8848 #注册中心配置
    timeout: 30000 # 到注册中心的超时时间
  consumer:
    timeout: 50000 # 访问服务超时时间

服务实现接口:

package web.login;


import cn.sg.user.LoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class loginController {

    @DubboReference
    public LoginService loginService;


    @RequestMapping("baseLogin")
    public String baseLogin(String username,String password){
        return loginService.baseLogin(username,password);
    }

}

服务实现接口只需要直接使用@DubboReference 引用接口类就能够直接远程调用了,前提是消费者和提供者都引用了公共接口包。

4.公共接口包sg-com-api

package cn.sg.user;

//登录接口服务定义
public interface LoginService {

   //普通登录接口 通过用户名密码登录
   String baseLogin(String username,String password);

}

只定义接口和实体类

运行结果

SpringBoot整合dubbo3.0+nacos2.0以及zookeeper_第2张图片
访问http://localhost:7070/baseLogin?username=xwl&password=123
在这里插入图片描述

问题: 因为使用的是dubbo3.0 好像只能使用nacos2.0以上的版本 这点需要注意 我踩了很多坑。

你可能感兴趣的:(spring,boot,java,spring,dubbo,zookeeper)