微服务-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;

泛化调用

泛化调用官方文档

相关推荐
团儿.3 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
王彬泽4 小时前
【微服务】服务注册与发现、分布式配置管理 - Nacos
微服务·服务注册与发现·分布式配置管理
艾伦~耶格尔13 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
攸攸太上14 小时前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
_.Switch16 小时前
Python机器学习框架介绍和入门案例:Scikit-learn、TensorFlow与Keras、PyTorch
python·机器学习·架构·tensorflow·keras·scikit-learn
一直在进步的派大星17 小时前
Docker 从安装到实战
java·运维·docker·微服务·容器
神一样的老师1 天前
构建5G-TSN测试平台:架构与挑战
5g·架构
huaqianzkh1 天前
付费计量系统通用功能(13)
网络·安全·架构
Gogeof1 天前
云原生化 - 基础镜像(简约版)
微服务·云原生·基础镜像
2402_857583491 天前
新闻推荐系统:Spring Boot的架构优势
数据库·spring boot·架构