Dubbo + Zookeeper 搭建微服务项目

项目搭建基本步骤

  • (一)搭建注册中心Zookeeper
  • (二)定义公共接口
  • (三)服务提供者
      • 1. 配置 pom.xml 文件
      • 2. 配置 application.yml
      • 3. 接口实现类
      • 4. 主启动类
  • (四)服务消费者
      • 1. 配置 pom.xml 文件
      • 2. 配置 application.yml
      • 3. 控制层
      • 4. 主启动类
  • (五)验证
  • (六)dubbo-admin搭建
      • 1. 拉取镜像
      • 2. 启动dubbo-admin容器
      • 3. 登录管理平台
      • 4. 问题记录
  • (七)高级使用
      • 7.1 负载均衡
          • 7.1.1 负载均衡接口
          • 7.1.2 自定义负载均衡策略

(一)搭建注册中心Zookeeper

  1. 从官网下载 Zookeeper ,Zookeeper 下载地址
  2. 使用 docker 启动 Zookeeper 容器
# 拉取镜像
docker pull zookeeper:3.8.0

# 运行容器
docker run --name zookeeper -p 2181:2181 --restart always -d zookeeper:3.8.0

(二)定义公共接口

public interface HelloService {
    String hello(String name);
}

(三)服务提供者

1. 配置 pom.xml 文件

<?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">
    
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.coffee</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <properties>
        <dubbo-version>3.0.0</dubbo-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.coffee</groupId>
            <artifactId>common-api</artifactId>
            <version>${project.version}</version>
        </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>

        <!--dubbo配置-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-configcenter-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-metadata-report-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-config-spring</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置 application.yml

server:
  port: 6060

dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: 20880
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181

3. 接口实现类

@DubboService
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello " + name + ", This is Dubbo + Zookeeper !";
    }
}

4. 主启动类

@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

(四)服务消费者

1. 配置 pom.xml 文件

<?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">
    
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.coffee</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

    <properties>
        <dubbo-version>3.0.0</dubbo-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.coffee</groupId>
            <artifactId>common-api</artifactId>
            <version>${project.version}</version>
        </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>

        <!--dubbo配置-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-configcenter-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-metadata-report-zookeeper</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-config-spring</artifactId>
            <version>${dubbo-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置 application.yml

server:
  port: 8686

dubbo:
  application:
    name: dubbo-consumer
  protocol:
    name: dubbo
    port: 20880
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181

3. 控制层

@RestController
public class ConsumerController {

    @DubboReference
    private HelloService helloService;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(@RequestParam("name") String name){
        return helloService.hello(name);
    }

}

4. 主启动类

@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}

(五)验证

  1. 分别启动服务的提供者和消费者
  2. 访问 http://localhost:8686/hello?name=Dubbo,页面显示(Hello Dubbo, This is Dubbo + Zookeeper !)则项目搭建成功

(六)dubbo-admin搭建

1. 拉取镜像

docker pull apache/dubbo-admin:0.4.0

2. 启动dubbo-admin容器

docker run -p 8888:8080 --name dubbo-admin  \
-e admin.registry.address=zookeeper://127.0.0.1:2181  \
-e admin.config-center=zookeeper://127.0.0.1:2181  \
-e admin.metadata-report.address=zookeeper://127.0.0.1:2181  \
--restart=always  \
-d apache/dubbo-admin:0.4.0

3. 登录管理平台

访问 http://127.0.0.1:8888,并使用账号与密码登录(默认账号与密码都是root)

4. 问题记录

a、访问 http://127.0.0.1:8888 如果出现无法访问或者查看 dubbo-admin 容器日志显示无法连接上 zookeeper,那么执行以下步骤:

  • docker inspect --format=‘{{.NetworkSettings.IPAddress}}’ [zookeeper容器ID],获取 zookeeper 容器的 IPAddress
  • 用该 IPAddress 数值替换启动 dubbo-admin 容器命令 中的 127.0.0.1,然后重新启动 dubbo-admin容器

b、查看zookeeper产生的日志,ZooKeeper的日志是序列化过的,所以需要反序列化。

# ZooKeeper 3.6版本及之后的版本,可使用该命令
zkTxnLogToolkit.sh -d 日志文件名

(七)高级使用

7.1 负载均衡

Dubbo 负载均衡策略支持用户自定义拓展,内置默认的负载均衡算法为“random”,内置五种负载均衡算法分别为:

  • random:随机,按照权重设置随机概率
  • roundrobin:轮询,按公约后的权重设置轮询比例
  • leastactive:最少活跃数,如果活跃数相同则随机调用,活跃数指调用前后计数差
  • consistenthash:一致性哈希,相同参数的请求总是发送到同一个提供者
  • shortestresponse:基于响应的时间进行计算,过滤成功调用响应时间最短的调用者数量,统计这些调用者的权重和数量(dubbo 2.7.7之后新增)
7.1.1 负载均衡接口
@SPI(RandomLoadBalance.NAME)
public interface LoadBalance {
    /**
     * select one invoker in list.
     * @param invokers   invokers.
     * @param url        refer url
     * @param invocation invocation.
     * @return selected invoker.
     */
    @Adaptive("loadbalance")
    <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
}

Dubbo + Zookeeper 搭建微服务项目_第1张图片

7.1.2 自定义负载均衡策略
  1. 继承 AbstractLoadBalance
public class CustomizeLoadBalanceRule extends AbstractLoadBalance {

    private AtomicInteger count = new AtomicInteger(0);

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int index = count.incrementAndGet() % invokers.size();
        return invokers.get(index);
    }
}
  1. 配置文件

在resource目录下,创建 META-INF/dubbo 目录,并创建名称为org.apache.dubbo.rpc.cluster.LoadBalance的文件,内容(key,value)形式,key自定义,value为自定义类的权限定类名。比如customize=com.huaweicloud.sample.config.CustomizeLoadBalanceRule

Dubbo + Zookeeper 搭建微服务项目_第2张图片
3. 使用自定义负载均衡

@DubboReference(loadbalance = "customize")
private HelloService helloService;

你可能感兴趣的:(dubbo,dubbo,java-zookeeper,zookeeper)