Dubbo、Zookeeper、SpringBoot整合

1. Zookeeper搭建

1.1 下载 Zookeeper
1.2 在zookeeper根目录下创建data文件夹
1.3 在zookeeper的conf目录下创建zoo.config,修改里面的dataDir配置
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.

# 数据文件夹路径
dataDir=C:\Users\apache-zookeeper-3.6.2\data

# 暴露端口
clientPort=2181

# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
1.4 点击bin目录下的zkServer.cmd,启动zookeeper

在这里插入图片描述



项目结构

在这里插入图片描述

2.1. user-api 用户服务接口

在这里插入图片描述

com.lbb.model.User 用户实体类
package com.lbb.model;
import java.io.Serializable;

public class User implements Serializable {
    private Long id;

    private String name;

    private String address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}


com.lbb.service.IUserService 用户接口
package com.lbb.service;
import com.lbb.model.User;

public interface IUserService {
    User getUserInfo();
}


pom.xml Maven配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lbb</groupId>
    <artifactId>user-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>


2.2. user-service 用户服务接口实现

在这里插入图片描述

com.lbb.Application 启动类
package com.lbb;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 开启Dubbo
@EnableDubbo
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


com.lbb.service.UserService
package com.lbb.service;

import com.lbb.model.User;
import org.apache.dubbo.config.annotation.Service;

@Service
public class UserService implements IUserService {

    @Override
    public User getUserInfo() {
        User user = new User();
        user.setId(1L);
        user.setName("lbb");
        user.setAddress("上海市青浦区华新镇");
        return user;
    }
}
application.yml
server:
  port: 8081

dubbo:
  application:
    name: userService
    
  # 注册中心
  registry:
    address: zookeeper://localhost:2181
    
  # 协议信息
  protocol:
    name: dubbo
    port: 20882

pom.xml Maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lbb</groupId>
    <artifactId>user-service</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <spring-boot.version>2.3.1.RELEASE</spring-boot.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 用户服务接口定义 -->
        <dependency>
            <groupId>com.lbb</groupId>
            <artifactId>user-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- SpringBootWeb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <!-- Dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- Zookeeper相关jar包 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>
</project>



2.3. order-service 订单服务

在这里插入图片描述

com.lbb.Application
package com.lbb;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 开启Dubbo
@EnableDubbo
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

com.lbb.controller.OrderController
package com.lbb.controller;

import com.lbb.model.User;
import com.lbb.service.IUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Reference
    private IUserService userService;

    // 获取用户信息
    @GetMapping(value = "/getUserInfo")
    public User getUserInfo() {
        return this.userService.getUserInfo();
    }
}

application.yml
server:
  port: 8082

dubbo:
  application:
    name: orderService

  # 注册中心
  registry:
    address: zookeeper://localhost:2181

  # 默认消费者配置
  consumer:
    timeout: 1000
    retry: 3

pom.xml Maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lbb</groupId>
    <artifactId>order-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 用户服务 -->
        <dependency>
            <groupId>com.lbb</groupId>
            <artifactId>user-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- SpringBootWeb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>

        <!-- Dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- Zookeeper相关jar包 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>
</project>


3. 启动服务

3.1 启动 user-service
3.2 启动 order-service
3.3 请求 http://localhost:8082/getUserInfo
3.4 返回信息
{
    "id": 1,
    "name": "lbb",
    "address": "上海市青浦区华新镇"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值