当前位置:网站首页>Trident tutorial
Trident tutorial
2022-06-23 22:10:00 【Great inventor】
tridenter Characteristics of :
- Adopt the idea of decentralization to manage clusters
- Support message multicast and unicast between clusters
- Support various load balancing strategies
- Support for multiple Leader Election algorithm
- Provide process pool / Implementation of scheduling process pool
- Built in microservice registry
- Built in flow limiting and degradation strategy for calls between multiple microservices
- Built in microservices Rest client
- built-in HTTP The service gateway
- Cluster status monitoring and alarm
- Distributed transaction management
Message multicast in a cluster is tridenter Very important function ,tridenter The bottom is through Redis(PubSub) Realize the multicast function, so as to realize the mutual discovery of applications , Then form the application cluster . Each member of the cluster supports the ability of message multicast and unicast .
utilize tridenter The ability to support message unicast ,tridenter Provides Leader Election algorithm interface , There are two built-in Leader Election algorithm , Fast Leader Election algorithm ( be based on Redis queue ) And consistent election algorithm ( be based on Paxos Algorithm )
meanwhile , utilize tridenter The ability to support message unicast ,tridenter Provides a process pool , It realizes the ability of cross process method call and method fragmentation
On the other hand ,tridenter It also provides the basic functions of microservice governance :
tridenter Bring your own registry , Using the principle of message multicast , Application is mutual discovery , Mutually registered , Therefore, each member in the cluster has a full list of members , That is, every application is a registry , It embodies the design idea of decentralization . Each member through a naming service , Between applications HTTP The ability of interfaces to call each other , It also provides various notes and Restful Configuration decouples service publishers and consumers
tridenter With gateway function , Applications can be published as gateway services independently , Can distribute HTTP Request and download tasks ( Uploading is not supported at the moment )
tridenter A variety of load balancing algorithms and current limiting degradation strategies are also built in , Users can also customize load balancing algorithms or degradation policies
tridenter Realized spring actuator Health check interface , In addition to monitoring cluster status , It also has functions such as statistical analysis of its own interface , The unified management and monitoring of the interface are preliminarily realized
therefore , be based on tridenter frame , We can also build a set similar to Spring Cloud Our microservice system
tridenter The design idea of decentralization is adopted , That is, developers do not need to know which is the current master node , Which nodes are slave nodes , More importantly, you should not explicitly define an application as the master node , This is from tridenter Adopted Leader The election algorithm determines , The default election algorithm is fast election algorithm . According to the election algorithm , Any application node in the cluster may become the master node , By default, the first application launched is the master node , But if the consistent election algorithm is used , It may be different . According to the author's description , The consistent election algorithm is not stable at present , It is recommended to use fast election algorithm in application .
Maven:
<dependency>
<groupId>com.github.paganini2008.atlantis</groupId>
<artifactId>tridenter-spring-boot-starter</artifactId>
<version>1.0-RC3</version>
</dependency>
tridenter The basic function of is to make different Spring Boot The application becomes a cluster mode , So when configuring , We will be having application.properties
Define two system variables , Otherwise, an error will be reported
spring.application.name=chaconne-management # The service name
spring.application.cluster.name=chaconne-management-cluster # Cluster name
After starting , If in Console The following information indicates that the cluster configuration is effective
2021-06-05 18:20:11 [INFO ] io.undertow - starting server: Undertow - 2.0.29.Final
2021-06-05 18:20:11 [INFO ] org.xnio - XNIO version 3.3.8.Final
2021-06-05 18:20:11 [INFO ] org.xnio.nio - XNIO NIO Implementation Version 3.3.8.Final
2021-06-05 18:20:11 [INFO ] i.a.f.t.m.ApplicationMulticastGroup - Registered candidate: {applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: false, startTime: 1622888405582, weight: 1}, Proportion: 1/12021-06-05 18:20:11 [INFO ] i.a.f.t.m.ApplicationRegistryCenter - Register application: [{applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: false, startTime: 1622888405582, weight: 1}] to ApplicationRegistryCenter2021-06-05 18:20:11 [INFO ] i.a.f.c.SerialDependencyListener - SerialDependencyHandler initialize successfully.
2021-06-05 18:20:11 [INFO ] i.a.f.t.e.ApplicationLeaderElection - This is the leader of application cluster 'chaconne-management-cluster'. Current application event type is 'indi.atlantis.framework.tridenter.election.ApplicationClusterLeaderEvent'
2021-06-05 18:20:11 [INFO ] i.a.f.t.e.ApplicationLeaderElection - Current leader: {applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: true, startTime: 1622888405582, weight: 1}2021-06-05 18:20:11 [INFO ] o.s.b.w.e.u.UndertowServletWebServer - Undertow started on port(s) 6543 (http) with context path ''
2021-06-05 18:20:12 [INFO ] i.a.f.c.m.ChaconneManagementMain - Started ChaconneManagementMain in 12.134 seconds (JVM running for 12.829)
First :
2021-06-05 18:20:11 [INFO ] i.a.f.t.m.ApplicationMulticastGroup - Registered candidate: {applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: false, startTime: 1622888405582, weight: 1}, Proportion: 1/12021-06-05 18:20:11 [INFO ] i.a.f.t.m.ApplicationRegistryCenter - Register application: [{applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: false, startTime: 1622888405582, weight: 1}] to ApplicationRegistryCenterThese two lines of logs indicate the successful registration of the message multicast group and the application registry
2021-06-05 18:20:11 [INFO ] i.a.f.t.e.ApplicationLeaderElection - This is the leader of application cluster 'chaconne-management-cluster'. Current application event type is 'indi.atlantis.framework.tridenter.election.ApplicationClusterLeaderEvent'
2021-06-05 18:20:11 [INFO ] i.a.f.t.e.ApplicationLeaderElection - Current leader: {applicationContextPath: http://192.168.159.1:6543, applicationName: chaconne-management, clusterName: chaconne-management-cluster, id: fafdc9ada3a5d1de3836b1a0ba4ef174, leader: true, startTime: 1622888405582, weight: 1}These two lines of logs represent the use of ApplicationLeaderElection The current application of the election algorithm is leader,( By default, the fast election algorithm takes the first application to start as Leader,
It's kind of similar Jgroups)
tridenter-spring-boot-starter It is a basic framework , Provides a variety of distributed capabilities , Here are some abilities :
The process of pool
Multiple applications with the same name (${spring.application.name}) It can be built into a process pool , Just as the thread pool allocates different threads to call a method , The process pool can make cross application method calls , The premise is that this method exists
Sample code :
@MultiProcessing(value = "calc", defaultValue = "11")
public int calc(int a, int b) { if (a % 3 == 0) { throw new IllegalArgumentException("a ==> " + a);}
log.info("[" + counter.incrementAndGet() + "]Port: " + port + ", Execute at: " + new Date());return a * b * 20;
}
@OnSuccess("calc") public void onSuccess(Object result, MethodInvocation invocation) { log.info("Result: " + result + ", Take: " + (System.currentTimeMillis() - invocation.getTimestamp()));}
@OnFailure("calc") public void onFailure(ThrowableProxy info, MethodInvocation invocation) { log.info("========================================"); log.error("{}", info);}
explain :
- annotation @MultiProcessing Modification methods calc, Indicates that this method is called by multiple processes
- onSuccess and onFailure Both methods are called asynchronously
Method slice
Method slicing is also called method parallel processing , In fact, each parameter of a set of parameters is distributed to different applications using the process pool , Then merge the output , And need to implement the partition rule interface , See source code :
public interface Parallelization {Object[] slice(Object argument); // section
Object merge(Object[] results); // Merge
}
Sample code :
@ParallelizingCall(value = "loop-test", usingParallelization = TestCallParallelization.class)
public long total(String arg) {// 0,1,2,3,4,5,6,7,8,9return 0L;
}
public static class TestCallParallelization implements Parallelization {@Override
public Long[] slice(Object argument) { String[] args = ((String) argument).split(",");Long[] longArray = new Long[args.length];
int i = 0;
for (String arg : args) {longArray[i++] = Long.parseLong(arg);
}
return longArray;
}
@Override
public Long merge(Object[] results) {long total = 0;
for (Object o : results) {total += ((Long) o).longValue();
}
return total;
}
}
explain :
- annotation @ParallelizingCall modification total Method , It means that this method needs to be segmented
- Parameters arg, For example, you can pass 0,1,2,3,4,5,6,7,8,9, The fragmentation rule will call slice Method takes the parameter as “,” Division , Become an array , Then convert each value to long type , And then distributed to each application for execution , It's all done , Re execution merge Method to add , somewhat MapReduce The smell of
- total Method 0, It refers to the default value returned when the parameter is empty or the method is abnormal
Micro service capabilities
Rest client
Sample code :
@RestClient(provider = "test-service")
// @RestClient(provider = "http://192.168.159.1:5050")
public interface TestRestClient { @PostMapping("/metrics/sequence/{dataType}") Map<String, Object> sequence(@PathVariable("dataType") String dataType, @RequestBody SequenceRequest sequenceRequest);}
explain :
- annotation @RestClient The modified interface indicates that this is a Http client
- In the annotations ,provider Attribute represents the service provider , It can be an application name in the cluster (${spring.application.name}), It can also be specific http Address
- Support Spring annotation (GetMapping, PostMapping, PutMapping, DeleteMapping), Besides , With notes @Api More fine-grained parameter settings can be provided
gateway
@EnableGateway
@SpringBootApplication
@ComponentScan
public class GatewayMain { public static void main(String[] args) {final int port = 9000;
System.setProperty("server.port", String.valueOf(port));SpringApplication.run(GatewayMain.class, args);
}
}
Reference notes @EnableGateway That's it ,tridenter The bottom layer of the gateway is Netty4 Realized
Configure the routing :
@Primary
@Component
public class MyRouterCustomizer extends DefaultRouterCustomizer {@Override
public void customize(RouterManager rm) {super.customize(rm);
rm.route("/my/**").provider("tester5"); rm.route("/test/baidu").url("https://www.baidu.com").resourceType(ResourceType.REDIRECT); rm.route("/test/stream").url(" https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").resourceType(ResourceType.STREAM);}
}
explain :
ResourceType Of 4 Types :
DEFAULT, Forward the request
REDIRECT, Jump
STREAM, Binary stream
FILE Save the file
Current limiting the drop
Current limiting refers to limiting current at the client , Instead of the server
Current limiting will depend on 3 Indicators :
- Response timeout rate
- Error rate
- Concurrency undefined By default , When any of these three indicators exceeds 80%, Current limiting will be triggered , Call downgrade service undefined Current limiting index statistics : RequestStatisticIndicatorundefined Degraded service interface :FallbackProviderundefined Relevant source code can be studied by yourself
Health monitoring
at present tridenter Provides 3 individual HealthIndicator Subclasses of
- ApplicationClusterHealthIndicatorundefined Displays the overall health status of the cluster
- TaskExecutorHealthIndicatorundefined Displays the health status of the cluster thread pool
- RestClientHealthIndicatorundefined Show Rest The health status of the client ( Response timeout rate , Error rate , Concurrency )
边栏推荐
- Freiburg University, Hildesheim University and other universities in Germany jointly | zero shot automl with pre trained models (zero sample automl based on pre training model)
- Leetcode algorithm interview sprint sorting algorithm theory (32)
- Nanny level anti crawling teaching, JS reverse implementation of font anti crawling
- HR SaaS is finally on the rise
- [tutorial] build librephotos using Tencent cloud lightweight application server to support photo management of face recognition!
- How to set the life cycle of API gateway
- 北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)
- Take you through the distributed file system
- Assembly deployment process
- CAD图在线Web测量工具代码实现(测量距离、面积、角度等)
猜你喜欢

