dubbo use

一. what is dubbo

  dubbo是个rpc框架,核心是远程方法调用。围绕着远程方法调用拓展了:服务发现、负载均衡、流量调度等技能。dubbo目前(2022/4/19)最新版本3.0,贼好用。

二. use dubbo 3.0

a. dubbo坐标:


    org.apache.dubbo
    dubbo
    3.0.7

dubbo会引入spring包,Java中dubbo好像不能独立使用,需要依赖于spring(也有可能可以单独使用,只是我没发现)。
b. 注册中心:dubbo官网推荐用zk(稳定版:3.6.3),zk客户端:


    org.apache.curator
    curator-recipes
    4.2.0


    org.apache.curator
    curator-framework
    4.2.0


    org.apache.curator
    curator-x-discovery
    4.2.0

不管是provider,还是consumer,导的包都是上边两个。


三. code

a. 配置provider

# 服务名称
dubbo.application.name=order-service
# 通信协议
dubbo.protocol.name=dubbo
# 通信端口
dubbo.protocol.port=20880
# zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 禁用qos,或者修改它占用的端口(不然报错影响心情)
dubbo.application.qosEnable=false
# 以上是最基础的配置
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:dubbo.properties")
@EnableDubbo(scanBasePackages = "a.b.service")
public class ProductConfig {
}
import a.b.ProductService;
import a.b.entity.Product;
import org.apache.dubbo.config.annotation.DubboService;

// 暴露服务
@DubboService(group = "dev", version = "1.0.0", retries = 0)
public class ProductServiceImpl implements ProductService {
    @Override
    public Product getProduct() {
        return new Product("xx01", "西瓜", 1);
    }
}
import a.b.config.ProductConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.concurrent.CountDownLatch;

public class ProductBootstrap {
    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(ProductConfig.class);
        context.start();
        System.out.println(context.getBean(ProductService.class).getProduct());
        new CountDownLatch(1).await();
    }
}

b. 配置consumer

# 服务名称
dubbo.application.name=user-service
# zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 禁用qos
dubbo.application.qosEnable=false
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo.properties")
@ComponentScan(value = "a.b.service")
@EnableDubboConfig // 自动配置dubbo
public class UserConfig {
}
import a.b.ProductService;
import a.b.entity.Product;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class UserServiceImpl {

    //  引入服务
    @DubboReference(group = "dev", version = "1.0.0", retries = 0)
    private ProductService productService;

    public Product getProduct() {
        return productService.getProduct();
    }
}
import a.b.config.UserConfig;
import a.b.service.UserServiceImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class UserBootstrap {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(UserConfig.class);
        context.start();
        System.out.println(context.getBean(UserServiceImpl.class).getProduct());
    }
}

以上内容github上都有,为加深记忆我这里抄一下。

你可能感兴趣的:(dubbo use)