微服务-dubbo工程案例搭建

基础案例搭建

1 依赖

  • 父工程POM
xml 复制代码
<dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-alibaba-dependencies</artifactId>
             <version>${com.alibaba.cloud.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>${com.cloud.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-dubbo -->
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-dubbo</artifactId>
             <version>${com.dubbo.version}</version>
         </dependency>
     </dependencies>
 </dependencyManagement>
  • 当前工程POM provider + consumer 都是这个POM
xml 复制代码
<dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-dubbo</artifactId>
     </dependency>
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>com.liyong.learn</groupId>
         <artifactId>dubbo-Facde</artifactId>
         <version>1.0-SNAPSHOT</version>
     </dependency>

这里的dubbo-Facde是一个单独的模块,提供了公共的接口,打包为jar引入即可:

2 配置(选用nacos作为注册中心)

  • provider
yml 复制代码
server:
  port: 9070
spring:
  application:
    name: dubbo-provider
  cloud:
    nacos:
      discovery:
        server-addr: 111.229.199.181:8848
dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: 29070 
  registry:
    address: nacos://111.229.199.181:8848
  • consumer
yml 复制代码
server:
  port: 9075
spring:
  application:
    name: dubbo-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 111.229.199.181:8848
dubbo:
  application:
    name: dubbo-consumer
  protocol:
    name: dubbo
    port: 29075 
  registry:
    address: nacos://111.229.199.181:8848

3 使用

  • provider
java 复制代码
@DubboService
public class DubboDemoServiceImpl implements DubboDemoService {
    @Override
    public String dubboDemo() {
        return "dubboDemo go on !!!!!!";
    }
}
  • consumer
java 复制代码
@Component
public class InvokeService {
    @DubboReference
    private DubboDemoService dubboDemoService;

    public String echo() {
        return dubboDemoService.dubboDemo();
    }
}
  • 启动类
java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

依赖检查&负载均衡

1 依赖检查

所谓的依赖检查也就是,检查提供方是否提供了对应的服务,如果没有则消费方是会启动报错的。

可以通过注解里面的参数控制:

java 复制代码
// 关闭检查,没有提供方也能正常启动
@DubboReference(check = false); 

check设置为false后,如果没有提供方调用会抛异常,如果将来提供方正常了以后,还是可以调用成功。

2 负载均衡

默认就有负载均衡,也可以通过@DubboReference进行设置

java 复制代码
@DubboReference(check = false , loadbalance = LoadbalanceRules.RANDOM)
// 有下面这些规则可以设置
public interface LoadbalanceRules {

    /**
     *  This class select one provider from multiple providers randomly.
     **/
    String RANDOM = "random";

    /**
     *  Round robin load balance.
     **/
    String ROUND_ROBIN = "roundrobin";

    /**
     *  Filter the number of invokers with the least number of active calls and count the weights and quantities of these invokers.
     **/
    String LEAST_ACTIVE = "leastactive";

    /**
     *  Consistent Hash, requests with the same parameters are always sent to the same provider.
     **/
    String CONSISTENT_HASH = "consistenthash";

    /**
     *  Filter the number of invokers with the shortest response time of success calls and count the weights and quantities of these invokers.
     **/
    String SHORTEST_RESPONSE = "shortestresponse";

    String EMPTY = "";

}

广播调用

广播调用就是我们调用一个引用,但是其实所有节点都会执行这个方法,这个应用场景就比如我们要清空每个节点的业务缓存,或者每个节点都要记录一些日志。

官方文档广播调用

java 复制代码
//broadcast.fail.percent=20 代表了当 20% 的节点调用失败就抛出异常,不再调用其他节点。
@DubboReference(cluster = "broadcast", parameters = {"broadcast.fail.percent", "20"})

缓存

也是就是提供方返回的数据进行缓存,前提是数据不需要频繁的变动。这里需要注意缓存数据的大小适不适合做本地缓存,如果是大数据缓存那么需要整合第三方缓存,例如Redis。

官方文档:
dubbo调用结果缓存

java 复制代码
// 接口粒度
@DubboReference(cache = "lru")
private DemoService demoService;
// 方法粒度
@DubboReference(methods = {@Method(name="sayHello",cache = "lru")})
private DemoService demoService;

点对点案例搭建

点对点调用就是,我们要指定一台特定的机器来调用。例如本地和其他人联调,例如需要选择一台服务器做一些事情,不关心有几个节点。官方不推荐在生产环境中使用,所以了解即可,可能会在调试中使用到。
官网网站

java 复制代码
@DubboReference(url = "dubbo://localhost:29072")
private DemoService demoService;

泛化调用

泛化调用官方文档

相关推荐
喂完待续17 小时前
【序列晋升】28 云原生时代的消息驱动架构 Spring Cloud Stream的未来可能性
spring cloud·微服务·云原生·重构·架构·big data·序列晋升
jzzy_hony17 小时前
云原生:微服务与Serverless指南
微服务·云原生·serverless
夫子39617 小时前
OnlyOffice的高可用方案如何做
运维·架构
欧阳的棉花糖17 小时前
微前端俯瞰
微服务·前端工程化
薛定谔的算法17 小时前
手写React:从Dideact理解前端框架的核心原理
前端·react.js·架构
掘金-我是哪吒18 小时前
分布式微服务系统架构第170集:Kafka消费者并发-多节点消费-可扩展性
分布式·微服务·架构·kafka·系统架构
胡耀超20 小时前
大模型架构演进全景:从Transformer到下一代智能系统的技术路径(MoE、Mamba/SSM、混合架构)
人工智能·深度学习·ai·架构·大模型·transformer·技术趋势分析
小马哥编程1 天前
【软考架构】第七章 系统架构设计基础知识-7.2基于架构的软件开发方法:Architecture-Based Software Design,ABSD
架构·系统架构
西陵1 天前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
LQ深蹲不写BUG1 天前
微服务的保护方式以及Sentinel详解
微服务·云原生·架构