forked from dunwu/java-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
[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
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
dba117d
refactor: 项目整理
dunwu ee4ee3f
feat: 将 javatech 项目迁移至 java-tutorial 项目中
dunwu 26da14a
fix: 修复错误链接
dunwu 63e9b72
docs: 更新文档
dunwu bb458a5
build(CI): 弃用 travis-ci,改用 github actions 进行 CI
dunwu e14c9d1
docs: 使用 vuepress-theme-vdoing,并整理文档
dunwu c05920a
build: 修改在线文档配置
dunwu 94a480e
docs: 整理文档
dunwu 98d8700
feat: 调整示例代码
dunwu 2c306d1
feat: okhttp 使用示例
dunwu 68388af
feat: rocketmq 使用示例
dunwu a3b24eb
Update README.md
yellow013 18ff447
Merge pull request #32 from yellow013/master
dunwu 74afe2b
feat: 添加单元测试示例
dunwu 7af3679
codes: 更新示例代码
dunwu 43c2271
docs: 更新文档
dunwu 9ef8de3
feat: 修改配置
dunwu aad3a98
feat: 修改 package
dunwu 70b3983
feat: 限流算法示例
dunwu c946f6a
feat: 分布式限流算法示例
dunwu 8934acd
build: 修改构建方式
dunwu d2f55e8
feat: 整理图片
dunwu 2b38ba8
refactor: 整理项目
dunwu 973d07a
feat: 分布式示例
dunwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
.babelrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "compact": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| *.less text | ||
| *.sql text | ||
| *.properties text | ||
| *.md text | ||
|
|
||
| # unix style | ||
| *.sh text eol=lf | ||
|
|
||
36 changes: 36 additions & 0 deletions
.github/workflows/deploy.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
.travis.yml
Oops, something went wrong.
250 changes: 197 additions & 53 deletions
README.md
Oops, something went wrong.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
55 changes: 55 additions & 0 deletions
...a-distributed-id/src/main/java/io/github/dunwu/distributed/id/ZookeeperDistributedId.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
...-distributed-id/src/main/java/io/github/dunwu/distributed/id/ZookeeperDistributedId2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
...va-distributed/java-distributed-id/src/main/resources/scripts/fixed_window_rate_limit.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
39 changes: 39 additions & 0 deletions
...va-distributed/java-distributed-id/src/main/resources/scripts/token_bucket_rate_limit.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...ithub/dunwu/javatech/BaseLoadBalance.java → ...ub/dunwu/distributed/BaseLoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...u/javatech/ConsistentHashLoadBalance.java → ...istributed/ConsistentHashLoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...hub/dunwu/javatech/IpHashLoadBalance.java → .../dunwu/distributed/IpHashLoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...unwu/javatech/LeastActiveLoadBalance.java → ...u/distributed/LeastActiveLoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...io/github/dunwu/javatech/LoadBalance.java → ...github/dunwu/distributed/LoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
|
|
||
8 changes: 6 additions & 2 deletions
...ithub/dunwu/javatech/LoadBalanceDemo.java → ...ub/dunwu/distributed/LoadBalanceDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
...n/java/io/github/dunwu/javatech/Node.java → ...ava/io/github/dunwu/distributed/Node.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
|
|
||
2 changes: 1 addition & 1 deletion
...hub/dunwu/javatech/RandomLoadBalance.java → .../dunwu/distributed/RandomLoadBalance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.