dubbo+zookeeper

Posted pinked

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo+zookeeper相关的知识,希望对你有一定的参考价值。

dubbo+zookeeper

技术图片

下载

zookeeper

https://zookeeper.apache.org/releases.html

dubbo-admin

https://github.com/apache/dubbo-admin/tree/master

打包dubbo-admin成jar包

provider-server

依赖

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.5.7</version>
    <!--排除这个slf4j-log4j12-->
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

配置

server:
  port: 8001

dubbo:
  application:
    name: provider-server
  registry:
    address: zookeeper://127.0.0.1:2181
  scan:
    base-packages: cn.pinked.service

服务类

@Service    //用于dubbo自动注册到注册中心
@Component  //为避免与dubbo的service注解冲突,这里使用component注解注册到spring
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "电影票";
    }
}

consumer-server

依赖

  • 同上

配置

server:
  port: 8002

dubbo:
  application:
    name: consumer-server
  registry:
    address: zookeeper://127.0.0.1:2181

服务类

@Service    //这里是放入spring容器中
public class UserService {
    //去注册中心拿到服务
    @Reference  //引用    通过pom坐标或定义路径相同的接口名
    TicketService ticketService;

    public void buyTicket(){
        String ticket = ticketService.getTicket();
        System.out.println(ticket);
    }
}

测试类

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

以上是关于dubbo+zookeeper的主要内容,如果未能解决你的问题,请参考以下文章

dubbo与zookeeper的关系

9.2 SpringBoot使用Zookeeper和Dubbo

Dubbo官方的监控后台管理

Dubbo的RPC远程过程调用+Dubbo的负载均衡+Zookeeper注册中心

zookeeper和dubbo的关系[转]

zookeeper在dubbo到底起了什么作用