Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compact": false
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*.less text
*.sql text
*.properties text
*.md text

# unix style
*.sh text eol=lf
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

# 在master分支发生push事件时触发。
on:
push:
branches:
- master

env: # 设置环境变量
TZ: Asia/Shanghai # 时区(设置时区可使页面中的`最近更新时间`使用时区时间)

jobs:
build: # 自定义名称
runs-on: ubuntu-latest # 运行在虚拟机环境ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
# 使用的动作。格式:userName/repoName。作用:检出仓库,获取源码。 官方actions库:https://github.com/actions
- name: Checkout
uses: actions/checkout@master

# 指定 nodejs 版本
- name: Use Nodejs ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

# 部署
- name: Deploy
env: # 设置环境变量
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
run: npm install && npm run deploy
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ hs_err_pid*

# maven plugin temp files
.flattened-pom.xml
package-lock.json


# ------------------------------- javascript -------------------------------
# dependencies
node_modules

# temp folders
.temp
build
dist
_book
_jsdoc
.temp
.deploy*/

# temp files
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
bundle*.js
.DS_Store
Thumbs.db
db.json
book.pdf
package-lock.json


# ------------------------------- intellij -------------------------------
Expand Down
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

250 changes: 197 additions & 53 deletions README.md

Large diffs are not rendered by default.

Binary file removed assets/Java业务问题.xmind
Binary file not shown.
Binary file removed assets/Java性能调优.xmind
Binary file not shown.
Binary file removed assets/TroubleShooting.eddx
Binary file not shown.
Binary file removed assets/TroubleShooting.xmind
Binary file not shown.
Binary file removed assets/javatool/monitor/监控工具.eddx
Binary file not shown.
Binary file removed assets/javatool/monitor/监控工具比对.xlsx
Binary file not shown.
Binary file removed assets/tomcat.eddx
Binary file not shown.
52 changes: 52 additions & 0 deletions codes/java-distributed/java-distributed-id/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.dunwu.distributed</groupId>
<artifactId>java-distributed</artifactId>
<version>1.0.0</version>
</parent>

<groupId>io.github.dunwu.javatech</groupId>
<artifactId>java-distributed-id</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.dunwu.distributed.id;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;

/**
* ZK 分布式 ID
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @date 2024-12-20
*/
@Slf4j
public class ZookeeperDistributedId {

public static void main(String[] args) throws Exception {

// 获取客户端
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);

// 开启会话
client.start();

String id1 = client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath("/zkid/id_");
log.info("id: {}", id1);

String id2 = client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath("/zkid/id_");
log.info("id: {}", id2);

List<String> children = client.getChildren().forPath("/zkid");
if (CollectionUtil.isNotEmpty(children)) {
for (String child : children) {
client.delete().forPath("/zkid/" + child);
}
}
client.delete().forPath("/zkid");

// 关闭客户端
client.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.dunwu.distributed.id;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
* ZK 分布式 ID
* <p>
* 基于原子计数器生成 ID
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @date 2024-12-20
*/
@Slf4j
public class ZookeeperDistributedId2 {

public static void main(String[] args) throws Exception {

// 获取客户端
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
DistributedAtomicLong atomicLong = new DistributedAtomicLong(client, "/zkid", retryPolicy);

// 开启会话
client.start();

// 基于原子计数器生成 ID
AtomicValue<Long> id1 = atomicLong.increment();
log.info("id: {}", id1.postValue());

AtomicValue<Long> id2 = atomicLong.increment();
log.info("id: {}", id2.postValue());

// 清理节点
client.delete().forPath("/zkid");

// 关闭客户端
client.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- 缓存 Key
local key = KEYS[1]
-- 访问请求数
local permits = tonumber(ARGV[1])
-- 过期时间
local seconds = tonumber(ARGV[2])
-- 限流阈值
local limit = tonumber(ARGV[3])

-- 获取统计值
local count = tonumber(redis.call('GET', key) or "0")

if count + permits > limit then
-- 请求拒绝
return -1
else
-- 请求通过
redis.call('INCRBY', key, permits)
redis.call('EXPIRE', key, seconds)
return count + permits
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local tokenKey = KEYS[1]
local timeKey = KEYS[2]

-- 申请令牌数
local permits = tonumber(ARGV[1])
-- QPS
local qps = tonumber(ARGV[2])
-- 桶的容量
local capacity = tonumber(ARGV[3])
-- 当前时间(单位:毫秒)
local nowMillis = tonumber(ARGV[4])
-- 填满令牌桶所需要的时间
local fillTime = capacity / qps
local ttl = math.min(capacity, math.floor(fillTime * 2))

local currentTokenNum = tonumber(redis.call("GET", tokenKey))
if currentTokenNum == nil then
currentTokenNum = capacity
end

local endTimeMillis = tonumber(redis.call("GET", timeKey))
if endTimeMillis == nil then
endTimeMillis = 0
end

local gap = nowMillis - endTimeMillis
local newTokenNum = math.max(0, gap * qps / 1000)
local currentTokenNum = math.min(capacity, currentTokenNum + newTokenNum)

if currentTokenNum < permits then
-- 请求拒绝
return -1
else
-- 请求通过
local finalTokenNum = currentTokenNum - permits
redis.call("SETEX", tokenKey, ttl, finalTokenNum)
redis.call("SETEX", timeKey, ttl, nowMillis)
return finalTokenNum
end
3 changes: 1 addition & 2 deletions codes/java-distributed/java-load-balance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.dunwu.javatech</groupId>
<groupId>io.github.dunwu.distributed</groupId>
<artifactId>java-load-balance</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import cn.hutool.core.collection.CollectionUtil;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import cn.hutool.core.util.HashUtil;
import cn.hutool.core.util.StrUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.util.List;
import java.util.Random;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/**
* 负载均衡算法测试例
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.javatech;
package io.github.dunwu.distributed;

import java.util.List;
import java.util.Random;
Expand Down
Loading