springboot分布式之整合zookeeper和dubbo

时间:2022-07-23
本文章向大家介绍springboot分布式之整合zookeeper和dubbo,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、docker安装zookeeper

sudo docker pull zookeeper:3.4.11

sudo docker run --name zoopker01 -p 2181:2181 --restart always -d zookeeper:3.4.11

sudo docker ps -a

2、新建一个空项目,再分别新建两个module,一个是provider-ticket,一个是consumer-user

建好之后建立如下目录及文件:

(1) 首先分别将相关依赖导入到provider-ticket和consumer-user的pom.xml中

        <!--引入dubbo-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!--引入zookeeper-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

(2)配置相关信息

在provider-ticker中application.properties

#提供者名称
dubbo.application.name=provider-ticket
#注册中心地址
dubbo.registry.address=zookeeper://192.168.124.22:2181
#将哪个包的服务发送出去
dubbo.scan.base-packages=com.gong.com.gong.providerticket.service

在consumer-user中application.properties

dubbo.application.name=consumer-user
dubbo.registry.address=zookeeper://192.168.124.22:2181

(3)在provider-ticker中的com.gong.providerticjet下的service.TicketService

package com.gong.providerticket.service;

public interface TicketService {
    public String getTicket();
}

service.TicketServiceImpl

package com.gong.providerticket.service;

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

@Component
@Service
public class TicketServieImpl implements TicketService {

    @Override
    public String getTicket() {
        return "囧妈";
    }
}

这里的@Service注解注意是dubbo包里面的。

在provider-ticket主入口加上EnableDubbo注解。

然后我们将com.gong.providerticket.service.TicketService拷贝一份到consumer-user下,注意包名要对应。

接着就可以进行测试了:

UserService.java

package com.gong.consumeruser.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gong.providerticket.service.TicketService;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Reference
    TicketService ticketService;

    public void hello(){
        String ticket = ticketService.getTicket();
        System.out.println("买到票了:"+ticket);
    }
}

这里的@Service是springboot中的。

最后是在ConsumerUserApplicationTests.java

package com.gong.consumeruser;

import com.gong.consumeruser.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerUserApplicationTests {

    @Autowired
    UserService userService;

    @Test
    public void contextLoads() {
        userService.hello();
    }

}

3、进行测试

首先运行provider-ticket项目。然后再运行ConsumerUserApplicationTests.java中的contextLoads方法:

在consumer-user中成功调用了provider-ticket中的TicketServiceImpl.java中的方法。