springboot+zookeeper+dubbo 搭建项目

 

 

最近在自学分布式的一些东西,所以搭建了一个rpc远程调用框架demo,在这里记录一下。

首先创建看看项目结构,我分成了3个服务,api是公共的接口服务。

springboot+zookeeper+dubbo 搭建项目_第1张图片

 

父项目的pom



    4.0.0
        
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    com.wrc.rpc
    rpcdemo
    pom
    1.0-SNAPSHOT
    
        api
        provider
        consumer
    

    
        0.2.0
        3.4.10
    



    
        
            
                com.alibaba.boot
                dubbo-spring-boot-starter
                ${dubbo.version}
            
            
                org.apache.zookeeper
                zookeeper
                ${zookeeper.vsersion}
                
                    
                        org.slf4j
                        slf4j-log4j12
                    
                
            

        
    


provider项目的pom



    4.0.0






    
        rpcdemo
        com.wrc.rpc
        1.0-SNAPSHOT
    

    com.wrc.rpc
    provider
    0.0.1-SNAPSHOT
    provider
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            com.wrc.rpc
            api
            1.0-SNAPSHOT
            compile
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
        
        
            org.apache.zookeeper
            zookeeper
    
    

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


需要继承父项目的pom,然后引用。consumer和provider一样即可。

provider项目结构

springboot+zookeeper+dubbo 搭建项目_第2张图片

application.properties配置文件

server.port=9000

#注册名字
dubbo.application.name=provider-demo

#zk注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#通讯规则 端口 协议
dubbo.protocol.port=20881
dubbo.protocol.name=dubbo

启动类

package com.wrc.rpc.provider;

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

//引用这个注解,会去扫描dubbo
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("发布方启动成功");
    }

}

接口的发布

package com.wrc.rpc.provider.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wrc.api.HelloDubboApi;
import org.springframework.stereotype.Component;

/**
 * @Author: wrc
 * @Classname HelloImpl
 * @Description TODO
 * @Date 2020/7/1 10:01
 * @Created wrc
 */
//service 这里引用dubbo的包,注意!
@Service 
@Component
public class HelloImpl  implements HelloDubboApi {


    @Override
    public String hello(String name) {
        return "干就完了"+name;
    }
}

 

consumer的配置文件

dubbo.application.name=consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181

server.port=9001

启动类

package com.wrc.rpc.consumer;

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


@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
        System.out.println("客户端启动成功");
    }

}

接口的调用

package com.wrc.rpc.consumer.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wrc.api.HelloDubboApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wrc
 * @Classname HelloController
 * @Description TODO
 * @Date 2020/7/1 10:49
 * @Created wrc
 */
@RestController
public class HelloController {

    @Reference
    private HelloDubboApi helloDubboApi;


    @GetMapping("hello")
    public String getHello(String name){
        return helloDubboApi.hello(name);
    }

}

 

api服务

就一个公共类,需要把这个服务引入发布方和调用方。

package com.wrc.api;

/**
 * @Author: wrc
 * @Classname HelloDubboApi
 * @Description TODO
 * @Date 2020/6/29 10:53
 * @Created wrc
 */
public interface HelloDubboApi {

    String hello(String name);
}

服务启动测试

springboot+zookeeper+dubbo 搭建项目_第3张图片

打开zooInspector,zk的可视化软件。

可以看到节点都注册上了

springboot+zookeeper+dubbo 搭建项目_第4张图片

用postman测试

springboot+zookeeper+dubbo 搭建项目_第5张图片

服务调用成功

 

 

 

 

你可能感兴趣的:(springboot,分布式,zookeeper,分布式)