Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from dunwu:master #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pull wants to merge 24 commits into axh2018:master
base: master
Choose a base branch
Loading
from dunwu:master
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dba117d
refactor: 项目整理
dunwu May 13, 2021
ee4ee3f
feat: 将 javatech 项目迁移至 java-tutorial 项目中
dunwu Feb 17, 2022
26da14a
fix: 修复错误链接
dunwu Apr 6, 2022
63e9b72
docs: 更新文档
dunwu Apr 8, 2022
bb458a5
build(CI): 弃用 travis-ci,改用 github actions 进行 CI
dunwu Apr 9, 2022
e14c9d1
docs: 使用 vuepress-theme-vdoing,并整理文档
dunwu Apr 9, 2022
c05920a
build: 修改在线文档配置
dunwu Apr 10, 2022
94a480e
docs: 整理文档
dunwu Apr 24, 2022
98d8700
feat: 调整示例代码
dunwu Jun 9, 2022
2c306d1
feat: okhttp 使用示例
dunwu Jun 10, 2022
68388af
feat: rocketmq 使用示例
dunwu Jul 8, 2022
a3b24eb
Update README.md
yellow013 Jul 14, 2022
18ff447
Merge pull request #32 from yellow013/master
dunwu Jul 17, 2022
74afe2b
feat: 添加单元测试示例
dunwu Jul 28, 2022
7af3679
codes: 更新示例代码
dunwu Jul 29, 2022
43c2271
docs: 更新文档
dunwu Feb 10, 2023
9ef8de3
feat: 修改配置
dunwu Aug 18, 2023
aad3a98
feat: 修改 package
dunwu Jan 22, 2024
70b3983
feat: 限流算法示例
dunwu Jan 22, 2024
c946f6a
feat: 分布式限流算法示例
dunwu Jan 24, 2024
8934acd
build: 修改构建方式
dunwu Jan 24, 2024
d2f55e8
feat: 整理图片
dunwu Jan 27, 2024
2b38ba8
refactor: 整理项目
dunwu Jan 29, 2024
973d07a
feat: 分布式示例
dunwu Dec 31, 2024
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
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compact": false
}
1 change: 1 addition & 0 deletions .gitattributes
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop

This file was deleted.

250 changes: 197 additions & 53 deletions README.md
View file Open in desktop

Large diffs are not rendered by default.

Binary file removed assets/Java业务问题.xmind
View file Open in desktop
Binary file not shown.
Binary file removed assets/Java性能调优.xmind
View file Open in desktop
Binary file not shown.
Binary file removed assets/TroubleShooting.eddx
View file Open in desktop
Binary file not shown.
Binary file removed assets/TroubleShooting.xmind
View file Open in desktop
Binary file not shown.
Binary file removed assets/javatool/monitor/监控工具.eddx
View file Open in desktop
Binary file not shown.
Binary file removed assets/javatool/monitor/监控工具比对.xlsx
View file Open in desktop
Binary file not shown.
Binary file removed assets/tomcat.eddx
View file Open in desktop
Binary file not shown.
52 changes: 52 additions & 0 deletions codes/java-distributed/java-distributed-id/pom.xml
View file Open in desktop
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /