dubbo+zookeeper

dubbo+zookeeper

Architecture

下载

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();
}
原文地址:https://www.cnblogs.com/pinked/p/12397435.html