springboot整合dubbo-zookeeper

springboot整合dubbo-zookeeper

1.zookeeper本地安装启动验证

1.1 zookeeper安装

1.1.1 下载zookeeper
http://archive.apache.org/dist/zookeeper/
1.1.2安装zookeeper流程地址
https://blog.csdn.net/qq_42445425/article/details/120049951?spm=1001.2014.3001.5501
1.1.3启动zookeeper

springboot整合dubbo-zookeeper_第1张图片

1.1.4验证zookeeper是否成功

springboot整合dubbo-zookeeper_第2张图片

2.dubbo本地安装启动验证

2.1 下载dubbo的war包(直接下载打好的war,避免自己长时间打包)

下载地址:http://www.java1234.com/a/javabook/javaweb/2018/0224/10496.html

2.2 启动dubbo

2.2.1 将dubbo的war包拷贝到tomcat的webapps目录下

springboot整合dubbo-zookeeper_第3张图片

2.2.2 启动dubbo-tomcat

springboot整合dubbo-zookeeper_第4张图片

2.2.3验证dubbo是否成功

springboot整合dubbo-zookeeper_第5张图片
springboot整合dubbo-zookeeper_第6张图片
如上图证明dubbo和zookeeper环境搭建完毕;

3.springboot-provider 服务生产端代码整合

3.1 创建一个普通maven项目

springboot整合dubbo-zookeeper_第7张图片

3.2配置初始化的pom坐标,内容如下:


<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.0modelVersion>
    <groupId>com.sl.tmcgroupId>
    <artifactId>tmc-providerartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.5version>
        <relativePath/> 
    parent>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>io.dubbo.springbootgroupId>
            <artifactId>spring-boot-starter-dubboartifactId>
            <version>1.0.0version>
        dependency>
        
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.7version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

3.3 配置yml文件

server:
  port: 8082 #Tomcat端口
spring:
  dubbo:
    application:
      name: TmcProvider #应用名
    registry:
      address: zookeeper://localhost #zookeeper地址
      port: 2181 #提供注册的端口
    protocol:
      name: dubbo
      port: 20890 #dubbo服务暴露的端口
    scan: com.sl.tmc.service.impl #扫描的包名

3.4创建服务启动类-测试是否能成功启动

springboot整合dubbo-zookeeper_第8张图片

3.5创建生产接口

springboot整合dubbo-zookeeper_第9张图片

3.6启动生产接口服务,在dubbo客户端上验证接口注册

springboot整合dubbo-zookeeper_第10张图片
如上图:生产接口发布成功!

4.springboot-consumer 消费端代码整合

4.1 创建一个普通的maven项目;(同3.1)

4.2 配置初始化pom坐标;(同3.2的基础上新增如下坐标:生产服务的坐标)


        <dependency>
            <groupId>com.sl.tmcgroupId>
            <artifactId>tmc-providerartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

4.3配置消费端服务的yml文件

server:
  port: 8083 #Tomcat端口
spring:
  dubbo:
    application:
      name: TmcConsumer #应用名
    registry:
      address: zookeeper://localhost #zookeeper地址
      port: 2181 #提供注册的端口
    scan: com.sl.tmc.controller #扫描的包名

4.4启动消费端服务-测试消费服务是否正常启动

4.5创建消费端消费接口

@Controller
@RequestMapping("/demo")
public class DemoController {

    @Reference(version = "1.0.0",timeout = 300)
    private DemoService demoService;

    @GetMapping("/testDubbo")
    @ResponseBody
    public String testDubbo(){
        try {
            String str = demoService.testDubbo();
            System.out.println(str);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return "调用失败";
        }
    }
}

springboot整合dubbo-zookeeper_第11张图片

4.6 验证服务调用是否成功

4.6.1 通过dubbo客户端验证消费端服务是否注册成功

springboot整合dubbo-zookeeper_第12张图片
springboot整合dubbo-zookeeper_第13张图片
如上图:已证明消费服务注册成功!

4.6.2 通过postman或者浏览器直接调用接口验证是否成功

springboot整合dubbo-zookeeper_第14张图片
如上图:已证明dubbo-zookeeper-springboot微服务之间整合搭建调用完成;

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