Embedded development: embedded foundation -- the difference between restart and reset

How to calculate individual income tax? You know what?

Experiment 5 module, package and Library

HDLBits-&gt;Circuits-&gt;Arithmetic Circuitd-&gt;3-bit binary adder

Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon

Sending network request in wechat applet

Ten thousand words! Understand the inheritedwidget local refresh mechanism

Introduction to scikit learn machine learning practice

Polar cycle graph and polar fan graph of high order histogram

使用 Provider 改造屎一样的代码,代码量降低了2/3!
随机推荐
Error running PyUIC: Cannot start process, the working directory ‘-m PyQt5. uic. pyuic register. ui -o
KnowDA: All-in-One Knowledge Mixture Model for Data Augmentation in Few-Shot NLP(KnowDA:用于 Few-Shot NLP 中数据增强的多合一知识混合模型)
What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
BenchCLAMP:评估语义分析语言模型的基准
Installation and use of Minio
How does the API gateway intercept requests? How does the security of the API gateway reflect?
Configuring error sets using MySQL for Ubuntu 20.04.4 LTS
Icml2022 | robust task representation for off-line meta reinforcement learning based on contrastive learning
北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)
Relevant logic of transaction code MICn in SAP mm
Notepad++ installing the jsonview plug-in
Go language core 36 lectures (go language practice and application 26) -- learning notes
Acl2022 | MVR: multi view document representation for open domain retrieval
Minimisé lorsque Outlook est allumé + éteint
[proteus simulation] lcd1602+ds1307 key setting simple clock
[同源策略 - 跨域问题]
The 10th Blue Bridge Cup single chip microcomputer
CMU doctoral thesis | meta reinforcement learning through memory, 118 Pages pdf
University of North China, Berkeley University of California, etc. | Domain Adaptive Text Classification with structural Knowledge from unlabeled data
实验五 模块、包和库