0%

free - Display amount of free and used memory in the system

描述:

total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)

used Used memory (calculated as total - free - buffers - cache)

free Unused memory (MemFree and SwapFree in /proc/meminfo)

shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo)

buffers Memory used by kernel buffers (Buffers in /proc/meminfo)

cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)

buff/cache Sum of buffers and cache

available
​ Estimation of how much memory is available for starting
​ new applications, without swapping. Unlike the data
​ provided by the cache or free fields, this field takes
​ into account page cache and also that not all reclaimable
​ memory slabs will be reclaimed due to items being in use
​ (MemAvailable in /proc/meminfo, available on kernels 3.14,
​ emulated on kernels 2.6.27+, otherwise the same as free)

OPTIONS

最常用的就是 free -h

直接用free -g的话,不到1G直接舍去。 见下图

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-b, --bytes
Display the amount of memory in bytes.

-k, --kibi
Display the amount of memory in kibibytes. This is the
default.

-m, --mebi
Display the amount of memory in mebibytes.

-g, --gibi
Display the amount of memory in gibibytes.

--tebi Display the amount of memory in tebibytes.

--pebi Display the amount of memory in pebibytes.

--kilo Display the amount of memory in kilobytes. Implies --si.

--mega Display the amount of memory in megabytes. Implies --si.

--giga Display the amount of memory in gigabytes. Implies --si.

--tera Display the amount of memory in terabytes. Implies --si.

--peta Display the amount of memory in petabytes. Implies --si.

-h, --human
Show all output fields automatically scaled to shortest
three digit unit and display the units of print out.
Following units are used.

B = bytes
Ki = kibibyte
Mi = mebibyte
Gi = gibibyte
Ti = tebibyte
Pi = pebibyte

If unit is missing, and you have exbibyte of RAM or swap,
the number is in tebibytes and columns might not be
aligned with header.

-w, --wide
Switch to the wide mode. The wide mode produces lines
longer than 80 characters. In this mode buffers and cache
are reported in two separate columns.

-c, --count count
Display the result count times. Requires the -s option.

-l, --lohi
Show detailed low and high memory statistics.

-s, --seconds delay
Continuously display the result delay seconds apart. You
may actually specify any floating point number for delay
using either . or , for decimal point. usleep(3) is used
for microsecond resolution delay times.

--si Use kilo, mega, giga etc (power of 1000) instead of kibi,
mebi, gibi (power of 1024).

-t, --total
Display a line showing the column totals.

--help Print help.

-V, --version
Display version information.

  • getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String
  • getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.

@AfterReturning:

After returning advice: Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).

只有当目标代码正常返回(没有抛出异常)时,才执行拦截器代码。

在目标方法执行之后执行。由于是目标方法之后执行,所以可以获取到目标方法的返回值。该注解的 returning 属性就是用于指定接收方法返回值的变量名的。所以,被注解为后置通知的方法,除了可以包含 JoinPoint 参数外,还可以包含用于接收返回值的变量。该变量最好为 Object 类型,因为目标方法的返回值可能是任何类型。

实现方法

1
2
3
4
5
6
7
8
9
@Service
public class LoginService {

public Integer ceshi(String userId,Integer i,Double d){
System.out.println("ccccccccccccc");
return 100;
}

}

切面方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Aspect
@Component
public class LoginAspect {

@AfterReturning(value = "execution(* *..LoginService.ceshi(..))",returning = "result")
public Object loginAspect(JoinPoint jp,Integer result) throws Throwable {
Object[] args = jp.getArgs();
for(Object o:args){
System.out.println(o);
System.out.println(o.getClass());
}
return result;
}

}

测试方法

1
2
3
4
5
@Test
void contextLoads2() {
Integer r = loginService.ceshi("qwe", 111, 1.12);
System.out.println(r);
}

结果

1
2
3
4
5
6
7
8
ccccccccccccc
qwe
class java.lang.String
111
class java.lang.Integer
1.12
class java.lang.Double
100
由此也可以知道jp.getArgs()获得的参数数组是按顺序的

1
2
3
4
5
6
7
8
# 查看防火墙,添加的端口也可以看到
firewall-cmd --list-all
开启端口:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
关闭端口:
firewall-cmd --permanent --zone=public --remove-port=8080/tcp
#重启
firewall-cmd --reload

