Skip to content

Latest commit

 

History

History
236 lines (192 loc) · 15.6 KB

README_EN.md

File metadata and controls

236 lines (192 loc) · 15.6 KB

smart cloud

build codecov license Latest release EN doc CN doc

features

A scaffolding based on spring cloud implementation. The implemented functions are as follows:

See the example project: smart-cloud-examples

Module description

smart-cloud
├──smart-api-core -- Interface permissions, encryption and decryption, signature and other annotations && user context
│  ├──smart-api-annotation -- Annotation for interface permissions, encryption and decryption, signature, etc.
│  └──smart-user-context -- user context
├──smart-code-generate -- code generation
├──smart-common-pojo -- public object( VO,DTO,BO,DO, etc.)
├──smart-common-web -- web module public processing
├──smart-constants -- constant module
├──smart-exception -- exception module
├──smart-mask -- Sensitive data obfuscation
├──smart-test -- test package
│  ├──smart-cloud-starter-test -- test package
│  ├──smart-cloud-starter-test-mock-rabbitmq -- test rabbitmq module package
│  ├──smart-cloud-starter-test-mock-redis -- test redis module package
│  └──smart-cloud-test-core -- test unit test, integration test package
├──smart-utility -- Tools
└──smart-could-starter -- frame encapsulation
   ├──smart-cloud-starter-api-version -- Interface multi-version control
   ├──smart-cloud-starter-configure -- Framework configuration property encapsulation
   ├──smart-cloud-starter-core -- Framework core (custom annotations, exception encapsulation, request and response public parameters, business-related tool classes)
   ├──smart-cloud-starter-elasticsearch-dynamic-datasource -- elasticsearch dynamic data source
   ├──smart-cloud-starter-feign -- feign package (removable and reusable, faceted)
   ├──smart-cloud-starter-global-id -- distributed id generator
   ├──smart-cloud-starter-job -- Scheduled task
   ├──smart-cloud-starter-locale -- international
   ├──smart-cloud-starter-log4j2 -- Log encapsulation (log4j2 encapsulation, support desensitization of log sensitive data)
   ├──smart-cloud-starter-log4j2 -- Log encapsulation (log4j2 log printing, support log sensitive data desensitization)
   ├──smart-cloud-starter-logback -- Log encapsulation (logback log printing, support log sensitive data desensitization)
   ├──smart-cloud-starter-method-log -- 方Method Aspect Log Encapsulation
   ├──smart-cloud-starter-mock -- Mock encapsulation (mock tool classes, common mock strategies, request interface mock interceptors)
   ├──smart-cloud-starter-monitor -- Monitor service by spring boot admin, send warning message by WXWork
   ├──smart-cloud-starter-mp-shardingjdbc -- mybatis plus, dynamic-datasource, shardingjdbc package (supports multiple data sources, sub-database sub-tables; mapper tool class)
   ├──smart-cloud-starter-mybatis-plus -- mybatis plus, dynamic-datasource encapsulation (supports multiple data sources; mapper tool class, table privacy field encryption and decryption, etc.)
   ├──smart-cloud-starter-rabbitmq -- rabbitmq encapsulation (consumption failure, automatically put into the delayed queue for re-consumption)
   ├──smart-cloud-starter-rate-limit -- api rate limit
   ├──smart-cloud-starter-redis -- redis encapsulation, custom distributed lock annotation
   └──smart-cloud-starter-web -- web encapsulation (log aspect, exception handling, parameter verification)
   └──smart-cloud-starter-monitor -- Service monitoring, interface monitoring
      ├──smart-cloud-monitor-common -- Monitor common code
      ├──smart-cloud-starter-monitor-api -- api exception monitoring
      └──smart-cloud-starter-monitor-admin -- spring boot admin monitoring(Service status, gc, thread count, memory, cpu, tomcat, etc.), wework notifications

technology stack

name role description
spring boot scaffolding
spring cloud gateway service gateway
nacos Service registration and configuration center
spring boot admin service monitoring
openfeign Declarative service invocation(RPC)
sleuthlog4j2 Link tracking, logs
mybatismybatis plus ORM
dynamic-datasource Multiple data sources
sharding jdbc Sub-library and sub-table
redisembedded-redis Caching, Integration Testing
sentinel Current limit, fuse, downgrade
rabbitmq message queue
fastdfs Distributed file storage
xxl-job Distributed timing tasks
easyexcel excel import and export
Hibernator-Validator parameter verification
mockitopodam Unit testing, data mocking
h2 database integration test
embedded-redis redis integration test
rabbitmq-mock rabbitmq integration test
embedded-elasticsearch elasticsearch integration test
freemarker code template generation
yapiidea yapi upload plugin api documentation
jasypt-spring-boot Encryption and decryption of sensitive data in configuration files
Lombok Simplified code

