Spring JDBCTemplate

概述

为了使JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架。
作为Spring JDBC框架的核心,JDBC模板的设计目的是为不同类型的JDBC操作提供模板方法,通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低。
可以将Spring的JdbcTemplate看作是一个小型的轻量级持久化层框架,和我们之前使用过的DBUtils风格非常接近。

Spring AOP之基于注解的AOP

基于注解AOP步骤

  1. 将目标类和切面类加到IOC容器中。@Component
  2. 告诉Spring哪个是切面类。@Aspect
  3. 在切面类中使用五个通知注解中的这些通知方法都何时运行
  4. 开启基于AOP注解功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<bean id="mathCalculator" class="xyz.lyhcc.calculate.MathCalculator"></bean>
<bean id="loggerUtils" class="xyz.lyhcc.logger.LoggerUtils"></bean>
<bean id="validAspect" class="xyz.lyhcc.logger.ValidAspect"></bean>


<aop:config>
<aop:pointcut expression="execution(* xyz.lyhcc.calculate.MathCalculator.*(int,int))" id="glabalPointcut"/>
<aop:aspect ref="loggerUtils">
<aop:before method="logStart" pointcut-ref="glabalPointcut"/>
<aop:after-returning method="logFinished" pointcut-ref="glabalPointcut" returning="result"/>
<aop:after-throwing method="logError" pointcut-ref="glabalPointcut" throwing="exception"/>
<aop:after method="logFinal" pointcut-ref="glabalPointcut"/>
<aop:around method="aroundMethod" pointcut-ref="glabalPointcut"/>
</aop:aspect>

<aop:aspect ref="validAspect">
<aop:before method="logStart" pointcut-ref="glabalPointcut"/>
<aop:after-returning method="logFinished" pointcut-ref="glabalPointcut" returning="result"/>
<aop:after-throwing method="logError" pointcut-ref="glabalPointcut" throwing="exception"/>
<aop:after method="logFinal" pointcut-ref="glabalPointcut"/>

</aop:aspect>

</aop:config>

脚下留心
注解: 快速方便
配置: 功能完善
重要的用配置,不重要的使用注解


Spring AOP引入之Java动态代理打印日志

概述

使用动态代理使得在不影响打印日志的类的情况下进行日志打印,也就解耦业务逻辑和日志打印

Spring AOP

AOP(Aspect Oriented Programming)

面向切面编程:基于OOP(Object Oriented Programing基础之上的编程思想)
指在程序运行期间,将某段代码动态的切入到指定的位置进行运行的编程方式

SpringIOC总结

IOC是一个容器,帮我们管理所有的组件

  • 依赖注入 @autowired,自动赋值
  • 某个组件要使用Spring提供的更多功能(IOC,AOP)必须加入到容器中

Kafka生产与消费

命令行测试

启动命令

1
./bin/kafka-server-start.sh ./config/server.properties &

创建topic和删除topic

  • –zookeeper ZooKeeper服务器地址
  • –partitions 分区个数
  • –topic topic-demo 指定topic名自为topic-demo
  • –replication-factor 指定副本数
  • –delete 删除指令
  • –create 创建指令
    1
    2
    3
    4
    5
    6
    7

    [root@slave1 kafka_2.11-2.2.1]# ./bin/kafka-topics.sh --zookeeper master:2181 --create --topic topic-demo --replication-factor 3 --partitions 4
    Created topic topic-demo.

    [root@slave1 kafka_2.11-2.2.1]# ./bin/kafka-topics.sh --zookeeper master:2181 --topic topic-demo --delete
    Topic topic-demo is marked for deletion.
    Note: This will have no impact if delete.topic.enable is not set to true.

    查看topic的详细信息

    1
    2
    3
    4
    5
    6
    [root@slave1 kafka_2.11-2.2.1]# ./bin/kafka-topics.sh --zookeeper master:2181 --topic topic-demo --describe 
    Topic:topic-demo PartitionCount:4 ReplicationFactor:3 Configs:
    Topic: topic-demo Partition: 0 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
    Topic: topic-demo Partition: 1 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
    Topic: topic-demo Partition: 2 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
    Topic: topic-demo Partition: 3 Leader: 2 Replicas: 2,1,3 Isr: 2,1,3

    生产者消费者

    1
    2
    3
    4
    5
    6
    7
    [root@slave1 kafka_2.11-2.2.1]# ./bin/kafka-console-producer.sh --topic topic-demo --broker-list master:9092
    >Hello, I am lyhcc.
    >

    [root@slave1 kafka_2.11-2.2.1]# ./bin/kafka-console-consumer.sh --bootstrap-server master:9092 --topic topic-demo

    Hello, I am lyhcc.

说明:命令行方式只是用来测试


Java实现生产者和消费者客户端

要往kafka中写入消息, 首先要创建一个生产者客户端并设置一些参数,然后创建消息的ProducerRecord对象,其中必须包含所要发往的主题以及消息的消息体,然后生产者客户端将消息发送出去,最后通过close方法关闭生产者客户端并回收相应的资源
消费者消费消息 首先创建一个消费者客户端实例并配置相应的参数,然后订阅主题并消费即可

生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class KafkaProducerDemo {
public static final String brokerList = "master:9092";
public static final String topic = "topic-demo";

public static void main(String[] args) {
Properties properties = new Properties();
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("bootstrap.servers",brokerList);

//1. 配置生产者客户端参数并创建kafka实例
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
//2. 构建所需要发送的信息
ProducerRecord<String,String> record = new ProducerRecord<String, String>(topic, "hello world");
//3. 发送消息
try {
producer.send(record);
} catch (Exception e) {
e.printStackTrace();
}
//4. 关闭生产者客户端
producer.close();
}
}

消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class KafkaComsumerDemo {
public static final String brokerList = "master:9092";
public static final String topic = "topic-demo";
public static final String groupId = "group.demo";

public static void main(String[] args) {
Properties properties = new Properties();
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties.put("group.id",groupId);

properties.put("bootstrap.servers", brokerList);

//1. 创建一个消费者客户端实例
KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
//2. 订阅主题
consumer.subscribe(Collections.singletonList(topic));
//3. 遍历消息
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
}
}
}
}

在编写之前需要引入Kafka相关包

1
2
3
4
5
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.2.1</version>
</dependency>

BigData/Kafka/Kafka之broker

Kafka启动过程

首先执行 ./bin/kafka-server-start.sh ./config/server.properties &
里面的最后一句 exec $base_dir/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka “$@”执行kafka.Kafka类,从里面可以发现,它使用了KafkaServerStartable,里面封装有KafkaServer,最终startup执行的是KafkaServer,KafkaServer里面的参数说明

1
2


42. Trapping Rain Water

42. Trapping Rain Water


题目地址


Vim多行注释

方法一

  1. 首先按 esc 进入命令行模式下,按下 Ctrl + v ,进入列(也叫区块)模式;
  2. 在行首使用上下键选择需要注释的多行;
  3. 按下键盘(大写) “I” 键,进入插入模式;
  4. 然后输入注释符( “//”、“#” 等);
  5. 最后按下 “Esc” 键。

方法2

1
在指令模式下,使用.,$s/^/^# (从指定行到最后一行)

Shell之脚本功能概述

需求描述:

  1. 实现一个脚本工具,该脚本提供类似supervisor功能, 可以对进程进行管理;
  2. 一键查看所有进程运行状态
  3. 单个或批量启动进程,单个或批量停止进程
  4. 提供进程分组功能,可以按组查看进行运行状态,可以按组启动或停止该组内所有进程
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×