springBoot+dubbo+zookeeper如何实现分布式开发应用

发布时间:2022-03-11 12:41:13 作者:小新
来源:亿速云 阅读:184

小编给大家分享一下springBoot+dubbo+zookeeper如何实现分布式开发应用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

环境搭建

项目结构图:

springBoot+dubbo+zookeeper如何实现分布式开发应用

1.我们首先做好服务端

pom.xml

   <dependencies>
        <!--导入依赖:Dubbo + zookeeper-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--引入zkclient-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!--引入zkclient,可能会日志冲突,还需要剔除日志依赖-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.properties

server.port=8001
# 服务应用名字
dubbo.application.name=provider-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 哪些服务要被注册
dubbo.scan.base-packages=com.wlm.service

TicketService

package com.wlm.service;

public interface TicketService {
    public String getTicket();
}

TicketServiceImpl.java

package com.wlm.service;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

//zookeeper:服务注册与发现
@Service //可以被扫描到,在项目一启动就自动注册到注册中心
@Component //使用了Dubbo后尽量不要用Service注解
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "咸鱼_翻身";
    }
}

2.启动服务端

打开服务端,开启之后不要点掉,让他在后台运行:

springBoot+dubbo+zookeeper如何实现分布式开发应用

执行服务端的jar包,待会我们要监控服务端:

springBoot+dubbo+zookeeper如何实现分布式开发应用

运行上面的服务端代码,然后去游览器中查看
http://localhost:7001/

springBoot+dubbo+zookeeper如何实现分布式开发应用

3.我们再来做客户端

pom.xml

<dependencies>
        <!--导入相关的依赖-->
        <!--导入依赖:Dubbo + zookeeper-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--引入zkclient-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!--引入zkclient,可能会日志冲突,还需要剔除日志依赖-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.properties

server.port=8002
# 消费者去哪里拿服务,需要暴露自己的名字
dubbo.application.name=consumer-server
# 注册中心的地址,可以在任何电脑上!
dubbo.registry.address=zookeeper://127.0.0.1:2181

TicketService

package com.wlm.service;

public interface TicketService {
    public String getTicket();
}

UserService.java

package com.wlm.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service //放到容器中,注意区分
public class UserService {
    //想拿到provider-server提供的票,要去注册中心拿到服务
    @Reference //引用, pom坐标,可以定义路径相同的接口名
    TicketService ticketService;

    public void buyTicket(){
        String ticket = ticketService.getTicket();
        System.out.println("在注册中心拿到=>"+ticket);
    }
}

ConsumerServerApplicationTests.java

package com.wlm;

import com.wlm.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConsumerServerApplicationTests {

    @Autowired
    UserService userService;
    @Test
    void contextLoads() {
        userService.buyTicket();
    }

}

运行ConsumerServerApplicationTests.java结果:拿到服务端的”咸鱼_翻身“

springBoot+dubbo+zookeeper如何实现分布式开发应用

4.总结

前提:zookeeper服务已经开启

1.首先提供者提供服务

2.消费者如何消费

springBoot+dubbo+zookeeper如何实现分布式开发应用

看完了这篇文章,相信你对“springBoot+dubbo+zookeeper如何实现分布式开发应用”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. phonegap+jQuery Mobile开发应用
  2. 《SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用》

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot dubbo zookeeper

上一篇:C++和python如何实现单链表

下一篇:如何使用Mybatis如何实现删除多个数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》