一.打jar包

为了使jar包能够直接运行,需要在pom里面加上一些配置

The plugin rewrites your manifest, and in particular it manages the Main-Class and Start-Class entries. If the defaults don’t work you have to configure the values in the Spring Boot plugin, not in the jar plugin. The Main-Class in the manifest is controlled by the layout property of the Spring Boot plugin, as shown in the following example:

意思是说:一般加上下面的就行了。如果不起作用,需要自己配置

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

自己配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class}</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

只用自己配一下启动类:

比如:org.xxx.xxx.xxxApplication

然后用maven去package一下获得jar包

二.写Dockerfile

把这个jar包放到任意一个文件夹下

在这个文件夹下创建Dockerfile文件

1
2
3
4
5
FROM java:8
VOLUME /tmp
ADD CSS-core-1.0-SNAPSHOT.jar css.jar
EXPOSE 8088
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/css.jar"]

三.创建镜像

1
docker build - 镜像名 . //Dockerfile上下文路径

镜像名必须为小写

[root@iZbp18425116ezu5mwz33wZ competition-service-system]# docker build -t CSS .
invalid argument "CSS" for "-t, --tag" flag: invalid reference format: repository name must be lowercase
See 'docker build --help'.

四.创建容器

常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
列出镜像
docker images
删除镜像
docker image rm 镜像ID //常用
查看容器状态
docker ps //查看运行的容器
docker ps –a //查看所有的容器(包含运行和退出)
启动容器

-d 后台运⾏容器
--rm 容器在启动后,执⾏完成命令或程序后就销毁
--name 给容器起⼀个⾃定义名称

docker run --rm -d --name tomcat1 -p 8080:8080 tomcat
停止容器
docker stop 容器ID
删除容器
docker rm 容器ID
进入容器
docker exec -it 容器ID(Names) bash
查看日志
docker logs 容器名称/ID

Docker定制镜像

当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改.

1.从已经创建的容器中更新镜像,并且提交这个镜像

2.使用 Dockerfile 指令来创建一个新的镜像

意义:

1、对于开发人员,可以为开发团队提供一个完全一致的开发环境

2、对于测试人员,可以直接拿开发时所构建的镜像测试。

3、对于运维人员,在部署时,可以实现快速部署、移值。

Dockerfile定制镜像

镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就都会解决。这个脚本就是 Dockerfile。

Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。

Docker overview

Docker 是一个用于开发、发布和运行应用程序的开放平台。 Docker 使您能够将应用程序与基础架构分离,以便您可以快速交付软件。 使用 Docker,您可以像管理应用程序一样管理基础设施。 通过利用 Docker 快速交付、测试和部署代码的方法,您可以显著减少编写代码和在生产中运行之间的延迟。

The Docker platform

Docker 提供了在称为容器的松散隔离环境中打包和运行应用程序的能力。 隔离和安全性允许您在给定主机上同时运行多个容器。 容器是轻量级的,包含运行应用程序所需的一切,因此您无需依赖主机上当前安装的内容。 您可以在工作时轻松共享容器,并确保与您共享的每个人都获得以相同方式工作的同一个容器。

Docker 提供工具和平台来管理容器的生命周期:

  1. 使用容器开发您的应用程序及其支持组件。
  2. 容器成为分发和测试应用程序的单元。
  3. 准备就绪后,将应用程序部署到生产环境中,作为容器或编排的服务。 无论您的生产环境是本地数据中心、云提供商还是两者的混合,这都是一样的。

What can I use Docker for?

快速、一致地交付您的应用程序

Docker 通过允许开发人员使用提供应用程序和服务的本地容器在标准化环境中工作来简化开发生命周期。Containers are great for continuous integration and continuous delivery (CI/CD) workflows.

Responsive deployment and scaling

Docker 基于容器的平台允许高度可移植的工作负载。 Docker 容器可以在开发人员的本地笔记本电脑、数据中心的物理或虚拟机、云提供商或混合环境中运行。

Docker 的可移植性和轻量级特性还使得动态管理工作负载、根据业务需求近乎实时地扩展或拆除应用程序和服务变得容易。

Running more workloads on the same hardware

Docker 是轻量级和快速的。 它为基于管理程序的虚拟机提供了一种可行且经济高效的替代方案,因此您可以使用更多计算容量来实现业务目标。 Docker 非常适合高密度环境以及需要用更少资源完成更多工作的中小型部署。

