sentinel入门

本地测试需要本地启动本地sentinel

依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 持久化配置 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

注解

1
2
3
@SentinelResource // d进去自己看
// fallback 后被隐藏能源
// blockhandler 不管了,开摆

配置文件

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
server:
port: 10000
spring:
application:
name: sentinel-service
cloud:
sentinel:
transport:
dashboard: localhost:8080 # 需要修改
web-context-unify: false # 链路
# 自动推送配置
datasource:
flow-rule: # 随便起名字
nacos:
serverAddr: sdadgz.cn:8849
username: nacos
password: sdadgz.cn
namespase: test # 命名空间
dataId: sentinel-service-flow-rule
rule-type: flow
nacos:
username: nacos
password: sdadgz.cn
discovery:
namespace: test
server-addr: sdadgz.cn:8849
feign:
sentinel:
enabled: true # sentinel接管feign

整合gateway

1
2
3
4
5
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080

整合feign

1
2
@FeignClient(value = "demo", path = "/test", fallback = DemoServiceFeignFallback.class)
// 写一个DemoServiceFeignFallback类继承当前类(DemoServiceFeign)

配置中心 限速咯

设置持久化配置

  1. 配置文件输入spring.cloud.sentinel.datasource
  2. datasource点进去,是个map
  3. map的value(DataSourcePropertiesConfiguration)点进去
  4. NacosDataSourceProperties点进去,若干配置可自定义
  5. 他的父类AbstractDataSourceProperties点进去
  6. RuleType点进去
  7. 规则名及其类得到了
  8. nacos创建配置 x-x-flow-rule ,内容:上一步获取的rule的对象数组
  9. 配置文件datasource写入对应 nacos创建的配置的dataId

例:FlowRule

狂点shift ,然后搜索FlowRule,下表不全

Field 说明 默认值
resource 资源名,资源名是限流规则的作用对象
count 限流阈值
grade 限流阈值类型,QPS 模式(1)或并发线程数模式(0) QPS 模式
limitApp 流控针对的调用来源 default,代表不区分调用来源
strategy 调用关系限流策略:直接、关联(1)、链路(2) 根据资源本身(直接)
controlBehavior 流控效果(直接拒绝/WarmUp/匀速+排队等待),不支持按调用关系限流 直接拒绝
clusterMode 是否集群限流

strtegy关联,链路

  • 关联:我关注的人限流了我也限 (1)
  • 链路:我达到阈值我关注的人限流(2)

例:匀速,5秒超时,qps1

  • warm up 需要额外增加 warmUpPeriodSec: [s]
  • 削峰填谷 需要额外增加 maxQueueingTimeMs: [ms]
1
2
3
4
5
6
7
8
9
10
11
[
{
"resource": "/sentinel/feign",
"controlBehavior": 2,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0,
"maxQueueingTimeMs": 5000
}
]

全局捕获异常

你抄我,我抄你,这种谁都用的东西跟hello world一样

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cn.sdadgz.exception;

import cn.sdadgz.common.Result;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.fastjson.JSON;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* <p>
* 废物本物
* </p>
*
* @author sdadgz
* @since 2023/2/16 00:52:36
*/
@Component
@Slf4j
public class DefaultBlockExceptionHandler implements BlockExceptionHandler {

@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
//getRule返回资源、规则的详细信息
log.info("BlockExceptionHandler BlockException================" + e.getRule());

Result r = null;
if (e instanceof FlowException) {
r = Result.error(100, "接口被限流了");
} else if (e instanceof DegradeException) {
r = Result.error(101, "服务降级了");
} else if (e instanceof ParamFlowException) {
r = Result.error(102, "热点参数限流了");
} else if (e instanceof AuthorityException) {
r = Result.error(104, "授权规则不通过");
}

//返回Json数据
httpServletResponse.setStatus(500);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
try (PrintWriter writer = httpServletResponse.getWriter()) {
writer.write(JSON.toJSONString(r));
writer.flush();
} catch (IOException ioException) {
log.error("异常:{}", ioException.toString());
}
}
}

