Dubbo学习笔记

dubbo中文官方网站

dubbo官方文档

分布式系统及其演变历程

单一应用架构:

当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。

垂直应用架构:

当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的Web框架(MVC)是关键。

分布式服务架构:

当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提高业务复用及整合的分布式服务框架(RPC)是关键。

流动计算架构:

当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。

RPC

RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。

远程过程调用:序列化和网络通信。

特性一览

  • 面向接口代理的高性能RPC调用
  • 智能负载均衡
  • 服务自动注册与发现
  • 高度可扩展能力
  • 运行期流量调度
  • 可视化的服务治理与运维

dubbo应用架构

高性能Java RPC框架

dubbo快速启动

dubbo快速启动

dubbo实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package bean;

import java.io.Serializable;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午7:45
*/
public class UserAddress implements Serializable {
private Integer id;
private String userId;
private String userAddress;
private String consignee;
private String phoneNum;
private boolean isDefault;

public UserAddress(Integer id, String userId, String userAddress, String consignee, String phoneNum,
boolean isDefault) {
this.id = id;
this.userId = userId;
this.userAddress = userAddress;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}

@Override
public String toString() {
return "UserAddress{" +
"id=" + id +
", userId='" + userId + '\'' +
", userAddress='" + userAddress + '\'' +
", consignee='" + consignee + '\'' +
", phoneNum='" + phoneNum + '\'' +
", isDefault=" + isDefault +
'}';
}
}

1
2
3
4
5
6
7
8
9
10
package service;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午7:46
*/
public interface OrderService {
String initOrder(String userId);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package service;

import bean.UserAddress;

import java.util.List;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午7:46
*/
public interface UserService {
List<UserAddress> getUserAddressList(String userId);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package service.impl;

import bean.UserAddress;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import service.UserService;

import java.util.Arrays;
import java.util.List;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午7:49
*/
@Service
@Component
public class UserServiceImpl implements UserService {
public List<UserAddress> getUserAddressList(String userId) {
UserAddress userAddress1 = new UserAddress(1, "001", "人民广场", "100", "230189", true);
UserAddress userAddress2 = new UserAddress(2, "002", "文化公园", "101", "230190", false);
return Arrays.asList(userAddress1, userAddress2);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.dubbo.service.consumer.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午8:43
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private UserService userService;

public String initOrder(String userId) {
return userService.getUserAddressList(userId);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.dubbo.service.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author zhulongkun20@163.com
* @since 2018/12/2 下午9:00
*/
@Controller
public class OrderController {
@Autowired
private OrderService orderService;

@ResponseBody
@RequestMapping("/initOrder.do")
public String initOrder(@RequestParam("userId") String userId) {
return orderService.initOrder(userId);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dubbo.service.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class DubboDemoApplication {

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Spring boot application
spring.application.name = dubbo-provider-demo
server.port = 9090
management.port = 9091

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = N/A