晒晒我家小院子

0%

介绍

  • 使用docker-slim来给docker镜像瘦身

安装

1
2
docker pull dslim/docker-slim
brew install docker-slim

使用

1
2
3
4
5
docker-slim build --from-dockerfile build/docker/Dockerfile --tag lukelau/laumonkey:release-8-g95cdcb3 .
# docker-slim build --help
docker-slim build --from-dockerfile build/docker/Dockerfile --tag lukelau/laumonkey:release-8-g95cdcb3 .
docker-slim build --target ccr.ccs.tencentyun.com/zsyltest/edu:frontend_20210818104832 --tag frontend:test --http-probe=false
2be29257fafc

注意事项

1
2
3
4
--include-path参数的使用
Docker-Slim默认压缩会把镜像里面的一些核心文件、文件夹删掉,会导致压缩后的镜像在容器中运行时报错,用--include-path参数可以解决该问题,保护路径不被剔除
# 示例
docker-slim build --http-probe=false --include-path=/var/lib/mysql-files --include-path=/var/run/mysqld mysql:5.7

画了一个脑图,记录一下当前主流的爬虫之间的对抗

对抗2

堆(heap)

  • Python没有独立的堆类型,而只有一个包含一些堆操作函数的模块,只有小顶堆,没有大顶堆。
  • 这个模块名为heapq(其中的q表示队列),它包含6个函数,其中前4个与堆操作直接相关。
  • 必须使用列表来表示堆对象本身。

常用函数

​ 函 数 描 述
​ heappush(heap, x) 将x压入堆中
​ heappop(heap) 从堆中弹出最小的元素
​ heapify(heap) 让列表具备堆特征
​ heapreplace(heap, x) 弹出最小的元素,并将x压入堆中
​ nlargest(n, iter) 返回iter中n个最大的元素
​ nsmallest(n, iter) 返回iter中n个最小的元素

堆特征(heap property)

  • 位置i处的元素总是大于位置i // 2处的元素(反过来说就是小于位置2 * i和2 * i + 1处的元素)。
  • 这是底层堆算法的基础,称为堆特征(heap property)

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import heapq
from random import shuffle

heap = list(range(10))
shuffle(heap)

heapq.heapify(heap)
print(type(heap)) # <class 'list'>
print(heap) # [0, 3, 1, 4, 7, 8, 2, 5, 6, 9]

heapq.heappush(heap, 11)
print(heap) # [0, 3, 1, 4, 7, 8, 2, 5, 6, 9, 11]

item = heapq.heappop(heap)
print(item) # 0
print(heap) # [1, 3, 2, 4, 7, 8, 11, 5, 6, 9]

items = heapq.nlargest(3, heap)
print(items) # [11, 9, 8]
print(heap) # [1, 3, 2, 4, 7, 8, 11, 5, 6, 9]

在python中使用大顶堆

  • 将push(e)改为push(-e)、pop(e)改为-pop(e)
  • 其他参考小顶堆

服务端应用-设计技巧

需求确定

1
2
3
1、需求需要考虑实际情况
2、需要明确需求,最好实现需求封闭
3、需要考虑更多需求的衍生,才能更好实现需求的封闭和开放

数据库设计

1
2
1、设计上严格按照设计范式来设计,最好达到BC范式
2、后期需求变化,增加比修改影响更小

用户注册

1
2
3
1、增加接口,判断用户名是否存在
2、增加接口,判断手机号是否存在
3、第三方登录,一定要检查同平台多端的唯一id是否一致

短信验证码

1
1、需要考虑业务情况,降低安全风险,登录和密码找回、手机号修改绑定不能混用

服务访问限制

1
1、最好限制访问,也可以做反爬虫措施

支付需求混乱

1
2
3
4
5
问题描述:公司产品很多,定价混乱
解决办法:
1、独立的一款收款应用,可生成任意产品、任意价格的收款码
2、产品名称、价格、产品描述
3、退款权交给运营,提供后台管理页面

目录结构

1
2
3
4
5
6
7
8
9
10
11
.
├── celery_tasks
│   ├── __init__.py
│   ├── config.py
│   ├── email
│   │   ├── __init__.py
│   │   └── tasks.py
│   ├── html
│   │   ├── __init__.py
│   │   └── tasks.py
│   ├── main.py

config.py

1
2
3
4
# celery配置文件

# 指定任务队列的位置
broker_url = "redis://192.168.103.210/7"

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from celery import Celery
import os

# 告诉celery 如果需要使用Django的配置文件,应该去那里加载
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")

# 1.创建celery实例对象
celery_app = Celery('project')

# 2.加载配置文件
celery_app.config_from_object('celery_tasks.config')

# 3.自动注册异步任务
celery_app.autodiscover_tasks(['celery_tasks.email', 'celery_tasks.html'])

celery_tasks/email/tasks.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from celery_tasks.main import celery_app
from django.core.mail import send_mail
from django.conf import settings


@celery_app.task(name='send_verify_email')
def send_verify_email(to_email, verify_url):
"""
发激活邮箱的邮件
:param to_email: 收件人邮箱
:param verify_url: 邮箱激活url
:return:
"""
subject = "邮箱验证" # 邮件主题/标题
html_message = '<p>尊敬的用户您好!</p>' \
'<p>您的邮箱为:%s 。请点击此链接激活您的邮箱:</p>' \
'<p><a href="%s">%s<a></p>' % (to_email, verify_url, verify_url)
# send_mail(subject:标题, message:普通邮件正文, 发件人, [收件人], html_message=超文本的邮件内容)
send_mail(subject, '', settings.EMAIL_FROM, [to_email], html_message=html_message)

运行celery

1
2
运行命令:
celery -A celery_tasks.main worker -l info

函数调用

1
2
3
4
5
6
7
8
9
10
class EmailSerializer(serializers.ModelSerializer):
"""更新邮箱序列化器"""
...
def update(self, instance, validated_data):
instance.email = validated_data.get('email')
instance.save()
verify_url = instance.generate_email_verify_url()
# 使用delay函数
send_verify_email.delay(instance.email, verify_url=verify_url)
return instance