赞
踩
Dubbo是一个高性能、轻量级RPC框架,官网现在已定义为微服务框架,目的就是让消费方、生产方通过服务注册、服务发现、服务调用实现动态通信。
服务调用的实现由三层模型组成:business+rpc+remoting 好文推荐
官方介绍除了服务注册、调用,还提供可视化监听运维、服务容错、服务降级等其它扩展功能
服务流程
RPC是一个跨应用层+传输层的网络通信服务,调用者主要设置应用层里面的传输协议,配置相关属性,调用时将数据传入并通过该协议发送
RPC是跨越应用层+传输层的程序,而HTTP只是传输协议,也就是说RPC服务包含HTTP协议,当然可以是其它通信协议,如Dubbo框架,传输协议可以更换,默认是dubbo协议(基于TCP的自定义协议)
如:dubbo://192.168.0.1:20188。默认就是走dubbo协议的,单一长连接,NIO异步通信,基于hessian作为序列化协议。
适用的场景:传输数据量很小(每次请求在100kb以内),但是并发量很高的长连接。
为了要支持高并发场景,一般是服务提供者就几台机器,但是服务消费者有上百台,可能每天调用量达到上亿次!此时用长连接是最合适的,就是跟每个服务消费者维持一个长连接就可以,可能总共就100个连接。然后后面直接基于长连接NIO异步通信,可以支撑高并发请求。
否则如果上亿次请求每次都是短连接的话,服务提供者会扛不住。
而且因为走的是单一长连接,所以传输数据量太大的话,会导致并发能力降低。所以一般建议是传输数据量很小,支撑高并发访问。
走java二进制序列化,多个短连接,适合消费者和提供者数量差不多,适用于文件的传输,一般较少用
走hessian序列化协议,多个短连接,适用于提供者数量比消费者数量还多,适用于文件的传输,一般较少用
多个短连接,form表单、json序列化,适用于提供者数量比消费者多的混合传参
多个短连接,基于http协议,走SOAP文本序列化(即XML格式封装数据)
redis
长连接,走resp序列化
当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。
在服务提供者工程集群环境中某节点出现故障时保障消费者仍可以正常执行调用机制。dubbo的容错机制主要有6类:
------------------------------------------------------面试到这基本够了---------------------------------------------------
引入springboot-dubbo-interface、dubbo-spring-boot-starter、spring-boot-starter、zookeeper、zkclient
- <dependencies>
- <!-- api-->
- <dependency>
- <groupId>com.example.order</groupId>
- <artifactId>springboot-dubbo-interface</artifactId>
- </dependency>
- <!-- dubbo-->
- <dependency>
- <groupId>com.alibaba.spring.boot</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- </dependency>
- <!-- spring boot starter-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>log4j-to-slf4j</artifactId>
- <groupId>org.apache.logging.log4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.9</version>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-log4j12</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- spring.application.name=dubbo-provider
- spring.dubbo.application.id=dubbo-provider
- spring.dubbo.application.name=dubbo-provider
- spring.dubbo.registry.address=zookeeper://192.168.48.128:2181;zookeeper://192.168.48.128:2182;zookeeper://192.168.48.128:2183
- #spring.dubbo.registry.address=zookeeper://106.52.131.197:2181;zookeeper://106.52.131.197:2182;zookeeper://106.52.131.197:2183
- spring.dubbo.server=true
- @EnableDubboConfiguration
- @EnableCaching
- @SpringBootApplication
- public class ProviderBootstrap {
- public static void main(String[] args) throws IOException {
- SpringApplication.run(ProviderBootstrap.class,args);
- }
- }
- @Component
- @com.alibaba.dubbo.config.annotation.Service(timeout = 100000,interfaceClass = IUseService.class)
- public class UserServiceImpl implements IUseService {
- @Override
- public String sayHello(String name) {
- return "hello "+name;
- }
- }
引入spring-boot-starter-web、dubbo-spring-boot-starter、spring-boot-starter、zookeeper、zkclient、springboot-dubbo-interface
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.spring.boot</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>log4j-to-slf4j</artifactId>
- <groupId>org.apache.logging.log4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.9</version>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-log4j12</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.example.order</groupId>
- <artifactId>springboot-dubbo-interface</artifactId>
- </dependency>
- </dependencies>
- spring.application.name=dubbo-demo-consumer
- spring.dubbo.application.name=dubbo-demo-consumer
- spring.dubbo.application.id=dubbo-demo-consumer
- spring.dubbo.registry.address=zookeeper://106.52.131.197:2181;zookeeper://106.52.131.197:2182;zookeeper://106.52.131.197:2183
- @SpringBootApplication
- @EnableDubboConfiguration
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class,args);
- }
- }
- @RestController
- @RequestMapping("/user")
- public class UserController {
- @Reference
- private IUseService useService;
-
- @RequestMapping("/sayHello")
- public String sayHello(String name){
- return useService.sayHello(name);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。