Docker objects

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

Images

镜像是一个只读模板,其中包含创建 Docker 容器的说明。通常,一个镜像基于另一个镜像,并带有一些额外的自定义。For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

Containers

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.


容器和镜像的关系——容器是镜像的可运行实例

1.execution

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression follows:

1
2
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
返回类型模式

除了返回类型模式(前面代码片段中的 ret-type-pattern)、名称模式和参数模式之外的所有部分都是可选的。

返回类型模式确定方法的返回类型必须是什么,以便匹配连接点。 * 最常用作返回类型模式。它匹配任何返回类型。


名称模式

名称模式与方法名称匹配。您可以使用 * 通配符作为名称模式的全部或一部分。

参数模式稍微复杂一些:() 匹配不带参数的方法,而 (..) 匹配任意数量(零个或多个)参数。

The (*) pattern matches a method that takes one parameter of any type. (*,String) matches a method that takes two parameters. The first can be of any type, while the second must be a String.

The following examples show some common pointcut expressions:

  • The execution of any public method:

    1
    execution(public * *(..))
  • The execution of any method with a name that begins with set:

    1
    execution(* set*(..))
  • The execution of any method defined by the AccountService interface:

    1
    execution(* com.xyz.service.AccountService.*(..))
  • The execution of any method defined in the service package:

    1
    execution(* com.xyz.service.*.*(..))
  • The execution of any method defined in the service package or one of its sub-packages:

    1
    execution(* com.xyz.service..*.*(..))

2.拦截器类型

顾名思义,拦截器有以下类型:

  • @Before:这种拦截器先执行拦截代码,再执行目标代码。如果拦截器抛异常,那么目标代码就不执行了;

  • @After:这种拦截器先执行目标代码,再执行拦截器代码。无论目标代码是否抛异常,拦截器代码都会执行;

  • @AfterReturning:和@After不同的是,只有当目标代码正常返回时,才执行拦截器代码;

  • @AfterThrowing:和@After不同的是,只有当目标代码抛出了异常时,才执行拦截器代码;

  • @Around:能完全控制目标代码是否执行,并可以在执行前后、抛异常后执行任意拦截代码,可以说是包含了上面所有功能。


官方文档:

Spring AOP includes the following types of advice:

  • Before advice: Advice that runs before a join point but that does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
  • After returning advice: Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).
  • After throwing advice: Advice to be run if a method exits by throwing an exception.
  • After (finally) advice: Advice to be run regardless of the means by which a join point exits (normal or exceptional return).
  • Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

3.获得方法参数

Access to the Current JoinPoint

Any advice method may declare, as its first parameter, a parameter of type org.aspectj.lang.JoinPoint (note that around advice is required to declare a first parameter of type ProceedingJoinPoint, which is a subclass of JoinPoint). The JoinPoint interface provides a number of useful methods:

  • getArgs(): Returns the method arguments.
  • getThis(): Returns the proxy object.
  • getTarget(): Returns the target object.
  • getSignature(): Returns a description of the method that is being advised.
  • toString(): Prints a useful description of the method being advised.

See the javadoc for more detail.

4.快速使用

(1)在切面类加上@Aspect,@Component(不需要写@EnableAspectJAutoProxy)

(2)写上execution语句

(3)demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class LoginService {
public void login(String userId){
if(isLegal(userId)){
System.out.println(userId+"正常登录");
return;
}
System.out.println(userId+"为非法用户,禁止登录");
}

public boolean isLegal(String userId){
return "qwe".equals(userId);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Aspect
@Component
public class LoginAspect {

@Around(value = "execution(* *..LoginService.login(..))")
public Object loginAspect(ProceedingJoinPoint jp) throws Throwable {
Object[] args = jp.getArgs();
String userId=(String) args[0];
System.out.println("日志:"+userId+"开始登录");
Object result = jp.proceed();
System.out.println("日志:"+userId+"结束登录");
return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
@SpringBootTest
class ThirdAopApplicationTests {

@Autowired
private LoginService loginService;
@Test
void contextLoads() {
loginService.login("qwe");
}

}

测试结果:

1
2
3
日志:qwe开始登录
qwe正常登录
日志:qwe结束登录

Serializable

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

serialVersionUID

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.