Saturday, July 11, 2020

spring boot note


spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
'Hamcrest' is an anagram of 'matchers' allowing match rules to be defined declaratively.These matchers have uses in unit testing frameworks such as JUnit and jMock. Hamcrest has been included in JUnit 4 since 2012, but was omitted from Junit 5 in 2017.

虽然,Spring Boot中实现了默认的error映射,但是在实际应用中,错误页面对用户来说并不够友好,我们通常需要去实现我们自己的异常提示。
创建全局异常处理类:通过使用@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义。@ExceptionHandler用来定义函数针对的异常类型,最后将Exception对象和请求URL映射到error.html


Spring Boot 2.2.6.RELEASE原理剖析

https://blog.csdn.net/zhaodongchao1992/article/details/105215849

https://blog.csdn.net/weixin_43570367/article/details/104960677

By default, all sensitive endpoints added by actuator are secured. This includes ‘/refresh' endpoint. For simplicity, we will turn off security by updating bootstrap.properties:
management.security.enabled=falseAdditionally, starting with Spring Boot 2, actuator endpoints are not exposed by default. To make them available for access, we need to add this in an application.yml:management:
endpoints:
web:
exposure:
include: "*"
Let's start the client first and update user role from existing ‘Developer' to ‘Programmer' in the properties file on GitHub. Config server will show updated values straight away; however, the client won't. To make client see new files we just need to send an empty POST request to ‘/refresh' endpoint, which was added by actuator:
$> curl -X POST http://localhost:8080/actuator/refreshWe will get JSON file back showing updated properties:[
  "user.role"
]The user role was updated successfully and by calling ‘/refresh' endpoint. Client updated configuration without restarting.
By using Actuator, we can refresh clients. However, in the cloud environment, we would need to go to every single client and reload configuration by accessing actuator endpoint.To solve this problem, we can use Spring Cloud Bus.
By using Actuator, we can refresh clients. However, in the cloud environment, we would need to go to every single client and reload configuration by accessing actuator endpoint.
To solve this problem, we can use Spring Cloud Bus.
Now, the client will have another endpoint ‘/bus-refresh'. Calling this endpoint will cause:
  • get the latest configuration from the config server and update its configuration annotated by @RefreshScope
  • send a message to AMQP exchange informing about refresh event
  • all subscribed nodes will update their configuration as well
This way, we don't need to go to individual nodes and trigger configuration update.
We will use spring-cloud-config-monitor to monitor configuration changes and broadcast events using RabbitMQ as transport.

Customizing the Management Endpoint Paths

You can use the management.endpoints.web.base-path property to change the prefix for your management endpoint, as shown in the following example:
management.endpoints.web.base-path=/manage
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.type=oracle.jdbc.pool.OracleDataSource

System.out.println(jdbcTemplate.getDataSource().getConnection());
will print oracle.jdbc.driver.T4CConnection instead of HikariProxyConnection

JSR-303 Bean Validation

JSR-303 是 JAVA EE 6 中的一项子规范,叫做 Bean Validation,官方的参考实现是Hibernate Validator。需要注意的是此实现与 Hibernate ORM 没有任何关系。JSR 303 用于对 Java Bean 中的字段的值进行合法性验证。

默认springboot集成了 hibernate-validator,而且默认是生效的,可以直接使用。默认不支持基础类型的验证,必须配置拦截器MethodValidationPostProcessor,在Controller类上加 @Validated注解可以实现对方法参数的校验。而且默认只验证 controller 方法上的 validator 注解,而不会验证 controller 层以外的。所以,如果要在其他层使用 validator 验证的话,需要单独配置拦截器.

这里可能你产生了疑问,如果校验失败我们怎么办?不知道你是否了解springboot 统一异常处理

(通过@ControllerAdvice 声明一个全局异常类)我们可以通过统一拦截处理校验异常把上面注解中的的message信息返回给前端。值得注意的是,如果你前台传入的参数不在body中,校验失败抛出BindException异常,如果接口方法的参数有@RequstBody 注解,即参数放在body中传输,会抛出MethodArgumentNotValidException 异常,分别加以处理即可。

如果上面的不满足需要,我们还可以对hibernate-validator 进行扩展,甚至可以实现fail-fast 机制(当有错误时剩下的都不再进行校验立即失败)。


Tuesday, July 7, 2020

Kafka


1.
Kafka uses ZooKeeper so you need to first start a ZooKeeper server if you don't already have one.
Kafka comes with a command line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster.
The broker.id property is the unique and permanent name of each node in the cluster. 
2.
Kafka Streams is a client library for building mission-critical real-time applications and microservices, where the input and/or output data is stored in Kafka clusters.

Saturday, July 4, 2020

IntelliJ IDEA


IntelliJ IDEA
1. For the JUnit test, the class must be public, or there's no context to run it. IntelliJ also requires the test methods are public. However, by default when Eclipse creates the JUnit test cases, the class is not public, the methods are not public either.
2. Community version "Terminal" works perfect, however in the company Ultimate version "Terminal" doesn't work, even if IntelliJ is started with administrator.
3. IntelliJ occupies much more memories, for one single Project, the memory usage is over 1G, Eclipse only uses 200 M for the same Project
4. IntelliJ doesn't have the work space concept?

Thursday, July 2, 2020

Oracle concern


Oracle concern
1. The sequence could be duplicate when update in parallel?
2. To export huge data, index is helpful/worth?

Wednesday, July 1, 2020

Master Microservices with Spring boot and Spring cloud (udemy) note


1. logging.level.org.springframework=debug
check auto configuration report, dispatch servlet, basicErrorController, HttpMessageConvert
2.
Implement validations for RESTful service (size, past..., message="")
3.
swagger-ui.html
v2/api-docs
4. acuator
management.endpoint.web.exposure.include=*
recheck http://localhost:8080/acuator
hal browser
5. spring cloud config
@EnableConfigServer
spring.cloud.config.server.git.uri=file:///in28mins...
localhost:8888/limits-service/default
6. Spring cloud config server
application.properties:
       spring.application.name=limits-service
limits-service-dev.properties
limits-service-qa.properties
localhost:8888/limits-service/qa
7. application
bootstrap.properties:
  spring.application.name=limits-service
  spring.cloud.config.uri=http://localhost:8888/
  spring.profiles.active=dev
8. Use feign to simplifying the REST client
@EnableFeignClients
  Use ribbon for client side load balancing