-
编程语言:
- 硬件级:微码编程,汇编语言
- 系统级:C,C++,…
- 应用级:Java, PHP, Python, Ruby, Perl, C#, …
-
程序:指令+数据
- 过程式编程:以指令为中心,数据服务于代码;
- 对象式编程:以数据为中心,指令服务于数据;
java
-
Java代码的运行:
*.java(source code) –> javac –> *.class(bytecode)
jvm:class loader,加载程序的类文件,及程序的类文件依赖到的其它的类文件而后运行; 整个运行表现为一个jvm进程; -
java技术体系
- Java编程语言
- Java Class文件格式
- Java API
-
Java VM
- class loader
- 执行引擎
-
JVM运行时区域:
- 方法区:线程共享; 用于存储被JVM加载的class信息、常量、静态变量、方法等;
- 堆:是jvm所管理的内存中占用空间最大的一部分;也是GC管理的主要区域;存储对象;
- Java栈:线程私有,存储 线程自己的局部变量;
- PC寄存器:线程私有的内存空间,程序的指令指针;
- 本地方法栈
-
安装JDK
-
了解当前的java环境:
~]# java -version -
OpenJDK:
-
java-VERSION-openjdk:
The OpenJDK runtime environment. -
java-VERSION-openjdk-headless:
The OpenJDK runtime environment without audio and video support. -
java-VERSION-openjdk-devel:
The OpenJDK development tools.注意:多版本并存时,可使用 alternatives命令设定默认使用的版本;
-
java-VERSION-openjdk:
-
Oracle JDK:
-
安装相应版本的rpm包;jdk-VERSION-OS-ARCH.rpm
示例:jdk-1.8.0_25-linux-x64.rpm
注意:安装完成后,要配置JAVA_HOME环境变量,指向java的安装路径;
OpenJDK: JAVA_HOME=/usr Oracle JDK: JAVA_HOME=/usr/java/jdk_VERSION 示例: /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH
-
-
-
Java 2 EE:
-
Servlet:
- 类库;web app;
- Servlet container, Servlet Engine
-
JSP: Java Server Page
<html> <title>TITLE</title> <body> <h1>...</h1> <% ... java code... %> </body> </html>
-
jsp文件的实现过程:index.jsp –> (jasper) –> index.java –> (javac) –> index.class –> jvm(engine)
注意:基于jasper将静态输出的数据转为java代码进行输出,结果为servlet规范的代码;
-
JSP Container:JSP + Servlet Container
-
商业实现:
WebSphere, WebLogic, Oc4j, Glassfish, Geronimo, JOnAS, JBoss, … -
开源实现:
Tomcat, Jetty, Resin, …
Tomcat: Java 2 EE技术体系的不完整实现;
-
商业实现:
-
Servlet:
Tomcat
-
安装Tomcat:
-
Base Repo:
tomcat, tomcat-lib, tomcat-admin-webapps, tomcat-webapps, tomcat-docs-webapp
-
Tomcat binary release
# tar xf apache-tomcat-VERSION.tar.gz -C /usr/local/ # cd /usr/local # ln -sv apache-tomcat-VERSION tomcat /etc/profile.d/tomcat.sh export CATALINA_BASE=/usr/local/tomcat export PATH=$CATALINA_BASE/bin:$PATH . /etc/profile.d/tomcat.sh
-
Base Repo:
-
tomcat程序环境:
-
解压安装tomcat
tomcat的目录结构 bin:脚本,及启动时用到的类; conf:配置文件目录; lib:库文件,Java类库,jar; logs:日志文件目录; temp:临时文件目录; webapps:webapp的默认目录; work:工作目录;
-
/tomcat/bin/catalina.sh
~]# catalina.sh --help debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running?
-
/tomcat/bin/catalina.sh
-
rpm包安装的程序环境:
-
配置文件目录:/etc/tomcat
- 主配置文件:server.xml
-
webapps存放位置:/var/lib/tomcat/webapps/
examples manager host-manager docs
- Unit File:tomcat.service
- 环境配置文件:/etc/sysconfig/tomcat
-
配置文件目录:/etc/tomcat
-
tomcat的配置文件构成:
- server.xml:主配置文件;
- web.xml:每个webapp只有“部署”后才能被访问,它的部署方式通常由web.xml进行定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认部署相关的配置;
- context.xml:每个webapp都可以专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认配置;
- tomcat-users.xml:用户认证的账号和密码文件;
- catalina.policy:当使用-security选项启动tomcat时,用于为tomcat设置安全策略;
- catalina.properties:Java属性的定义文件,用于设定类加载器路径,以及一些与JVM调优相关参数;
- logging.properties:日志系统相关的配置;
-
Tomcat的核心组件:server.xml
<Server> ... </Server> ... <Server> <Service> ... </Service> ... <Service> <connector/> <connector/> ... <Engine> <Host> <Context/> <Context/> ... </Host> <Host> ... </Host> ... </Engine> </Service> </Server>
-
每一个组件都由一个Java“类”实现,这些组件大体可分为以下几个类型:
顶级组件:Server
服务类组件:Service
连接器组件:http, https, ajp(apache jserv protocol)
容器类:Engine, Host, Context
被嵌套类:valve, logger, realm, loader, manager, …
集群类组件:listener, cluster, …
-
每一个组件都由一个Java“类”实现,这些组件大体可分为以下几个类型:
-
JSP WebAPP的组织结构
/: webapps的根目录 index.jsp:主页; WEB-INF/:当前webapp的私有资源路径;通常用于存储当前webapp的web.xml和context.xml配置文件; META-INF/:类似于WEB-INF/; classes/:类文件,当前webapp所提供的类; lib/:类文件,当前webapp所提供的类,被打包为jar格式;
-
webapp归档格式:
-
.war:webapp
.jar:EJB的类打包文件;
.rar:资源适配器类打包文件;
.ear:企业级webapp;
-
.war:webapp
-
-
部署(deploy)webapp的相关操作:
-
deploy:将webapp的源文件放置于目标目录(网页程序文件存放目录),配置tomcat服务器能够基于web.xml和context.xml文件中定义的路径来访问此webapp;将其特有的类和依赖的类通过class loader装载至JVM;
-
自动部署:auto deploy
手动部署:- 冷部署:把webapp复制到指定的位置,而后才启动tomcat;
-
热部署:在不停止tomcat的前提下进行部署;
部署工具:manager、ant脚本、tcd(tomcat client deployer)等;
-
自动部署:auto deploy
- undeploy:反部署,停止webapp,并从tomcat实例上卸载webapp;
- start:启动处于停止状态的webapp;
- stop:停止webapp,不再向用户提供服务;其类依然在jvm上;
- redeploy:重新部署;
-
deploy:将webapp的源文件放置于目标目录(网页程序文件存放目录),配置tomcat服务器能够基于web.xml和context.xml文件中定义的路径来访问此webapp;将其特有的类和依赖的类通过class loader装载至JVM;
-
手动提供一测试类应用,并冷部署:
~]# mkidr -pv /usr/local/tomcat/webapps/test/{classes,lib,WEB-INF}
创建文件/usr/local/tomcat/webapps/test/index.jsp<%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>Test Page</title> </head> <body> <% out.println("hello world"); %> </body> </html>
-
tomcat的两个管理应用:
-
manager
host-manager
-
manager
-
tomcat的常用组件配置:
-
Server:代表tomcat instance,即表现出的一个java进程;监听在8005端口,只接收“SHUTDOWN”。各server监听的端口不能相同,因此,在同一物理主机启动多个实例时,需要修改其监听端口为不同的端口;
-
Service:用于实现将一个或多个connector组件关联至一个engine组件;
-
Connector组件:
-
负责接收请求,常见的有三类http/https/ajp;
-
进入tomcat的请求可分为两类:
- standalone : 请求来自于客户端浏览器;
-
由其它的web server反代:来自前端的反代服务器;
nginx --> http connector --> tomcat httpd(proxy_http_module) --> http connector --> tomcat httpd(proxy_ajp_module) --> ajp connector --> tomcat httpd(mod_jk) --> ajp connector --> tomcat
-
属性:
port="8080" protocol="HTTP/1.1" connectionTimeout="20000" address:监听的IP地址;默认为本机所有可用地址; maxThreads:最大并发连接数,默认为200; enableLookups:是否启用DNS查询功能; acceptCount:等待队列的最大长度; secure: sslProtocol:
-
-
Engine组件:Servlet实例,即servlet引擎,其内部可以一个或多个host组件来定义站点; 通常需要通过defaultHost来定义默认的虚拟主机;
-
属性:
name= defaultHost="localhost" jvmRoute=
-
属性:
-
Host组件:位于engine内部用于接收请求并进行相应处理的主机或虚拟主机,
示例:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> </Host>
-
常用属性说明:
- appBase:此Host的webapps的默认存放目录,指存放非归档的web应用程序的目录或归档的WAR文件目录路径;可以使用基于$CATALINA_BASE变量所定义的路径的相对路径;
- autoDeploy:在Tomcat处于运行状态时,将某webapp放置于appBase所定义的目录中时,是否自动将其部署至tomcat;
-
示例:
<Host name="tc1.magedu.com" appBase="/appdata/webapps" unpackWARs="true" autoDeploy="true"> </Host> ~]# mkdir -pv /appdata/webapps ~]# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF} 提供一个测试页即可;
-
-
Context组件:
-
示例:
<Context path=”/PATH” docBase=”/PATH/TO/SOMEDIR” reloadable=””/>
-
示例:
-
Valve组件:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
-
Valve存在多种类型:
- 定义访问日志:org.apache.catalina.valves.AccessLogValve
-
定义访问控制:org.apache.catalina.valves.RemoteAddrValve
<Valve className=”org.apache.catalina.valves.RemoteAddrValve” deny=”172\.16\.100\.67″/>
-
-
综合示例:
<Host name="node1.magedu.com" appBase="/web/apps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="node1_access" suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> <Context path="/test" docBase="test" reloadable=""> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="node1_test_access_" suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> </Context> </Host>
-
会话保持:
-
session sticky
-
source_ip
-
nginx: ip_hash
haproxy: source
lvs: sh
-
nginx: ip_hash
-
cookie:
-
nginx:hash
haproxy: cookie
-
nginx:hash
-
source_ip
- session cluster:delta session manager
-
session server:redis(store), memcached(cache)
-
Tomcat Cluster(session)
- session sticky
-
session cluster
tomcat delta manager -
session server
memcached -
Tomcat Cluster
-
httpd + tomcat cluster
httpd: mod_proxy, mod_proxy_http, mod_proxy_balancer
tomcat cluster:http connector -
httpd + tomcat cluster
httpd: mod_proxy, mod_proxy_ajp, mod_proxy_balancer
tomcat cluster:ajp connector -
httpd + tomcat cluster
httpd: mod_jk
tomcat cluster:ajp connector - nginx + tomcat cluster
-
httpd + tomcat cluster
-
BalancerMember:
BalancerMember [balancerurl] url [key=value [key=value …]]-
status:
D: Worker is disabled and will not accept any requests. S: Worker is administratively stopped. I: Worker is in ignore-errors mode and will always be considered available. H: Worker is in hot-standby mode and will only be used if no other viable workers are available. E: Worker is in an error state. N: Worker is in drain mode and will only accept existing sticky sessions destined for itself and ignore all other requests.
- loadfactor:负载因子,即权重;
-
lbmethod
Balancer load-balance method. Select the load-balancing scheduler method to use. Either byrequests, to perform weighted request counting; bytraffic, to perform weighted traffic byte count balancing; or bybusyness, to perform pending request balancing. The default is byrequests.
-
stickysession
Balancer sticky session name. The value is usually set to something like JSESSIONID or PHPSESSIONID, and it depends on the backend application server that support sessions. If the backend application server uses different name for cookies and url encoded id (like servlet containers) use | to separate them. The first part is for the cookie the second for the path. Available in Apache HTTP Server 2.4.4 and later.
-
status:
-
httpd使用第三方模块:以mod_jk为例进行说明,(mod_jk没有rpm包得编译安装)
-
安装httpd-devel和gcc(可安装development tools)
-
编译安装模块:
关键指明httpd的apxs文件的位置,apxs是为httpd安装第三方模块提供的一个对接接口.
–with-apxs示例:
~]# which apxs /usr/bin/apxs ~]# ./configure --with-apxs=/usr/bin/apxs make & make install
-
使用模块:(编辑配置文件)
示例:
vi /etc/httpd/conf.modules.d/mod_jk.conf loadModule jk_module /usr/lib64/httpd/modules/mod_jk.so
-
-
Tomcat Session Server (memcached)
-
支持的session server类型:
-
memcached
couchbase
redis
-
memcached
-
memcached:
memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.-
缓存服务器:
- 缓存:cache,无持久存储功能;
- bypass缓存
- k/v cache,仅支持存储流式化数据;
- 旁挂式缓存;
-
特性:
- k/v cache:仅可存储可序列化数据;value:最大不能超过1兆;存储项:k/v;
- 智能性一半依赖于客户端(调用memcached的API开发程序),一半依赖于服务端;
-
分布式缓存:互不通信的分布式集群;
分布式系统请求路由方法:取模法,一致性哈希算法; - 算法复杂度:O(1)
-
清理过期缓存项:
- 缓存耗尽:LRU
- 缓存项过期:惰性清理机制
-
安装配置:
-
监听的端口:
11211/tcp, 11211/udp -
主程序:/usr/bin/memcached
- 配置文件:/etc/sysconfig/memcached
-
Unit File:/usr/lib/systemd/system/memcached.service
-
协议格式:memcached协议
- 文本格式
- 二进制格式
-
命令:
- 统计类:stats, stats items, stats slabs, stats sizes
-
存储类:set, add, replace, append, prepend
- 命令格式:<command name> <key> <flags> <exptime> <bytes> <cas unique>
- 检索类:get, delete, incr/decr
-
清空:flush_all
-
示例
telnet> add KEY <flags> <expiretime> <bytes> \r telnet> VALUE
-
memcached程序的常用选项:
-m <num>:Use <num> MB memory max to use for object storage; the default is 64 megabytes. -c <num>:Use <num> max simultaneous connections; the default is 1024. -u <username>:以指定的用户身份来运行进程; -l <ip_addr>:监听的IP地址,默认为本机所有地址; -p <num>:监听的TCP端口, the default is port 11211. -U <num>:Listen on UDP port <num>, the default is port 11211, 0 is off. -M:内存耗尽时,不执行LRU清理缓存,而是拒绝存入新的缓存项,直到有多余的空间可用时为止; -f <factor>:增长因子;默认是1.25; -t <threads>:启动的用于响应用户请求的线程数;
-
memcached默认没有认证机制,可借用于SASL进行认证;
- SASL:Simple Authentication Secure Layer
-
API:
php-pecl-memcache php-pecl-memcached python-memcached libmemcached libmemcached-devel
-
命令行工具:
memcached-tool SERVER:PORT COMMAND
-
-
-
-
Tomcat的常用优化配置:
-
内存空间:
/etc/sysconfig/tomcat JAVA_OPTS="-server -Xms32g -Xmx32g -XX:NewSize= -XX:MaxNewSize= -XX:PermSize= -XX:MaxPermSize=" -server:服务器模型 -Xms:堆内存初始化大小; -Xmx:堆内存空间上限; -XX:NewSize=:新生代空间初始化大小; -XX:MaxNewSize=:新生代空间最大值; -XX:PermSize=:持久代空间初始化大小; -XX:MaxPermSize=:持久代空间最大值;
-
线程池设置:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 常用属性: maxThreads:最大线程数; minSpareThreads:最小空闲线程数; maxSpareThreads:最大空闲线程数; acceptCount:等待队列的最大长度; URIEncoding:URI地址编码格式,建议使用UTF-8; enableLookups:是否启用dns解析,建议禁用; compression:是否启用传输压缩机制,建议“on"; compressionMinSize:启用压缩传输的数据流最小值,单位是字节; compressableMimeType:定义启用压缩功能的MIME类型; text/html, text/xml, text/css, text/javascript
-
禁用8005端口;
<Server port=”-1″ shutdown=”SHUTDOWN”> -
隐藏版本信息:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> Server="SOME STRING"
-
内存空间:
-
JVM常用的分析工具:
jps:用来查看运行的所有jvm进程; jinfo:查看进程的运行环境参数,主要是jvm命令行参数; jstat:对jvm应用程序的资源和性能进行实时监控; jstack:查看所有线程的运行状态; jmap:查看jvm占用物理内存的状态; jconsole jvisualvm
-
jps:Java virutal machine Process Status tool,
jps [-q] [-mlvV] [<hostid>] -q:静默模式; -v:显示传递给jvm的命令行参数; -m:输出传入main方法的参数; -l:输出main类或jar完全限定名称; -V:显示通过flag文件传递给jvm的参数; [<hostid>]:主机id,默认为localhost;
-
jinfo:输出给定的java进程的所有配置信息;
jinfo [option] <pid> -flags:to print VM flags -sysprops:to print Java system properties -flag <name>:to print the value of the named VM flag
-
jstack:查看指定的java进程的线程栈的相关信息;
jstack [-l] <pid> jstack -F [-m] [-l] <pid> -l:long listings,会显示额外的锁信息,因此,发生死锁时常用此选项; -m:混合模式,既输出java堆栈信息,也输出C/C++堆栈信息; -F:当使用“jstack -l PID"无响应,可以使用-F强制输出信息;
-
jstat:输出指定的java进程的统计信息
jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]-
~]# jstat -options
-class:class loader -compiler:JIT -gc:gc -gccapacity:统计堆中各代的容量 -gccause: -gcmetacapacity -gcnew:新生代 -gcnewcapacity -gcold:老年代 -gcoldcapacity -gcutil -printcompilation
-
[<interval> [<count>]]
-
interval:时间间隔,单位是毫秒;
count:显示的次数;
-
interval:时间间隔,单位是毫秒;
-
-gc:
-
YGC:新生代的垃圾回收次数;
YGCT:新生代垃圾回收消耗的时长;
FGC:Full GC的次数;
FGCT:Full GC消耗的时长;
GCT:GC消耗的总时长;
-
YGC:新生代的垃圾回收次数;
-
~]# jstat -options
-
jmap:Memory Map, 用于查看堆内存的使用状态;
-
jhat:Java Heap Analysis Tool
jmap [option] <pid>-
查看堆空间的详细信息:
jmap -heap <pid> -
查看堆内存中的对象的数目:
jmap -histo[:live] <pid>
live:只统计活动对象; -
保存堆内存数据至文件中,而后使用jvisualvm或jhat进行查看:
jmap -dump:<dump-options> <pid>dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file>
-
-
jps:Java virutal machine Process Status tool,
原创文章,作者:s,如若转载,请注明出处:http://www.178linux.com/79380