feign入门

接口

类名前加@FeignClient(name = "[service-name]", path = "[bashPath]")

启动项前加@EnableFeignClients

nacos入门

部署

这是全部了

nacos/nacos-server - Docker Image | Docker Hub

1
2
3
4
docker run --name nacos -e MODE=standalone -p 8849:8848 -d -e SPRING_
DATASOURCE_PLATFORM=mysql -e MYSQL_SERVICE_HOST=sdadgz.cn -e MYSQL_SERVICE_PORT=3307 -e MYSQL_SERVICE_DB_NAME=nacos -e M
YSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=sdadgz.cn -e JVM_XMS=840m -e JVM_XMX=840m -e TZ="Asia/Shanghai" -e LC_A
LL=en.UTF-8 -v /www/www.sdadgz.cn/nacos/:/home/nacos/ --network website_net nacos/nacos-server:1.4.2

mysql配置

初始化数据库

去官网找RELEASE下载,在conf里找nacos-mysql.sql

注册与发现

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--    nacos discovery   -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

yaml配置

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 8000
spring:
application:
name: demo
cloud:
nacos:
server-addr: sdadgz.cn:8849
username: nacos
password: sdadgz.cn
discovery:
namespace: 85198eed-65a8-4c00-9547-76fa3d77e973 # 手动去创建一个命名空间,name用作查看,id用作这里,手动创建多少有点不讲究啊,官网有api

启动项,别忘了soringboot依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.sdadgz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* @author sdadgz
* @since 2023/2/12
*/
@SpringBootApplication
@EnableFeignClients // 重中之重
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

feign包,来自其他服务的接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package cn.sdadgz.feign;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author sdadgz
* @since 2023/2/13
*/

@FeignClient(name = "demo-02",path = "/test")
public interface TestService {

@GetMapping("/m")
String m();

}

controller包

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
package cn.sdadgz.controller;

import cn.sdadgz.feign.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
* @author sdadgz
* @since 2023/2/12
*/

@RestController
@RequestMapping("/test")
public class TestController {

// 其他服务
@Resource
private TestService testService;

@GetMapping("/hello")
public String test(){
// 就像自己人一样调用
String m = testService.m();
return "hello world:" + m;
}

}

配置中心

依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

bootstrap.yaml,别自己造名字,别仍application里

config命名规范 *.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
nacos:
server-addr: sdadgz.cn:8849
username: nacos
password: sdadgz.cn
config:
namespace: test
file-extension: yaml # 重中之重
shared-configs: [config数组] # 低优先级 d进去自己看格式
extension-configs: [config数组] # 高优先级
application:
name: config

application.yaml

1
2
3
4
5
6
7
server:
port: 9000
spring:
cloud:
nacos:
discovery:
namespace: test

注解

  • @Value(“${[my.config]}”) // 配置中心取值
  • @RefreshScope // 让上面的值实时同步

问题

nacos负载均衡报错

自某个版本之后。。。反正需要手动加个依赖了,其他的照官网抄就完事了

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

nacos配置中心启动报错

同上,他妈的

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

安装

传统功夫,点到为止,再下去就喝茶了

v2ray官网

dockerhub v2fly

能跑的安装但是很麻烦,出了很多错误

1
2
3
4
apt update
apt install curl
curl -O https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh # 进不去?curl --help 有proxy
bash install-release.sh # 升级也是他,无需担心,就当update用

启动,关闭,验证

1
2
3
4
systemctl start v2ray
systemctl enable v2ray
systemctl status v2ray
systemctl stop v2ray

配置文件

位置:/usr/local/etc/v2ray

没找到/sbin/init目录

1
2
apt update
apt install systemd

curl 77 error 什么什么 ca-certificates 什么玩应

1
apt-get install --reinstall ca-certificates

curl: (56) OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0

不是问题,弹出来四五次但是自己消失了

妈的桥接用不了

桥接选择具体物理连接