Principles of Service Merger

  • To merge services, you only need to modify pom.xml and import the services to be merged.
  • rpc interface through custom annotationSmartFeignClientaccomplish. When a single service is deployed alone, FeignClient will take effect; when the service provider and service consumer are deployed together, the FeignClient annotation will be invalid, and the rpc interface will be called directly through the implementation class object. For specific logic, seeSmartFeignClientCondition

Related instructions

Problems encountered with service mergers

A single service is introduced into the merged service through maven in the form of a jar. In a single service, the feign interface requests through http; after the services are merged, the feign interface communicates through internal processes.

Bean name conflict

After different services are merged together, there will be a problem of bean name conflict. Resolved by custom bean name generation rules.

rpc conflicts with rpc implementation class

Custom conditional annotations encapsulate FeignClient. When it is used as a single service, rpc uses feign; when it is combined service, rpc uses internal process communication.

Automatic loading of yaml files

Custom annotation YamlScan, used to load the configured yaml file (supports regular matching). Through the SPI mechanism, the implementation class of EnvironmentPostProcessor is added to the spring.factories file, and the information of the startup class is obtained through its method parameter SpringApplication, thereby obtaining the information of the yaml file configured by the YamlScan annotation. Then add the yaml file to the ConfigurableEnvironment.

Startup class annotation conflict

The custom condition annotation SmartSpringCloudApplicationCondition will only make the startup annotation marked by the startup class take effect.

maven packaging exception

When the combined service is packaged, the packages that the single service depends on are also entered into the single service jar. Solved by maven profiles

Desensitization of log data

1. Cut in from the log side, customize the label, and perform desensitization processing when printing the log;
2. Customize the serializer of jackson; when printing the log, use the customized serializer;
3. Obtain the MaskRule annotation information of the log incoming parameters through reflection, and finally intercept and replace the string according to the annotation rules.

Service monitor

Monitoring the service status through springboot admin, monitoring the interface abnormality in the service through extending the actuator interface, and notifying the WXWork.

  • When the service is offline, check the gitlab job record. If there is no tag job running record in the last half hour (configurable), the enterprise WeChat will be notified by @ the specified reminder person.
  • Customize the interface aspect, calculate the interface failure rate within a simple sliding window time. When springboot admin queries through actuator/health, if some interfaces exceed the failure rate threshold, the service health check will be returned.

1.Service Offline Notification

2.Service Health Check Failed

3.Service up

Interface mock data

The interface can obtain all the information of the returned object through reflection, and then randomly generate data according to the attribute type of the object; for the data required by specific requirements, you can formulate mock rules to generate data in a specified format.

Test

Unit Test

Improve test coverage with unit testing.

Integration Testing

  • Under integration tests, turn off nacos to reduce dependencies.
  • Depends on the service rpc interface, go baffle through mockito.
  • The redis layer uses embedded-redis for integration testing.
  • rabbitmq layer uses rabbitmq-mock for integration testing
  • The database layer uses h2 for integration testing (there are two other schemes: scheme 1 restores the modification of the DB by the test case through transaction rollback; scheme 2 deletes the relevant table before the test case is executed).

System Test

api documentation

The interface documentation is automatically generated by the following steps:

Through the idea yapi upload plugin plugin, upload to yapi server

Interface document renderings

summary

Details

Error code description

module code message
smart-constants 200 success
smart-constants 101 Verification failed
smart-constants 102 Data does not exist
smart-constants 103 Data already exists
smart-constants 400 Signature error
smart-constants 401 Unauthorized access
smart-constants 404 request url error
smart-constants 408 Request timed out
smart-constants 409 duplicate commit
smart-constants 412 Incomplete parameters
smart-constants 413 Attribute not configured
smart-constants 415 Request method not supported
smart-constants 416 Request type not supported
smart-constants 417 Failed to acquire lock
smart-constants 418 Upload file size exceeds limit
smart-constants 419 The current session has expired, please log in again
smart-constants 420 json parsing of the input parameter is abnormal
smart-constants 421 Interface access is too frequent, please try again later
smart-constants 500 Server exception
smart-constants 501 Failed to get Request
smart-constants 502 Failed to get Response
smart-constants 503 rpc request failed
smart-cloud-starter-web 2001 The parameter object to be verified cannot be null
smart-cloud-starter-elasticsearch-dynamic-datasource 3001 elasticsearch ds key does not exist
smart-cloud-starter-elasticsearch-dynamic-datasource 3002 elasticsearch data source not found
smart-cloud-starter-elasticsearch-dynamic-datasource 3003 elasticsearch dynamic data source properties are not configured