栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot整合Dubbo3.0+Zookeeper

SpringBoot整合Dubbo3.0+Zookeeper

Dubbo是一个分布式服务框架,用于多个系统间的RPC相互调用

Docker安装Zookeeper
# 1、查看镜像
docker search zookeeper

# 2、拉取镜像
docker pull zookeeper

# 3、查看镜像
docker images

# 4、运行容器
# 命令限制它的内存大小,并映射端口2181到本地
docker run -d --name myZookeeper --restart always -e JVMFLAGS="-Xmx1024m" -p 2181:2181 zookeeper

# 5、查看容器是否运行
docker ps

# 6、进入容器
docker exec -it myZookeeper /bin/bash

# 7、进入到bin目录下,连接zkClinet.sh
cd bin
zkClinet.sh
项目搭建

父模块搭建

pom.xml文件



    4.0.0

    com.buddha
    springboot-dubbo-zookeeper
    1.0.0
    
        provider
        consumer
        common
    

    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
        
    

    
    
        UTF-8
        1.8
        1.8
        3.0.4
        4.2.0
    

    
        
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                ${dubbo-boot.version}
            
            
            
                org.apache.curator
                curator-x-discovery
                ${zkclient.version}
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

公共common子模块

pom.xml文件



    
        springboot-dubbo-zookeeper
        com.buddha
        1.0.0
    
    4.0.0

    common

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

service接口文件

package com.buddha.service;

public interface User {
    String getName(String name);
}
消费者provider子模块

pom.xml文件



    
        springboot-dubbo-zookeeper
        com.buddha
        1.0.0
    
    4.0.0

    provider

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.curator
            curator-x-discovery
        
        
            com.buddha
            common
            1.0.0
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.yml配置文件

server:
  port: 8080
dubbo:
  application:
    name: provider
  registry:
    address: zookeeper://192.168.123.199:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.buddha.service

主程序main类

package com.buddha;

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

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

service实现类

package com.buddha.Service;

import com.buddha.service.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

@Component
@DubboService
public class UserImpl implements User {
    @Override
    public String getName(String name) {
        return "hello " + name;
    }
}
消费者consumer子模块

pom.xml文件



    
        springboot-dubbo-zookeeper
        com.buddha
        1.0.0
    
    4.0.0

    consumer

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.curator
            curator-x-discovery
        
        
            com.buddha
            common
            1.0.0
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.yml配置文件

server:
  port: 80
dubbo:
  application:
    name: consumer
  registry:
    address: zookeeper://192.168.123.199:2181
  protocol:
    name: dubbo
    port: 20880

main主类

package com.buddha;

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

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

controller类

package com.buddha.controller;

import com.buddha.service.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SiteController {
    @DubboReference
    private User user;

    @RequestMapping("/user")
    public String test(@RequestParam("name") String name) {
        return user.getName(name);
    }
}

从而实现了从一个系统到另外一个系统RPC调用的DEMO

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号