备份虚拟机

  1. 只复制 .vmx 和 .vmdk
  2. 转移
  3. 用vmware打开
  4. 选择我已复制该虚拟机

  1. 右键
  2. 管理
  3. 克隆
  4. 深克隆

centos安装vmware tools

1
2
3
4
5
6
7
8
9
10
11
# 虚拟机 -> 重新安装VMWare tools
su
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
mkdir /www/vmware_tools
cd /www/vmware_tools
tar xzpf /mnt/cdrom/VMWareTools # 省略名字,自己按tap
./vmware-tools-distrib/vmware-insatll.pl # 同上
# 全点回车
# 启动
/usr/bin/vmware-config-tools.pl

乱码问题

powershell 命令行改 powershell.exe -NoExit chcp 65001

cmd 命令行改 cmd.exe /K chcp 65001

1675140062904

依赖问题

所有的子模块都需要web模块但是gateway排斥他

离谱的解决办法

找到优雅的方法就不用他了

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>

他妈的怎么打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--这一串全扔进去-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>

<configuration>
<archive>
<manifest>
<mainClass>[cn.sdadgz.wallpaper.Main]</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

spring-cloud父项目build爆红

整个build都删咯

必须性感,哪里红了删哪里

wireguard入门

本机部署

摆货摆货这小东西

1
apt install wireguard

配置

1
2
3
4
# /etc/sysctl.conf
net.ipv4.ip_forward = 1
# 更新
sysctl -p

生成密钥对

1
wg genkey | tee server_privatekey | wg pubkey > server_publickey

带公网的

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

# Note: Do not edit this file directly.
# Your changes will be overwritten!

# Server
[Interface]
PrivateKey = WFmgkokEDrHvuWVloTYZoxrCQMJhoDLS/Fd0s45Kuk8=
Address = 10.8.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -w -j ACCEPT; iptables -A FORWARD -o wg0 -w -j ACCEPT; iptables -t nat -w -A POSTROUTING -o eth0:1 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -w -j ACCEPT; iptables -D FORWARD -o wg0 -w -j ACCEPT; iptables -t nat -w -D POSTROUTING -o eth0:1 -j MASQUERADE


# Client: 02 (d431a095-f626-4d76-aadb-839431972424)
[Peer]
PublicKey = WVwXv5+KF9OTiO43poAjrEZNbKWoZ80L0/Ocs1SjzHY=
PresharedKey = 0SlqmDcVXTH+H+uJPlKaENJdQC6HC6c+M6OM/J8Tyg8=
AllowedIPs = 10.8.0.2/32

# Client: vm (8d1c1e5d-af34-4ceb-b5b7-a113d546650f)
[Peer]
PublicKey = VtyIRwzA00PH/otRp37jxdgKEI+IjcAQmB3V5ia6fV8=
PresharedKey = c9UxBaJexVAbK/SURcVPIE1q+nLRh2KWB7OTDqzrvVs=
AllowedIPs = 10.8.0.3/32

# Client: 嘟嘟可丶 (e707ce20-fd24-409f-820d-2ca04154bc14)
[Peer]
PublicKey = muL+tdMkxhQwXSdQUzzYXunqFkwJuyxVgOKBxHKGp0w=
PresharedKey = kCGe73ajPoaMFx80kezwOndI6hVBowP8s0sEeOEy1i8=
AllowedIPs = 10.8.0.4/32

# Client: 随便起一个就行 (1b572976-6bb3-4df1-8b0f-af36a7998fd1)
[Peer]
PublicKey = bH0IfPIP/K9HXAqxeDDOODRC7szO5hJjKWnJ6+5koEg=
PresharedKey = JLjVpnyFxh5nMbK5eM+ZcUJmkaAlaZvMJrSgEZXl2sA=
AllowedIPs = 10.8.0.5/32

不带公网的

