Dubbo integrates the simple use of SpringBoor

Dubbo use

  • In the previous chapter, I have already got a preliminary understanding of Dubbo. Now let’s start using Dubbo.

One, build a zookeeper environment

  • Zookeeper download link: http://archive.apache.org/dist/zookeeper/
  • This time using zookeeper-3.3.6 version
  1. After the download is complete, enter the conf directory
  2. Make a copy of zoo-sample.cfg first just in case
  3. Modify zoo-sample.cfg to zoo.cfg

img

  1. Enter the bin directory and right click to run as an administratorzkServer.cmd Start zookeeper server

img

  1. Can also be used zkCli.cmd Run the connection test as an administrator right click

img

Second, build the dubbo-admin panel

  • Go to GitHub to download/clone the dubbox project

download link

  • Downloaded directory structure
  • This is a maven project

If you don’t know how to use Maven, please refer to the simple use of Maven

img

  • Use the maven command to package the project

    img

mvn clean package -Dmaven.test.skip=true  #使用maven命令清理并打包 跳过测试
  • Long wait~

img

  • When BUILD SUCCESS appears, it means the packaging is successful!

  • Enter the dubbo-admin/target directory to find dubbo-admin-2.8.4.war

  • Copy the modified war package to the webapps directory of tomcat

    • It is recommended to use tomcat8
  • You must turn on zookeeper first before you can turn on dubbo-admin!

address

  • Enter account: root password root to enter the dubbo-admin panel

img

  • dubbo-admin is installed successfully!

Three, use SpringBoot to build a simple Dubbo project

Environmental preparation

  • IDEA
  • Zookeeper3.3.6
  • dubbo-admin management panel
  • maven environment

The dubbo project pays attention to layered architecture

So we find a suitable location and create an empty folder

img

  • Use IDEA to open the folder

Create a unified api interface

hello-dubbo-api

Right click in IDEA => new => Module => Maven project => select nothing => fill in the project information you want => continue to the next step

  • Create interface
package service;
/**
 *
 * <p>
 * Description:  统一对外提供服务的接口
 * </p>
 *
 * @author yufire
 * @version v1.0.0
 * @since 2020-06-11 18:36:20
 * @see service
 *
 */
public interface HelloDubboApi {
    
    

    /**
     * 测试方法
     * @param name
     * @return
     */
    String sayHi(String name);

}
  • It is also possible to install the created interface to the local maven warehouse private server
  • command
mvn clean install
  • carry out

Create a provider that provides services

hello-dubbo-provider

  • Right click => new => Module => SpringInitializr => Next step

  • Add the maven dependencies required by the provider

<!--添加统一接口的依赖-->
<dependency>
    <groupId>cn.yufire</groupId>
    <artifactId>hello-dubbo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<!--添加 dubbo与Spring Boot整合的依赖-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

<!--因为 provider项目不需要对外提供 web服务 所以普通的 starter即可-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • Create an implementation class that implements the api module interface
package cn.yufire.hellodubboprovider.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import service.HelloDubboApi;
// 注意导包  是 com.alibaba.duboo 包下的 @Service注解
@Service(interfaceClass = HelloDubboApi.class)
public class HelloDubboApiImpl implements HelloDubboApi {
    
    
    @Override
    public String sayHi(String name) {
    
    
        return "Hello Dubbo " + name;
    }
}

  • Modify the configuration file application.yml
# 配置连接注册中心的信息
dubbo:
  application:
    name: hello-dubbo-provider # 服务名称
  registry:
    address: zookeeper://zool.yufure.cn:2181 # 注册中心地址
  protocol: # 协议
    name: dubbo # 协议名称
    port: 20888 # 服务的端口号
  • Modify the startup class
package cn.yufire.hellodubboprovider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //启动 dubbo
@SpringBootApplication
public class HelloDubboProviderApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(HelloDubboProviderApplication.class, args);
    }

}
  • start up

img

  • After the startup is successful, check the dubbo-admin control panel and you will find that a provider appears

img

Create service consumers

hello-dubbo-consumer

  • Right click => new => Module => SpringInitializr => Next step

  • Add the maven dependencies required by the consumer

 <!--添加统一接口的依赖-->
