Skip to content

Instantly share code, notes, and snippets.

View xialeistudio's full-sized avatar

Xia Lei xialeistudio

View GitHub Profile
@xialeistudio
xialeistudio / ec2.json
Created March 23, 2024 01:44
Influxdb2 + Telegraf1 linux metrics
[{"apiVersion":"influxdata.com/v2alpha1","kind":"Dashboard","metadata":{"name":"angry-panini-430001"},"spec":{"charts":[{"colors":[{"id":"base","name":"laser","type":"text","hex":"#00C9FF"}],"decimalPlaces":2,"height":2,"kind":"Single_Stat","name":"Uptime","queries":[{"query":"from(bucket: v.bucket)\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n |> filter(fn: (r) => r[\"_measurement\"] == \"system\")\n |> filter(fn: (r) => r[\"_field\"] == \"uptime_format\")\n |> filter(fn: (r) => r[\"host\"] == v.host)\n |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)\n |> yield(name: \"last\")"}],"staticLegend":{},"width":2},{"axes":[{"base":"10","name":"x","scale":"linear"},{"base":"10","name":"y","scale":"linear"}],"colorizeRows":true,"colors":[{"id":"rZdaE3Hfvdn9wPVg9kcwc","name":"Nineteen Eighty Four","type":"scale","hex":"#31C0F6"},{"id":"ia2P6hmznggQZ_phrnOJ3","name":"Nineteen Eighty Four","type":"scale","hex":"#A500A5"},{"id":"CL06ZdsYkcDj-HOHoDZRv","name":"Nineteen Eighty
public class Base62 {
private final static String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private final static int BASE = CHARS.length();
public static String encode(long number) {
var sb = new StringBuilder();
while (number > 0) {
var ch = CHARS.charAt((int) (number % BASE));
number /= BASE;
sb.insert(0, ch);
@xialeistudio
xialeistudio / Main.java
Created May 1, 2023 04:41
atomikos distributed transaction
package com.example.jta;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@xialeistudio
xialeistudio / README.md
Last active April 30, 2023 13:45
Distributed MySQL Transaction

2PC

  1. Define Transaction Manager as TM, define Resource Manager as RM
  2. Business send request to TM to start a new transaction
  3. TM send msg to RM to prepare a transaction
  4. if all RM response ok, go to next step; if any RM fails, TM will send rollback to all RM
  5. TM send commit to all RM, if any RM fails, TM will send rollback to all RM
  6. All RM commited transaction

3PC

  1. Define Transaction Manager as TM, define Resource Manager as RM
@xialeistudio
xialeistudio / README.md
Last active April 30, 2023 03:43
Procedure of Mysql Transaction

Example

An example of A transfer 10 yuan to B.

Before transaction

  • A: 10
  • B: 10
sequenceDiagram
@xialeistudio
xialeistudio / Main.java
Created April 29, 2023 01:19
Feign retry example
package com.example.feign;
import feign.Feign;
import feign.Headers;
import feign.Logger.JavaLogger;
import feign.Logger.Level;
import feign.Param;
import feign.Request.Options;
import feign.RequestLine;
import feign.Response;
@xialeistudio
xialeistudio / HttpRequestTest.java
Created April 28, 2023 09:42
SpringBootApplication with mock and unit test
package com.example.springbootjunitexample;
import com.example.springbootjunitexample.SpringbootJunitExampleApplication.GreetingService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@xialeistudio
xialeistudio / README.md
Created April 24, 2023 11:24
Deployment an ingress controller

Deloyment a nginx ingress

Step

  1. Create configmap to store nginx.conf, which modified the events config
  2. Apply nginx-deployment.yaml to deploy nginx pods
  3. Apply nginx-service.yaml to deploy nginx services
  4. Execute kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/cloud/deploy.yaml to deploy a nginx ingress controller, once deployed, you can access http://localhost through browser. Because there is no ingress, so any request will get a 404 Not Found
  5. Apply nginx-ingress.yaml to deploy ingress rul
@xialeistudio
xialeistudio / ProducerConsumer.java
Last active January 16, 2020 08:22
ProducerConsumer.java
package org.xialei.learn.concurrent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
// 生产者消费者例子
public class Main {
public static void main(String[] args) {
List<String> queue = new ArrayList<>();
```java
package com.ddhigh.learn;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10000, 128);