Keepalived+Nginx+Tomcat 实现高可用Web集群
https://www.jianshu.com/p/bc34f9101c5e
Keepalived+Nginx+Tomcat 实现高可用Web集群
一、Nginx的安装过程
1.下载Nginx安装包,安装依赖环境包
(1)安装 C++编译环境
yum -y install gcc #C++
(2)安装pcre
yum -y install pcre-devel
(3)安装zlib
yum -y install zlib-devel
(4)安装Nginx
定位到nginx 解压文件位置,执行编译安装命令
[root@localhost nginx-1.12.2]# pwd
/usr/local/nginx/nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure && make && make install
(5)启动Nginx
安装完成后先寻找那安装完成的目录位置
[root@localhost nginx-1.12.2]# whereis nginx
nginx: /usr/local/nginx
[root@localhost nginx-1.12.2]#
进入Nginx子目录sbin启动Nginx
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx &
[1] 5768
[root@localhost sbin]#
查看Nginx是否启动
或通过进程查看Nginx启动情况
[root@localhost sbin]# ps -aux|grep nginx
root 5769 0.0 0.0 20484 608 ? Ss 14:03 0:00 nginx: master process ./nginx
nobody 5770 0.0 0.0 23012 1620 ? S 14:03 0:00 nginx: worker process
root 5796 0.0 0.0 112668 972 pts/0 R+ 14:07 0:00 grep --color=auto nginx
[1]+ 完成 ./nginx
[root@localhost sbin]#
到此Nginx安装完成并启动成功。
(6)Nginx快捷启动和开机启动配置
编辑Nginx快捷启动脚本【注意Nginx安装路径,需要根据自己的NGINX路径进行改动】
[root@localhost init.d]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
为启动脚本授权 并加入开机启动
[root@localhost init.d]# chmod -R 777 /etc/rc.d/init.d/nginx
[root@localhost init.d]# chkconfig nginx
启动Nginx
[root@localhost init.d]# ./nginx start
将Nginx加入系统环境变量
[root@localhost init.d]# echo 'export PATH=$PATH:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile
Nginx命令 [ service nginx (start|stop|restart) ]
[root@localhost init.d]# service nginx start
Starting nginx (via systemctl): [ 确定 ]
Tips:快捷命令
service nginx (start|stop|restart)
二、KeepAlived安装和配置
1.安装Keepalived依赖环境
yum install -y popt-devel
yum install -y ipvsadm
yum install -y libnl*
yum install -y libnf*
yum install -y openssl-devel
2.编译Keepalived并安装
[root@localhost keepalived-1.3.9]# ./configure
[root@localhost keepalived-1.3.9]# make && make install
3.将Keepalive 安装成系统服务
[root@localhost etc]# mkdir /etc/keepalived
[root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
手动复制默认的配置文件到默认路径
[root@localhost etc]# mkdir /etc/keepalived
[root@localhost etc]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
为keepalived 创建软链接
[root@localhost sysconfig]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
设置Keepalived开机自启动
[root@localhost sysconfig]# chkconfig keepalived on
注意:正在将请求转发到“systemctl enable keepalived.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service
启动Keepalived服务
[root@localhost keepalived]# keepalived -D -f /etc/keepalived/keepalived.conf
关闭Keepalived服务
[root@localhost keepalived]# killall keepalived
三、集群规划和搭建
环境准备:
CentOS 7.2
Keepalived Version 1.4.0 - December 29, 2017
Nginx Version: nginx/1.12.2
Tomcat Version:8
集群规划清单
虚拟机 | IP | 说明 |
---|---|---|
Keepalived+Nginx1[Master] | 192.168.43.101 | Nginx Server 01 |
Keeepalived+Nginx[Backup] | 192.168.43.102 | Nginx Server 02 |
Tomcat01 | 192.168.43.103 | Tomcat Web Server01 |
Tomcat02 | 192.168.43.104 | Tomcat Web Server02 |
VIP | 192.168.43.150 | 虚拟漂移IP |
1.更改Tomcat默认欢迎页面,用于标识切换Web
更改TomcatServer01 节点ROOT/index.jsp 信息,加入TomcatIP地址,并加入Nginx值,即修改节点192.168.43.103信息如下:
<div id="asf-box">
<h1>${pageContext.servletContext.serverInfo}(192.168.224.103)<%=request.getHeader("X-NGINX")%></h1>
</div>
更改TomcatServer02 节点ROOT/index.jsp信息,加入TomcatIP地址,并加入Nginx值,即修改节点192.168.43.104信息如下:
<div id="asf-box">
<h1>${pageContext.servletContext.serverInfo}(192.168.224.104)<%=request.getHeader("X-NGINX")%></h1>
</div>
2.启动Tomcat服务,查看Tomcat服务IP信息,此时Nginx未启动,因此request-header没有Nginx信息。
3.配置Nginx代理信息
1.配置Master节点[192.168.43.101]代理信息
upstream tomcat {
server 192.168.43.103:8080 weight=1;
server 192.168.43.104:8080 weight=1;
}
server{
location / {
proxy_pass http://tomcat;
proxy_set_header X-NGINX "NGINX-1";
}
#......其他省略
}
2.配置Backup节点[192.168.43.102]代理信息
upstream tomcat {
server 192.168.43.103:8080 weight=1;
server 192.168.43.104:8080 weight=1;
}
server{
location / {
proxy_pass http://tomcat;
proxy_set_header X-NGINX "NGINX-2";
}
#......其他省略
}
3.启动Master 节点Nginx服务
[root@localhost init.d]# service nginx start
Starting nginx (via systemctl): [ 确定 ]
此时访问 192.168.43.101 可以看到103和104节点Tcomat交替显示,说明Nginx服务已经将请求负载到了2台tomcat上。
4.同理配置Backup[192.168.43.102] Nginx信息,启动Nginx后,访问192.168.43.102后可以看到Backup节点已起到负载的效果。
4.配置Keepalived 脚本信息
1.在Master节点和Slave节点 /etc/keepalived目录下添加check_nginx.sh 文件,用于检测Nginx的存活状况,添加keepalived.conf文件
check_nginx.sh文件信息如下:
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/rc.d/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
添加完成后,为check_nginx.sh 文件授权,便于脚本获得执行权限。
[root@localhost keepalived]# chmod -R 777 /etc/keepalived/check_nginx.sh
2.在Master 节点 /etc/keepalived目录下,添加keepalived.conf 文件,具体信息如下:
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh" //检测nginx进程的脚本
interval 2
weight -20
}
global_defs {
notification_email {
//可以添加邮件提醒
}
}
vrrp_instance VI_1 {
state MASTER #标示状态为MASTER 备份机为BACKUP
interface ens33 #设置实例绑定的网卡(ip addr查看,需要根据个人网卡绑定)
virtual_router_id 51 #同一实例下virtual_router_id必须相同
mcast_src_ip 192.168.43.101
priority 250 #MASTER权重要高于BACKUP 比如BACKUP为240
advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt #非抢占模式
authentication { #设置认证
auth_type PASS #主从服务器验证方式
auth_pass 123456
}
track_script {
check_nginx
}
virtual_ipaddress { #设置vip
192.168.43.150 #可以多个虚拟IP,换行即可
}
}
3.在Backup节点 etc/keepalived目录下添加 keepalived.conf 配置文件
信息如下:
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh" //检测nginx进程的脚本
interval 2
weight -20
}
global_defs {
notification_email {
//可以添加邮件提醒
}
}
vrrp_instance VI_1 {
state BACKUP #标示状态为MASTER 备份机为BACKUP
interface ens33 #设置实例绑定的网卡(ip addr查看)
virtual_router_id 51 #同一实例下virtual_router_id必须相同
mcast_src_ip 192.168.43.102
priority 240 #MASTER权重要高于BACKUP 比如BACKUP为240
advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt #非抢占模式
authentication { #设置认证
auth_type PASS #主从服务器验证方式
auth_pass 123456
}
track_script {
check_nginx
}
virtual_ipaddress { #设置vip
192.168.43.150 #可以多个虚拟IP,换行即可
}
}
Tips:关于配置信息的几点说明
- state - 主服务器需配成MASTER,从服务器需配成BACKUP
- interface - 这个是网卡名,我使用的是VM12.0的版本,所以这里网卡名为ens33
- mcast_src_ip - 配置各自的实际IP地址
- priority - 主服务器的优先级必须比从服务器的高,这里主服务器配置成250,从服务器配置成240
- virtual_ipaddress - 配置虚拟IP(192.168.43.150)
- authentication - auth_pass主从服务器必须一致,keepalived靠这个来通信
- virtual_router_id - 主从服务器必须保持一致
5.集群高可用(HA)验证
- Step1 启动Master机器的Keepalived和 Nginx服务
[root@localhost keepalived]# keepalived -D -f /etc/keepalived/keepalived.conf
[root@localhost keepalived]# service nginx start
查看服务启动进程
[root@localhost keepalived]# ps -aux|grep nginx
root 6390 0.0 0.0 20484 612 ? Ss 19:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 6392 0.0 0.0 23008 1628 ? S 19:13 0:00 nginx: worker process
root 6978 0.0 0.0 112672 968 pts/0 S+ 20:08 0:00 grep --color=auto nginx
查看Keepalived启动进程
[root@localhost keepalived]# ps -aux|grep keepalived
root 6402 0.0 0.0 45920 1016 ? Ss 19:13 0:00 keepalived -D -f /etc/keepalived/keepalived.conf
root 6403 0.0 0.0 48044 1468 ? S 19:13 0:00 keepalived -D -f /etc/keepalived/keepalived.conf
root 6404 0.0 0.0 50128 1780 ? S 19:13 0:00 keepalived -D -f /etc/keepalived/keepalived.conf
root 7004 0.0 0.0 112672 976 pts/0 S+ 20:10 0:00 grep --color=auto keepalived
使用 ip add 查看虚拟IP绑定情况,如出现192.168.43.150 节点信息则绑定到Master节点
[root@localhost keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:91:bf:59 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.101/24 brd 192.168.43.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.43.150/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::9abb:4544:f6db:8255/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::b0b3:d0ca:7382:2779/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
- Step 2 启动Backup节点Nginx服务和Keepalived服务,查看服务启动情况,如Backup节点出现了虚拟IP,则Keepalvied配置文件有问题,此情况称为脑裂。
[root@localhost keepalived]# clear
[root@localhost keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:14:df:79 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.102/24 brd 192.168.43.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
- Step 3 验证服务
浏览并多次强制刷新地址: http://192.168.43.150 ,可以看到103和104多次交替显示,并显示Nginx-1,则表明 Master节点在进行web服务转发。
- Step 4 关闭Master keepalived服务和Nginx服务,访问Web服务观察服务转移情况
[root@localhost keepalived]# killall keepalived
[root@localhost keepalived]# service nginx stop
此时强制刷新192.168.43.150发现 页面交替显示103和104并显示Nginx-2 ,VIP已转移到192.168.43.102上,已证明服务自动切换到备份节点上。
- Step 5 启动Master Keepalived 服务和Nginx服务
此时再次验证发现,VIP已被Master重新夺回,并页面交替显示 103和104,此时显示Nginx-1
四、Keepalived抢占模式和非抢占模式
keepalived的HA分为抢占模式和非抢占模式,抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP。
非抢占模式配置:
- 1> 在vrrp_instance块下两个节点各增加了nopreempt指令,表示不争抢vip
- 2> 节点的state都为BACKUP
两个keepalived节点都启动后,默认都是BACKUP状态,双方在发送组播信息后,会根据优先级来选举一个MASTER出来。由于两者都配置了nopreempt,所以MASTER从故障中恢复后,不会抢占vip。这样会避免VIP切换可能造成的服务延迟。
Keepalived+Nginx+Tomcat 实现高可用Web集群的更多相关文章
- 搭建 Keepalived + Nginx + Tomcat 的高可用负载均衡架构
1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...
- 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构
1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...
- Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建
Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建 Nginx 是一个高性能的 HTTP反向代理服务器 Keepalived 是一个基于VRRP协议来实现的LVS服务高可 ...
- 一寸宕机一寸血,十万容器十万兵|Win10/Mac系统下基于Kubernetes(k8s)搭建Gunicorn+Flask高可用Web集群
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_185 2021年,君不言容器技术则已,欲言容器则必称Docker,毫无疑问,它是当今最流行的容器技术之一,但是当我们面对海量的镜像 ...
- Keepalived+Nginx+Tomcat配置高可用负载均衡系统示例
前言 此示例为keepalived+nginx+tomcat的基础配置示例,某些特定配置此例中不会出现,在示例中会用到三个虚拟机:两个纯命令行用于模拟服务端配置,一个带桌面环境的用于模拟客户端访问,这 ...
- Apache tomcat高可用web集群搭建过程配置记录
说明,本文仅作为个人搭建配置保存,问题处理没有一一列出,过程也未见详尽,有问题的朋友可以直接留言给我,会一一回复,谢谢. 小目标: 支持故障转移(或主备,扩展性不佳),保证故障转移后,对前端用户透明, ...
- Nginx+Tomcat+memcached高可用会话保持
Nginx+Tomcat+memcached高可用会话保持 文章来源dyc2005 一.概述 之前文章已经描述了企业高可用负载相关的架构及实现,其中常用的nginx或haproxy,LVS结合ke ...
- Keepalived+Nginx解决方案实现高可用的API网关(nginx)
一. 采用Keepalived+Nginx解决方案实现高可用的API网关. 2.1 Nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP ...
- Keepalived之高可用LVS集群
前文我们聊了下keepalived的邮件通知相关配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13645163.html:今天我们来说说keepalive ...
随机推荐
- C++入门经典-例8.5-多重继承
1:C++允许子类从多个父类继承公有的和受保护的成员,这称为多重继承. 2:多重继承的定义.多重继承有多个基类名称标识符,其声明形式如下: class 派生类名标识符:[继承方式] 基类名标识符1,. ...
- IDEA2019 JFormDesigner-6.0.6.2-intellij-idea破解版
IDEA2019 JFormDesigner-6.0.6.2-intellij-idea破解版 下载链接:百度云 提取码:10ox 下载完后通过idea的本地磁盘安装即可
- Java-数据类型与编码(ASCII、Unicode 和 UTF-8)
机械硬盘硬件结构(了解)https://diy.pconline.com.cn/cpu/study_cpu/1009/2215404_all.html 一.数据储存单位 1.bit(位) https: ...
- git备份代码
仓库备份位置: /huawei-bak vim /huawei-bak/huawei-bak.sh #!/bin/bash#项目克隆下来后将其注释即可PROJECT="git@codehub ...
- C# Await
每次提到异步我都选择绕开,感觉深不可测,最近打算看看异步,但又不愿意看书,网上找了几个视频看,发现传智播客的老师讲异步都不是很深入,关键的问题一笔带过,倒是把我弄糊涂了,印象最深刻的是那个老师说的一句 ...
- SAS数据挖掘实战篇【一】
SAS数据挖掘实战篇[一] 1数据挖掘简介 1.1数据挖掘的产生 需求是一切技术之母,管理和计算机技术的发展,促使数据挖掘技术的诞生.随着世界信息技术的迅猛发展,信息量也呈几何指数增长,如何从巨量.复 ...
- C#析构函数(destructor)和终结器(Finalizer) .
使用析构函数释放资源 析构函数用于析构类的实例. 1) 不能在结构中定义析构函数.只能对类使用析构函数. 2) 一个类只能有一个析构函数. 3) 无法继 ...
- JavaScript基础入门04
目录 JavaScript 基础入门04 JavaScript 对象 介绍 关于键名 对象的引用 语句和表达式需要注意的地方 对象属性常见的操作 with语句 JSON 特点 语法规则 JSON合法示 ...
- 继承以及Super
一个小小的总结,主要关注以下三个问题:ES5的继承方式,ES5的继承与ES6的继承的区别,ES6的super的几种使用方式以及其中this的指向. From http://supermaryy.com ...
- 【JulyEdu-Python基础】第 4 课:面向对象基础
类(class):用来描述具有相同的属性和方法的对象的集合,它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 对象:通过类定义的数据结构实例.对象包括两个数据成员(类变量和实例变量)和方法 ...