<dependency>
    <groupId>cn.yufire</groupId>
    <artifactId>hello-dubbo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

<!--添加 dubbo与Spring Boot整合的依赖-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

<!--consumer项目是需要对外提供web服务的所以需要使用 web的starter-->
<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>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • Create Controller
package cn.yufire.hello.dubbo.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import service.HelloDubboApi;

@RestController
public class HelloDubboController {
    
    

    // 使用 dubbo提供的 @Reference 远程调用 注册中心的 HelloDubboApi类
    @Reference
    private HelloDubboApi helloDubboApi;

    @GetMapping("/hello")
    public String helloDubbo(String name) {
    
    
        return helloDubboApi.sayHi(name);
    }

}
  • Modify the startup class
package cn.yufire.hello.dubbo.consumer;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo // 开启Dubbo
@SpringBootApplication
public class HelloDubboConsumerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(HelloDubboConsumerApplication.class, args);
    }

}
  • Modify the configuration file
server:
  port: 8090 # consumer项目需要对外 提供web服务 避免与本地tomcat冲突 所以 改下端口号

dubbo:
  application:
    name: hello-dubbo-consumer
  registry:
    address: zookeeper://zool.yufuie.cn:2181
  protocol:
    name: dubbo
  consumer:
    timeout: 2000
  • Start and test access

Test address

img

  • The dubbo-admin control panel at this time

img

The basic operation of dubbo has been completed!

Fourth, Dubbo's load balancing

  • When the cluster load balancing, Dubbo offers a variety of balanced strategies, the default is randoma random call.

The load balancing strategy of the official website

Random LoadBalance

  • Random , set random probability by weight.
  • The probability of collision on a cross-section is high, but the larger the amount of calls, the more even the distribution, and the more even the weight is used according to the probability, which is beneficial to dynamically adjusting the provider weight.

RoundRobin LoadBalance

  • Polling , set the polling ratio according to the weight after the convention.
  • There is a problem that slow providers accumulate requests. For example, the second machine is slow, but it is not hung up. When the request is transferred to the second machine, it is stuck there. Over time, all requests are stuck in the second machine.

LeastActive LoadBalance

  • The minimum number of active calls , the same active number is random, and the active number refers to the difference between the counts before and after the call.
  • Make the slower provider receive fewer requests, because the slower the provider will have a greater difference in counts before and after the call.

ConsistentHash LoadBalance

  • Consistent Hash , requests with the same parameters are always sent to the same provider.
  • When a certain provider hangs up, the request originally sent to that provider will be spread to other providers based on the virtual node without causing drastic changes.
  • Algorithm see: http://en.wikipedia.org/wiki/Consistent_hashing
  • By default, only the first parameter Hash is used. If you want to modify it, please configure ``
  • 160 virtual nodes are used by default, if you want to modify, please configure ``

Configure load balancing

  • Open the configuration file of the provider project to add load balancing configuration
# 配置连接注册中心的信息
dubbo:
  application:
    name: hello-dubbo-provider # 服务名称
  registry:
    address: zookeeper://zool.yufire.cn:2181 # 注册中心地址
  protocol: # 协议
    name: dubbo # 协议名称
    port: 12345  # 服务的端口号
  provider: 
    loadbalance: roundrobin # 配置负载均衡  策略为: 轮询
  • Modify the HelloDubboApiImpl class
package cn.yufire.hellodubboprovider.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import service.HelloDubboApi;
// 注意导包  是 com.alibaba.duboo 包下的 @Service注解
@Service(interfaceClass = HelloDubboApi.class)
public class HelloDubboApiImpl implements HelloDubboApi {
    
    
    
    @Value("${dubbo.protocol.port}")
    private String port;
    
    @Override
    public String sayHi(String name) {
    
    
        return "Hello Dubbo " + name + " 我来自:" + port;
    }
}
  • Modify the configuration of IDEA

Top tab of idea => RUN => Edit Configurations

img

  • Restart the project
  • After starting one, modify the port number of the configuration file to start again

img

  • Test visit

img

  • At this time, you will find that the corresponding port number will change with each visit. This is the polling mechanism of dubbo.

Finish!

Guess you like

Origin blog.csdn.net/weixin_43420255/article/details/106695665