文章目录
RestTemplate
RestTemplate restTemplate = new RestTemplate();
//请求头设置参数格式
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//http实体
HttpEntity<String> httpEntity = new HttpEntity<>(String jsonStr,httpHeaders);
//发送请求
restTemplate.put("url",httpEntity);
lua
*.lua
//赋值
local var1 = redis.call('[order]',KEYS[1],ARGV[1],ARGV[2]);
//载入脚本
DefaultRedisScript<Object> objectDefaultRedisScript = new DefaultRedisScript<Object>();
objectDefaultRedisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("*.lua")));
Object 0 = redisTemplate.execute(objectDefaultRedisScript, [List<> key],ARGV[1],ARGV[2]);
Amqp
pom.xml
<!--自动导的不是这个,不能自动注入amqpTemplate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
rabbitmq:
host: $[host}
port:${port}
username: ${username}
password: $[pasword}
virtual-host:${virtualHosts}
component
@Autowired
AmqpTemplate amqpTemplate;
/**
发送消息
var1: 交换机名称,使用默认交换机设置""
var2:路由key,不是DIRECT模式时绑定队列,没有写"",可以多段*.*.*(*匹配一个,#匹配多个)
var3:消息内容
*/
void function(){
amqpTemplate.convertAndSend(String var1, String var2, Object var3);
}
//接收消息
//不用交换机
@RabbitListener(queuesToDeclare = @Queue("RSA"))
//用交换机
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queueName",durable = "true"),
exchange = @Exchange(value = "exchangeName"),
key = {"routingKey"}))
public void function(String msg){}
config
//转换成json传输
@Configuration
public class RabbitConfig {
@Bean
public Jackson2JsonMessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
BCryptPasswordEncoder
component
@Component
public class encodeConfig {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
//随机安全码, yml配置${random.uuid}也可以生成String
SecureRandom secureRandom = new SecureRandom(UUID.randomUUID().toString().getBytes());
return new BCryptPasswordEncoder(10/*加密次数*/,secureRandom);
}
}
server
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
public void fuction(){
//加密
bCryptPasswordEncoder.encode(""));
}
scheduling
Application
@SpringBootApplication
//开启定时任务
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
Schedule
@Component
public class Schedule {
/**
秒 分 时 日 月 周 年
,时点 /间隔 -范围 *所有
L 日和周后表示最后一个
W 日后加表示工作日
日 和 周 其中一个要是 ?
[起始时间]/[间隔时间],之前的时间要设为0
*/
@Scheduled(cron = "")
//单位毫秒
//上一次任务完成后开始计时
@Scheduled(fixedDelay = 1000L)
//上一次任务开始后开始计时
@Scheduled(fixedRate = 1000L)
public void schedule(){}
}
application.yml
#可以防止任务执行时间太长阻塞任务执行
spring:
task:
scheduling:
pool:
size: 2
BeanUtils
复制相同的filed
BeanUtils.copyProperties(Object source, Object target);