1
2
3
4
5
6
7
8
9
10
11
12
13
[Interface]
PrivateKey = KDDZ2N1HY3F7sP85K6uULl3mzuiFUl7h8UllzVFRElY=
Address = 10.8.0.2/24
DNS = 114.114.114.114
#PostUp = iptables -A FORWARD -i wg0 -w -j ACCEPT; iptables -A FORWARD -o wg0 -w -j ACCEPT; iptables -t nat -w -A POSTROUTING -o ens33 -j MASQUERADE
#PostDown = iptables -D FORWARD -i wg0 -w -j ACCEPT; iptables -D FORWARD -o wg0 -w -j ACCEPT; iptables -t nat -w -D POSTROUTING -o ens33 -j MASQUERADE

[Peer]
PublicKey = FT+Z4CX9DP2BnxfGibDmt7mXDIcM48JSnq19mSWuNU4=
PresharedKey = c9UxBaJexVAbK/SURcVPIE1q+nLRh2KWB7OTDqzrvVs=
AllowedIPs = 10.8.0.1/24
PersistentKeepalive = 25
Endpoint = 114.115.168.220:49232

自启动

1
2
3
4
5
6
systemctl enable wg-quick@wg0
systemctl stop wg-quick@wg0
systemctl start wg-quick@wg0 # wg0代表wg0.conf文件

# 关闭自启动
systemctl disable wg-quick@wg0

这逼东西折磨我一晚上我可真是废物

以后长记性,文档写的额外配置别动,哪怕是你赋的默认值,就离谱,我也不知道为什么

使用

wireguard,一款v…,不对,内网穿透软件(😀笑)

这只是2022/12/26可以跑的,一切以dockerhub为准,下次安装去官网看老朋友文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
docker run -d \
--name=wg-easy \
-e WG_HOST=sdadgz.cn \
-e PASSWORD=123456 \
-e WG_DEFAULT_ADDRESS=10.0.8.X \
-e WG_DEFAULT_DNS=114.114.114.114 \
-e WG_ALLOWED_IPS=10.0.8.0/24 \
-e WG_PERSISTENT_KEEPALIVE=25 \
-v /root/wg-esay/config:/etc/wireguard \
-p 37:51820/udp \
-p 38:51821/tcp \
--cap-add=NET_ADMIN \
--cap-add=SYS_MODULE \
--sysctl="net.ipv4.conf.all.src_valid_mark=1" \
--sysctl="net.ipv4.ip_forward=1" \
--restart unless-stopped \
weejewel/wg-easy

# 都揉一行里
docker run -d --name=wg-esay -e WG_HOST=49.232.139.28 -e PASSWORD=123456 -e WG_PERSISTENT_KEEPALIVE=25 -v /root/wg-easy:/etc/wireguard -p 37:51820/udp -p 38:51821/tcp --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl="net.ipv4.conf.all.src_valid_mark=1" --sysctl="net.ipv4.ip_forward=1" weejewel/wg-easy

问题

/usr/bin/wg-quick: line 32: resolvconf: command not found

1
apt install resolvconf

zookeeper入门

我怎么感觉这个东西白学呢

原生命令

1
2
3
4
5
6
7
8
9
10
# 手动加 --help
ls
create
set
get
help
quit
delete
deleteall
...

curator用上的话再学

建立连接

1
2
3
// 照抄就完事了
CuratorFramework client = CuratorFrameworkFactory.builder().connectString("[ip:port]").sesionTimeoutMs([ms]).connectionTimeoutMs([ms]).retryPolicy([重试策略]).namespace("[类似工作目录]").build();
client.start();

使用

学个der,点个点儿有啥都有了

分布式锁

一共两行,用到再说,不记了

集群

dubbo入门

xml开发??

no!

注解开发??

yes!

什么叫持续的学习能力?2022新出的dubbo3又得学,他妈的

等教程完善了再说吧

config

@EnableDubbo开启dubbo

application配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
dubbo:
application:
name: [dubbo-springboot-demo-provider]
protocol:
name: [dubbo|tri]
port: [port]
registry:
id: [zk-registry]
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181

provider

service层注解改@DubboService(version = "[version]")

consumer

resource注解改@DubboReference(version = "[version]")

StreamOberver

里面有个onNext,onError,onCompleted方法