简化您的微服务开发
You might think it must be RPC end point part, which makes your business logic as a real service can be accessed from the network.
But, this is not true. By leveraging the opensource RPC packages, such as, apache thrift, gRPC, this part could be extremely easy except defining your service interface with some IDL.
The following codes are about starting a service server based on thrift.
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
serverTransport, err := thrift.NewTServerSocket(NetworkAddr)
if err != nil {
os.Exit(1)
}
processor := calculator_thrift.NewCalculatorProcessor(&calculatorHandler{})
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
server.Serve()As we all known, to build a robust and maintainable service is not an easy task. There are a lot of tricky work as the following:
Most of them are much complicated than RPC, in some cases they are even complicated than your core business logic.
For example, the code section of rate limit
...
bucket := make(chan struct{}, tokenBucketSize)
//fill the bucket firstly
for j := 0; j
}Circuit breaker is the essential part of fault tolerance and recovery oriented solution. Circuit breaker is to stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
Stop cascading failures. Fallbacks and graceful degradation. Fail fast and rapid recovery.
CircuitBreakDecorator would interrupt client invoking when its time spent being longer than the expectation. A timeout exception or the result coming from the degradation method will be return to client. The similar logic is also for the concurrency limit. https://github.com/easierway/service_decorators_example/blob/master/example_service_test.go#L46
AdvancedCircuitBreakDecorator is a stateful circuit breaker. Not like CircuitBreakDecorator, which each client call will invoke the service function wrapped by the decorators finally, AdvancedCircuitBreakDecorator is rarely invoked the service function when it's in "OPEN" state. Refer to the following state flow.
To use AdvancedCircuitBreakDecorator to handle the timeout and max concurrency limit, the service will be decorated by both CircuitBreakDecorator and AdvancedCircuitBreakDecorator.
Be careful: 1 AdvancedCircuitBreakDecorator should be put out of CircuitBreakDecorator to get timeout or beyond max concurrency errors. 2 To let AdvancedCircuitBreakDecorator catch the errors and process the faults, not setting the fallback methods for CircuitBreakDecorator.
According to the principles of chaos engineering, chaos engineering is “the discipline of experimenting on a distributed system in order to build confidence in the system’s capability to withstand turbulent conditions in production.”
You learn how to fix the things that often break.
You don’t learn how to fix the things that rarely break.
If something hurts, do it more often!
For today’s large scale distributed system, normally it is impossible to simulate all the cases (including the failure modes) in a nonproductive environment. Chaos engineering is about experimenting with continuous low level of breakage to make sure the system can handle the big things.
By ChaosEngineeringDecorator you can inject the failures (such as, slow response, error response) into the distributed system under control. This would help you to test and improve the resilience of your system.
The following is the configuration about ChaosEngineeringDecorator.
{
"IsToInjectChaos" : true, // Is it to start chaos injection, if it is false, all chaos injects (chaos function, slow response) will be stopped
"AdditionalResponseTime" : 500, // Inject additional time spent (milseconds) to simulate slow response.
"ChaosRate" : 40 // The proportion of the chaos response, the range is 0-100
}The configuration is stored in the storage that can be accessed with "ConfigStorage" interface.
……暂无开放 Issues,或尚未同步最近议题。