docker网络Spring事务

This commit is contained in:
法然
2022-10-12 00:15:51 +08:00
parent 8ea2295dc5
commit b91ab7307e
19 changed files with 531 additions and 44 deletions

View File

@@ -7,6 +7,9 @@ xargs
**xargs 命令** 是给其他命令传递参数的一个过滤器也是组合多个命令的一个工具。它擅长将标准输入数据转换成命令行参数xargs 能够处理管道或者 stdin 并将其转换成特定命令的命令参数。xargs 也可以将单行或多行文本输入转换为其他格式例如多行变单行单行变多行。xargs 的默认命令是 echo空格是默认定界符。这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理换行和空白将被空格取代。xargs 是构建单行命令的重要组件之一。
* 将输入分割成多个,分别执行后续的命令。例如这里每一行都是一个文件名,相当于多个文件或者多个文本,分别执行后续的处理。
* 如果只用|管道运算符,表示将之前的命令作为一整个命令进行执行。例如返回的整体作为一个文本进行处理
### xargs 命令用法
xargs 用作替换工具,读取输入数据重新格式化后输出。

View File

@@ -115,4 +115,98 @@ public class UserService {
}
```
### 增删查改业务的实现
## 2 增删查改业务的实现
### 基本业务实现
* 增 jdbcTemplate.update()
* 删 jdbcTemplate.update()
* 改 jdbcTemplate.update()
*
* 数量返回值 jdbcTemplate.queryForObject
* 单挑记录 jdbcTemplate.queryForObject
* 多条记录 jdbcTemplate.query
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int addUser(User user) {
String sql = "insert into user values(?,?,?)";
Object[] args = {user.getUserId(),user.getUsername(),user.getUserStatus()};
int update = jdbcTemplate.update(sql,args);
System.out.println(update);
return update;
}
@Override
public void updateUser(User user) {
String sql = "update user set username=?,userstatus=? where userid=?";
Object[] args = {user.getUsername(),user.getUserStatus(),user.getUserId()};
int update = jdbcTemplate.update(sql,args);
System.out.println(update);
return ;
}
@Override
public void delete(String id) {
String sql = "delete from user where userid=?";
Object[] args = {id,};
int update = jdbcTemplate.update(sql,args);
System.out.println(update);
return ;
}
/**
* 查询返回整数
* @return
*/
@Override
public int selectCount(){
String sql = "select count(*) from user";
Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
return count;
}
/**
* 查询返回单个记录
* @param id
* @return
*/
@Override
public User findUserInfo(String id){
String sql = "select * from user where userid=?";
User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),id);
return user;
}
@Override
public List<User> findAll() {
String sql = "select * from user";
List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
return users;
}
}
```
### 批量操作
* 批量操作,依次遍历集合中的值,调用添加操作。
```java
@Override
public void batchAdd(List<Object[]> batchArgs) {
String sql = "insert into user values(?,?,?)";
int [] ints = jdbcTemplate.batchUpdate(sql,batchArgs);
System.out.println(Arrays.toString(ints));
}
```

View File

@@ -0,0 +1,234 @@
# 事务
## 1 简介
### 事务概念
事务是数据库操作最近本的单元,逻辑上一组操作,要么都成功,如果有一个失败,所有的都失败。
事务有四大特性ACID
* 原子性,不可分割
* 一致性,多个事务看到的数据是一致的
* 隔离性,多个事务不会产生影响
* 持久性,可以持久化
### 准备环境
1. 创建service和dao层的bean设计代码架构
2. 实现转账的业务逻辑(开发业务逻辑)
3. 撰写测试用例进行测试(测试代码)
## 2 事务步骤
### 操作步骤
1. 开启事务操作
2. 进行业务操作,并添加异常处理
3. 没有发生异常,提交事务。
4. 第四部 出现异常事务回滚
### Spring事务管理介绍
1. 事务添加到三层结构的service层
2. 在spring进行事务管理操作
1. 声明式事务管理。通过配置实现。
2. 编程式事务管理。需要写代码
3. 生命式事务管理
1. 注解方式
2. xml配置文件方式
4. 在Spring进行声明式事务管理底层使用AOP
5. 提供一个接口,代表事务管理。针对不同的框架提供了不同的实现类。 事务管理器
![](image/2022-10-11-20-56-50.png)
## 3 基于注解的声明式事务管理
### 步骤
1. 配置事务管理器
```
```
2. 在Spring配置文件中开启事务注解。
1. 在spring配置文件中引入事务命名空间
2. 开启事务管理器
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/tx-context.xsd">
```
```
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
```
3. 在service类上添加事务注解。或者在方法上。@Transactional.
1. 在类上,所有的类都开启事务。
2. 在方法上,在某个方法上开启事务。
```java
@Service
@Transactional
public class AccountService {
@Autowired
private AccountDao accountDao;
public void pay(){
accountDao.reduceMoney();
int i= 1/0;
accountDao.addMoney();
}
```
## 3 声明式事务管理参数配置
### propagation传播行为
propagation 传播行为。当一个事物方法,被另一个事物调用的时候。
* Required如果有事务在运行当前的方法就在这个事务内运行否则就启动一个新的事物并在自己的事务内运行。
![](image/2022-10-11-21-14-34.png)
* Required_NEW 当前方法必须启动新事物,并在他自己的事务内运行,如果有事务正在运行,应该将它挂起。(外层事务对内层事务不影响,外层事务执行失败回滚,不影响已经提交的内层事务)
![](image/2022-10-11-21-17-25.png)
* SUPPORTS 如果有事务在运行,当前的方法就在这个事务内运行,否则它可以不运行在事务中
![](image/2022-10-11-21-22-02.png)
```java
@Transactional(propagation=Propagation.REQUIRED)
```
### isolation隔离级别
事务特性:隔离性。多事务操作的时候不会产生影响。不考虑隔离性会产生一系列问题。
* 脏读,读到其他事务未提交的数据
* 不可重复读,同一个事物内读取的数据不一致,被修改
* 幻读读到另一个数据添加的数据例如30条数据在后边读的时候变成了31条。
![](image/2022-10-11-21-27-02.png)
![](image/2022-10-11-21-31-06.png)
使用不同的事务隔离级别。设置事务的隔离性,解决读的问题。
![](image/2022-10-11-21-32-13.png)
```java
@Transactional(isolation=Isolation.REPEATEABLE_READ)
```
### timeout 超时时间
* 事务需要再一定时间内进行提交,如果不提交进行回滚
* 默认超时时间为-1设置时间以秒为单位进行计算
```java
@Transactional(isolation=Isolation.REPEATEABLE_READ)
```
### ReadOnly 是否只读
* 读:查询操作
* 写:添加、修改、删除
* 默认是false表示可以查询可以添加修改删除。
* 可以修改为true只能查询。
```java
@Transactional(isolation=Isolation.REPEATEABLE_READ)
```
### rollbackFor & noRollbackFor异常回滚
* rollbackFor设置出现那些异常进行事务回滚。
* noRollbackFor设置出现哪些异常不进行回滚。
## 4 基于XML声明式事务管理
1. 在Spring配置文件中进行配置配置事务管理器
2. 配置通知
3. 配置切入点和切面
> 能够很明显地显示事务配置的原理。xml配置的好处就是对源代码没有任何入侵。只要更改代码就可以。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启组件扫描
1. 如果扫描多个包,多个包之间使用逗号隔开
2. 扫描包的上层目录-->
<context:component-scan base-package="com.ykl"></context:component-scan>
<!--引入外部属性文件-->
<!-- <context:property-path path="classpath:jdbc.properties"/>-->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- <property name="driverClassName" value="${prop.driverClass}"></property>-->
<!-- <property name="url" value="${prop.url}"></property>-->
<!-- <property name="username" value="${prop.username}"></property>-->
<!-- <property name="password" value="${prop.password}"></property>-->
<!-- </bean>-->
<!--引入数据库连接-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3310/user"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入参数-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--引入事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置通知-->
<tx:advice id="txadvice">
<!---->
<tx:attributes>
<tx:method name="pay"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.ykl.*(..)))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>
</beans>
```
## 4 完全注解开发
1. 创建配置类使用配置类替代xml配置文件
1. @Configuration
2. @Bean可以将返回值注册为bean。并且可以自动装配参数。
3. AnnotationConfigApplicationContext(Config.class)可以加载配置类。

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 648 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 244 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 322 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

View File

@@ -1,6 +1,8 @@
# Dockerfile
## 1 DockerFile介绍
## 1 DockerFile
### 构建过程
dockerfile是用来构建docker镜像文件命令参数脚本
@@ -13,7 +15,6 @@ dockerfile是用来构建docker镜像文件命令参数脚本
dockerhub官方的镜像会对应到github上一个仓库中的Dockerfile
## 2 DockerFile构建过程
### 指令
```
@@ -37,7 +38,7 @@ ENV 构建的时候设置环境变量
2. `#`表示注释
3. 每一个指令都会创建提交一个新的镜像层,并提交
### 构建过程
### DockFile的作用
![](image/2022-10-09-22-01-02.png)
dockerfile是面向开发的发布项目、做镜像需要编写dockerfile。
@@ -130,11 +131,37 @@ lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
### 实践CMD和ENTRYPOINT
1. 编写dockerfilefile
```
vim dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
```
2. 构建镜像
```
docker build -f dockerfile-cmd-test -t cmdtest
```
3. 运行镜像
```
docker run cmdtest
```
4. 测试镜像
```
发现CMD追加命令报错发现ENTRYPOINT命令追加的命令是可以执行的。
docker run cmtest -l
```
### 实践制作tomcat镜像
> 更加复杂,日后补充
## 2 发布自己的镜像
### 发布自己的镜像
### 发布自己的镜像到dockerhub
> DockeHub
@@ -147,22 +174,20 @@ lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
### 发布自己的镜像到阿里云
1. 登录阿里云
2. 申请仓库地址
3. 登录到阿里云仓库
4. docker push
### docker save & docker load
### 本地发布docker save & docker load
将镜像保存压缩包
1. 将镜像保存压缩包
将压缩包加载为镜像
2. 将压缩包加载为镜像
## 总结
![](image/2022-10-09-23-25-09.png)
### 简介
* Docker将应用和环境打包成一个镜像
@@ -212,33 +237,3 @@ create table
![](image/2022-10-07-22-43-49.png)
## 概念原理
### docker volume数据卷操作
```
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
```
* 查看所有的卷的情况
```
➜ data docker volume ls
DRIVER VOLUME NAME
local 635d632e8d79ad10168a6bd6b65ba5b67de68c38c63b619915b6d00db2bd1b4a
local 0704fe09fca1d196b4d4f1cc14141ba05bae986d03ac1209f63ed1cca3d7bd7c
local 924b3cbb61444c73191a11200c727b52f9ccc17dd27024bce61ba63a33577663
# 这里发现的,就是匿名挂载。
```
### 匿名挂载
```
# 匿名挂载
```

View File

@@ -0,0 +1,145 @@
## 1 简介
### 网络原理
* 下载iproute工具linuxip addr查看地址映射
![](image/2022-10-10-23-58-54.png)
* 容器内ip地址会进行映射@符号。docker分配的地址。
```
77: eth0@if78: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
```
* 容器、宿主机之间可以通过docker构建的docker虚拟网络进行通信相互ping通
### docker桥接原理
vethc-pair技术充当桥梁。
* 宿主机能够生成成对的地址,一端连接容器的虚拟网卡,另一端连接宿主机的虚拟网卡。
* 宿主机端的虚拟网卡们,能够构成一个局域网络。
* 宿主机的网卡能够和docker中的网卡进行一对一通信。
![](image/2022-10-10-22-33-15.png)
### 容器网络
所有容器在不指定网络的情况下使用docker0的地址做路由。
docker中的所有网络接口都是虚拟的转发效率很高。
容器删除,对应的网桥一对就没了。
![](image/2022-10-11-00-06-31.png)
## 2 补充知识
### --link名称解析
--link可以通过名称访问具体的docker
* 在docker启动的过程中早host中添加了一条路由。
* 所以只有在这个容器中访问指定的名字的时候才能pint通。
### docker network网络查看
```
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
```
* ls能够查看网卡
* inspect能够查看网卡细节的元数据信息。
![](image/2022-10-11-00-11-22.png)
## 3 自定义网络
### 网络原理
* 桥接模式bridge
* none不配置网络
* host和宿主机共享网络
* container:容器内网络联通
```
直接启动默认参数--net bridge
➜ notes git:(master) ✗ docker run -d -P --name tomcat01 --net bridge tomcat
创建容器网络
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
查看自己创建的网络
docker network inspect mynet
创建容器加入容器网络
docker run -d -P --name tomcat01 --net mynet tomcat
能够将容器加入到网络中
docker network connnet mynet tomcat01
```
## 4 实战Redis集群
![](image/2022-10-11-23-19-15.png)
```
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
for i in(1..6);
do
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v ~/mydata/redis/node-1/data:/data \
-v ~/mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done
```
## 实战SpringBoot服务
1. 开构建springboot项目
2. 打包应用
3. 编写dockerfilefile
4. 构建镜像
5. 发布运行
```
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","jar","/app.jar"]
```

16
docker/create.sh Normal file
View File

@@ -0,0 +1,16 @@
for port in $(seq 1 6); \
do \
mkdir -p ./mydata/redis/node-${port}/conf
touch ./mydata/redis/node-${port}/conf/redis.conf
cat << EOF >./mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

View File

@@ -5,4 +5,4 @@ git push git@github.com:Estom/notes.git
# 统计代码的数量
find . -name "*.java" -exec wc -l {} \;#利用exec参数
find . -name "*.java" |xargs wc -l # 利用xargs参数
find . -name "*.java" -exec wc -l {} \; | awk 'BEGIN{num=0} {num+=$1;print $1;}END{print num}'
find . -name "*.java" -exec wc -l {} \; | awk 'BEGIN{num=0} {num+=$1;print $1;}END{print num}'