首页 文章 精选 留言 我的

精选列表

搜索[TDSQL PostgreSQL 版],共10000篇文章
优秀的个人博客,低调大师

【Sigma敏捷版系列文章】从运行流程和list-watch看kubernetes系统的设计理念

1. 写在前面 kubernetes作为容器化应用集群管理系统,为容器化应用提供了便利的资源调度,部署运行,服务发现,扩容缩容,自动运维等贴心功能。也正因为其强大且不断丰富的功能,让kubernetes在容器云系统领域越来越受到大家关注。而做为kubernetes系统的设计开发人员,更关注kubernetes系统的设计理念层面,从而可以更好的增强和优化kubernetes。kubernentes的主要设计理念如下: 系统运行基于声明式(Declarative)数据而非命令式(Imperative)数据 组件间消息传递基于 Level Trigger而非Edge Trigger 系统运行状态控制基于各类闭环控制器 系统可扩展基于各类抽象接口(CRI, CNI, CSI, scheduler, admission等) 本文通过分析kubernet

优秀的个人博客,低调大师

资源 | 台大李宏毅教授最新课程,深度强化学习有国语版啦!

入坑深度学习的同学肯定都听过台湾大学的李宏毅教授,或者是他那本非常受欢迎的课程——《1天搞懂深度学习》。这门课程的讲义PPT总共有286页,深入浅出的介绍了深度学习的概念、框架及展望。 课程内容通俗易懂,适合深度学习初学者及相关从业人员,在大数据文摘后台回复“深度学习”可下载课程讲义。 文摘菌今天要给大家推荐的是李老师最近更新的课程:深度强化学习(deep reinforcement learning),目前这门课程在youtobe上更新了4个视频,分别是策略梯度算法(Policy Gradient)、近端策略优化(Proximal Policy Optimization,PPO)、Q-learning算法基本概念和Q-learning算法高级技巧。 每个视频大约40分钟左右,PPT内容为英文,讲授语言为中文。课程内容主打强化学习,涉及

优秀的个人博客,低调大师

Shell脚本快速部署Kubernetes(K8S v1.1版本)集群系统

提醒:即使按照本篇文章一步一步做,也不一定部署成功。原因你懂得!如果失败,也建议你仔细看看脚本内容,从中寻找解决办法。同时,相信你也会从脚本中获取到其他有价值的信息。 实验环境: 操作系统:Ubuntu14.04_x64 master:192.168.1.150 minion01: 192.168.1.151 容器网段:172.17.1.0/24 minion02: 192.168.1.152 容器网段:172.17.2.0/24 安装包下载: etcd:http://pan.baidu.com/s/1c1wITMw kubernetes:http://pan.baidu.com/s/1kUoxgYb 相关脚本下载:http://pan.baidu.com/s/1o7nEaca 脚本说明: config_ssh_root_remote.sh #配置root SSH登录(默认ubuntu系统禁止root SSH登录) ssh_keypair_auth.sh #配置master主机与minion主机SSH免交互认证 kubernetes-install.sh #安装kubernetes的master端与minion端 config_gre_channel.sh #配置两台Docker主机容器实现跨主机访问(OVS) 安装步骤(请按照步骤做): 1)在minion主机root权限开启root允许SSH远程登录 操作命令:$ sudo bash config_ssh_root_remote.sh 脚本内容:$ cat config_ssh_root_remote.sh 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 #!/bin/bash #Description:configurationrootaccountsshremotelogin if [$USER!= "root" ]; then echo "Pleaseuserootaccountoperationorsudo!" exit 1 fi function color_echo(){ if [$1== "green" ]; then echo -e "\033[32;40m$2\033[0m" elif [$1== "red" ]; then echo -e "\033[31;40m$2\033[0m" fi } function check_pkg(){ if !$(dpkg-l$PKG_NAME> /dev/null 2>&1); then echo no else echo yes fi } function install_pkg(){ local PKG_NAME=$1 if [$(check_pkg$PKG_NAME)== "no" ]; then apt-get install $PKG_NAME-y if [$(check_pkg$PKG_NAME)== "no" ]; then color_echogreen "$PKG_NAMEinstallationfailure!Trytoinstallagain." apt-getautoremove&&apt-getupdate apt-get install $PKG_NAME--force- yes -y [$(check_pkg$PKG_NAME)== "no" ]&&color_echored "The$PKG_NAMEinstallationfailure!" && exit 1 fi fi } install_pkgexpect #modifysshconfigfile sed -r-i 's/(PermitRootLogin).*/\1yes/' /etc/ssh/sshd_config service ssh restart> /dev/null #setrootaccountpassword echo "------------------------------------------------------>" while true ; do read -p "Pleaseenteryouwanttosettherootaccountpassword:" ROOT_PASS if [-n "$ROOT_PASS" ]; then break else color_echored "Passwordcannotbeempty!" continue fi done expect-c" spawn passwd root expect{ \"EnternewUNIXpassword:\"{send\"$ROOT_PASS\r\";exp_continue} \"RetypenewUNIXpassword:\"{send\"$ROOT_PASS\r\"} } expecteof "> /dev/null color_echogreen "Therootaccountpasswordis:$ROOT_PASS" 2)在master主机切换到root用户执行脚本与minion主机root用户建立SSH免交互登录 操作命令:# bashssh_keypair_auth.sh root@192.168.1.151-152@123 脚本内容:# catssh_keypair_auth.sh 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 #!/bin/bash #Description:configurationlocalhostandremotehostsshkeypairauthentication,SupportUbuntuandCentOSoperationsystem. function color_echo(){ if [$1== "green" ]; then echo -e "\033[32;40m$2\033[0m" elif [$1== "red" ]; then echo -e "\033[31;40m$2\033[0m" fi } function os_version(){ local OS_V=$( cat /etc/issue | awk 'NR==1{print$1}' ) if [$OS_V== "\S" -o$OS_V== "CentOS" ]; then echo "CentOS" elif [$OS_V== "Ubuntu" ]; then echo "Ubuntu" fi } function check_ssh_auth(){ if $( grep "Permissiondenied" $EXP_TMP_FILE> /dev/null ); then color_echored "Host$IPSSHauthenticationfailure!Loginpassworderror." exit 1 elif $( ssh $INFO 'echoyes>/dev/null' ); then color_echogreen "Host$IPSSHauthenticationsuccessfully." fi rm $EXP_TMP_FILE> /dev/null } function check_pkg(){ local PKG_NAME=$1 if [$(os_version)== "CentOS" ]; then if !$(rpm-ql$PKG_NAME> /dev/null 2>&1); then echo no else echo yes fi elif [$(os_version)== "Ubuntu" ]; then if !$(dpkg-l$PKG_NAME> /dev/null 2>&1); then echo no else echo yes fi fi } function install_pkg(){ local PKG_NAME=$1 if [$(os_version)== "CentOS" ]; then if [$(check_pkg$PKG_NAME)== "no" ]; then yum install $PKG_NAME-y if [$(check_pkg$PKG_NAME)== "no" ]; then color_echogreen "The$PKG_NAMEinstallationfailure!Trytoinstallagain." yummakecache yum install $PKG_NAME-y [$(check_pkg$PKG_NAME)== "no" ]&&color_echored "The$PKG_NAMEinstallationfailure!" && exit 1 fi fi elif [$(os_version)== "Ubuntu" ]; then if [$(check_pkg$PKG_NAME)== "no" ]; then apt-get install $PKG_NAME-y if [$(check_pkg$PKG_NAME)== "no" ]; then color_echogreen "$PKG_NAMEinstallationfailure!Trytoinstallagain." apt-getautoremove&&apt-getupdate apt-get install $PKG_NAME--force- yes -y [$(check_pkg$PKG_NAME)== "no" ]&&color_echored "The$PKG_NAMEinstallationfailure!" && exit 1 fi fi fi } function generate_keypair(){ if [!-e~/. ssh /id_rsa .pub]; then color_echogreen "Thepublic/privatersakeypairnotexist,startGenerating..." expect-c" spawn ssh -keygen expect{ \" ssh /id_rsa ):\"{send\"\r\";exp_continue} \"passphrase):\"{send\"\r\";exp_continue} \"again:\"{send\"\r\";exp_continue} } "> /dev/null 2>&1 if [-e~/. ssh /id_rsa .pub]; then color_echogreen "Generatingpublic/privatersakeypairsuccessfully." else color_echored "Generatingpublic/privatersakeypairfailure!" exit 1 fi fi } EXP_TMP_FILE= /tmp/expect_ssh .tmp if [[$1=~^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@.*]]; then install_pkgexpect;generate_keypair for i in $@; do USER=$( echo $i| cut -d@-f1) IP=$( echo $i| cut -d@-f2) PASS=$( echo $i| cut -d@-f3) INFO=$USER@$IP expect-c" spawn ssh -copy- id $INFO expect{ \"( yes /no )?\"{send\" yes \r\";exp_continue} \"password:\"{send\"$PASS\r\";exp_continue} } ">$EXP_TMP_FILE #ifloginfailed,loginerrorinfoappendtempfile check_ssh_auth done elif [[$1=~^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}@.*]]; then install_pkgexpect;generate_keypair START_IP_NUM=$( echo $1| sed -r 's/.*\.(.*)-(.*)@.*/\1/' ) END_IP_NUM=$( echo $1| sed -r 's/.*\.(.*)-(.*)@.*/\2/' ) for ((i=$START_IP_NUM;i<=$END_IP_NUM;i++)); do USER=$( echo $1| cut -d@-f1) PASS=$( echo $1| cut -d@-f3) IP_RANGE=$( echo $1| sed -r 's/.*@(.*\.).*/\1/' ) IP=$IP_RANGE$i INFO=$USER@$IP_RANGE$i expect-c" spawn ssh -copy- id $INFO expect{ \"( yes /no )?\"{send\" yes \r\";exp_continue} \"password:\"{send\"$PASS\r\";exp_continue} } ">$EXP_TMP_FILE check_ssh_auth done else echo "Example1:$0<root@192.168.1.10-15@password>" echo "Example2:$0<root@192.168.1.10@password>" echo "Example3:$0[root@192.168.1.10@passwordroot@192.168.1.11@passwordroot@192.168.1.12@password...]" fi 3)在master主机root权限执行脚本安装master端服务 操作命令:$ sudo bash kubernetes-install.sh master 脚本内容:$ cat kubernetes-install.sh 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 #!/bin/bash #Description:InstallationKubernetes1.1.3 #EtcdDownload:https://github.com/coreos/etcd/releases/download/v2.2.2/etcd-v2.2.2-linux-amd64.tar.gz #K8SDownload:https://storage.googleapis.com/kubernetes-release/release/v1.1.3/kubernetes.tar.gz . /lib/lsb/init-functions if [$( cat /etc/issue | awk '{print$1}' )!= "Ubuntu" ]; then echo "Onlysupportubuntuoperatingsystem!" exit 1 fi if [$USER!= "root" ]; then echo "Pleaseuserootaccountoperation!" exit 1 fi function color_echo(){ if [$1== "green" ]; then echo -e "\033[32;40m$2\033[0m" elif [$1== "red" ]; then echo -e "\033[31;40m$2\033[0m" fi } function check_install_pkg(){ if [!-e$ETCD_FILE-a!-e$K8S_FILE]; then color_echored "$ETCD_FILEand$K8S_FILEfilenotexist!" exit 1 elif [!-e$ETCD_FILE]; then color_echored "$ETCD_FILEfilenotexist!" exit 1 elif [!-e$K8S_FILE]; then color_echored "$K8S_FILEfilenotexist!" exit 1 fi } function local_ip(){ local NUMARRAY_LENGTH NUM=0 for NIC_NAME in $( ls /sys/class/net | grep -vE "lo|docker0" ); do NIC_IP=$( ifconfig $NIC_NAME| awk -F '[:]+' '/inetaddr/{print$4}' ) if [-n "$NIC_IP" ]; then NIC_IP_ARRAY[$NUM]= "$NIC_NAME:$NIC_IP" let NUM++ fi done ARRAY_LENGTH=${ #NIC_IP_ARRAY[*]} if [$ARRAY_LENGTH- eq 1]; then LOCAL_IP=${NIC_IP_ARRAY[0] #*:} return 0 elif [$ARRAY_LENGTH- eq 0]; then color_echored "Noavailablenetworkcard!" exit 1 else #multinetworkcardselect for NIC in ${NIC_IP_ARRAY[*]}; do echo $NIC done while true ; do read -p "Pleaseenterlocalusetonetworkcardname:" INPUT_NIC_NAME for NIC in ${NIC_IP_ARRAY[*]}; do NIC_NAME=${NIC%:*} if [$NIC_NAME== "$INPUT_NIC_NAME" ]; then LOCAL_IP=${NIC_IP_ARRAY[0] #*:} return 0 fi done echo "Notmatch!Pleaseinputagain." done fi } function check_ip(){ local IP=$1 VALID_CHECK=$( echo $IP| awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print"yes"}' ) if echo $IP| grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" > /dev/null ; then if [${VALID_CHECK:-no}== "yes" ]; then return 0 else echo "IP$IPnotavailable!" return 1 fi else echo "IPformaterror!" return 1 fi } function cluster_ip(){ if [$1== "master" ]; then while true ; do read -p "PleaseentermasterIP:" MASTER_IP check_ip$MASTER_IP [$?- eq 0]&& break done elif [$1== "minion" ]; then while true ; do local MINION_NUM read -p "Pleaseenterclusterminionnodenumber:" MINION_NUM if [[$MINION_NUM=~^[0-9]+$]]; then break else color_echored "Formaterror!" fi done NUM=1 while [$NUM- le $MINION_NUM]; do local MINION_IP read -p "Pleaseenterminionhost$NUMIP:" MINION_IP check_ip$MINION_IP if [$?- eq 0]; then let NUM++ MINION_IP_ARRAY+=($MINION_IP) fi done fi } function modify_init_script(){ if [$1== "master" ]; then cd $MASTER_MODULE_INIT_SCRIPT_DIR elif [$1== "minion" ]; then cd $MINION_MODULE_INIT_SCRIPT_DIR fi for MODULE_INIT_SCRIPT in $( ls | grep - v etcd); do if [-x$MODULE_INIT_SCRIPT]; then sed -r-i '/\/sbin\/initctl/{s/(if)(.*)/\1false\&\&\2/}' $MODULE_INIT_SCRIPT fi done } function check_service_status(){ sleep 1 if [$( ps -ef| grep - v grep | grep -c "$BIN_DIR/$MODULE_INIT_SCRIPT" )- eq 1]; then log_begin_msg "Starting$MODULE_INIT_SCRIPT" log_end_msg0 #0istherightcommandexecutionstatus else log_failure_msg "$(color_echored" Starting$MODULE_INIT_SCRIPT ")" log_end_msg1 #1isthewrongcommandexecutionstatus fi } function check_exec_status(){ if [$?- ne 0]; then color_echogreen "Pleasetrytorunthescript!" exit 1 fi } BASE_DIR=$PWD ETCD_FILE=$BASE_DIR /etcd-v2 .2.2-linux-amd64. tar .gz K8S_FILE=$BASE_DIR /kubernetes . tar .gz BIN_DIR= /opt/bin INIT_SCRIPT_DIR= /etc/init .d OPTS_FILE_DIR= /etc/default MODULE_BIN_DIR=$BASE_DIR /kubernetes/server/bin MASTER_MODULE_INIT_SCRIPT_DIR=$BASE_DIR /kubernetes/cluster/ubuntu/master/init_scripts MINION_MODULE_INIT_SCRIPT_DIR=$BASE_DIR /kubernetes/cluster/ubuntu/minion/init_scripts case $1 in master) check_install_pkg pkilletcd;pkillkube cluster_ipminion #Createbinaryfiledirectory [!-d$BIN_DIR]&& mkdir $BIN_DIR #Installationstoragesystemetcd log_action_msg "Unzipthe$ETCD_FILE" tar zxf$ETCD_FILE;check_exec_status cp $BASE_DIR /etcd-v2 .2.2-linux-amd64 /etc *$BIN_DIR;check_exec_status echo " ETCD_OPTS=\"\ --listen-client-urlshttp: //0 .0.0.0:4001\ --advertise-client-urlshttp: //0 .0.0.0:4001\ --data- dir /var/lib/etcd/default .etcd\" ">$OPTS_FILE_DIR /etcd #Installationmodulekube-apiserverkube-schedulerandkube-controller-manager log_action_msg "Unzipthe$K8S_FILE" tar zxf$BASE_DIR /kubernetes . tar .gz;check_exec_status tar zxf$BASE_DIR /kubernetes/server/kubernetes-server-linux-amd64 . tar .gz;check_exec_status cd $MODULE_BIN_DIR&& cp -akubectlkube-apiserverkube-schedulerkube-controller-manager$BIN_DIR;check_exec_status #Configureinitscripts modify_init_scriptmaster cp -aetcdkube-*$INIT_SCRIPT_DIR;check_exec_status sed -i '63s/.*/"/' $INIT_SCRIPT_DIR /etcd #Removetheappendlogfile,Otherwiseetcdmaycannotbestarted #ModuleConfigureoption log_action_msg "Create$OPTS_FILE_DIR/kube-apiserverstartupoptionsfile..." echo " KUBE_APISERVER_OPTS=\"\ --insecure-bind-address=0.0.0.0\ --insecure-port=8080\ --service-cluster-ip-range=10.0.0.0 /16 \ --etcd_servers=http: //127 .0.0.1:4001\ --logtostderr= true \" ">$OPTS_FILE_DIR /kube-apiserver check_exec_status log_action_msg "Create$OPTS_FILE_DIR/kube-controller-managerstartupoptionsfile..." echo " KUBE_CONTROLLER_MANAGER_OPTS=\"\ --master=127.0.0.1:8080\ --logtostderr= true \" ">$OPTS_FILE_DIR /kube-controller-manager log_action_msg "Create$OPTS_FILE_DIR/kube-schedulerstartupoptionsfile..." echo " KUBE_SCHEDULER_OPTS=\"\ --master=127.0.0.1:8080\ --logtostderr= true \" ">$OPTS_FILE_DIR /kube-scheduler #Startingmodule for MODULE_INIT_SCRIPT in $( ls $INIT_SCRIPT_DIR| grep -E "(etcd|kube.*)" ); do service$MODULE_INIT_SCRIPTstart> /dev/null check_service_status done #setvariable echo "exportPATH=$PATH:$BIN_DIR" >> /etc/profile . /etc/profile #Copymodulekubeletandkube-proxytominion SSH_OPTS= "-oConnectTimeout=1-oConnectionAttempts=3" cd $MODULE_BIN_DIR for MINION_IP in ${MINION_IP_ARRAY[*]}; do log_action_msg "Copymoduleto$MINION_IP:$BIN_DIR..." ssh $SSH_OPTSroot@$MINION_IP "mkdir$BIN_DIR2>/dev/null" ;check_exec_status scp $SSH_OPTSkubeletkube-proxyroot@$MINION_IP:$BIN_DIR done #Copymoduleinitscriptstominion modify_init_scriptminion cd $MINION_MODULE_INIT_SCRIPT_DIR for MINION_IP in ${MINION_IP_ARRAY[*]}; do log_action_msg "Copymoduleinitscriptsto$MINION_IP:$INIT_SCRIPT_DIR..." scp $SSH_OPTSkubeletkube-proxyroot@$MINION_IP:$INIT_SCRIPT_DIR;check_exec_status done color_echogreen "Kubernetesmasterinstallationcomplete." ;; minion) cluster_ipmaster #Noticeinputmasterip local_ip pkillkube #InstallDocker if !$(dpkg-ldocker-engine> /dev/null 2>&1)&&!$(dockerinfo> /dev/null 2>&1); then log_action_msg "StarttheinstallationDocker..." apt-keyadv--keyserverhkp: //p80 .pool.sks-keyservers.net:80--recv-keys58118E89F3A912897C070ADBF76221572C52609D> /dev/null 2>&1 [$?- ne 0]&& echo "Dockersourcesecretkeyregisterfailure!" DOCKER_U_SOURCE= /tmp/docker_source .tmp echo " debhttps: //apt .dockerproject.org /repo ubuntu-precisemain debhttps: //apt .dockerproject.org /repo ubuntu-trustymain debhttps: //apt .dockerproject.org /repo ubuntu-vividmain debhttps: //apt .dockerproject.org /repo ubuntu-wilymain ">$DOCKER_U_SOURCE OS_CODE_V=$(lsb_release-cs) DOKER_SOURCE=$( grep $OS_CODE_V$DOCKER_U_SOURCE) echo "$DOKER_SOURCE" > /etc/apt/sources .list.d /docker .list rm $DOCKER_U_SOURCE apt-getupdate apt-get install docker-engine-y if $(dpkg-ldocker-engine> /dev/null )&&$(dockerinfo> /dev/null ); then color_echogreen "Dockerinstallationsuccessfully." else apt-getremove;apt-get install docker-engine--force- yes -y if !$(dpkg-ldocker-engine> /dev/null )&&!$(dockerinfo> /dev/null ); then color_echored "Dockerinstallationfailure!" exit 1 fi fi fi #ModuleConfigureoption log_action_msg "Create$OPTS_FILE_DIR/kubeletstartupoptionsfile..." echo " KUBELET_OPTS=\"\ --address=0.0.0.0\ --port=10250\ --hostname_override=$LOCAL_IP\ --api_servers=http: // $MASTER_IP:8080\ --pod-infra-container-image=docker.io /kubernetes/pause :latest\ --logtostderr= true \" ">$OPTS_FILE_DIR /kubelet log_action_msg "Create$OPTS_FILE_DIR/kube-proxystartupoptionsfile..." echo " KUBE_PROXY_OPTS=\"\ --master=http: // $MASTER_IP:8080\ --proxy-mode=iptables\ --logtostderr= true \" ">$OPTS_FILE_DIR /kube-proxy #Startingmodule for MODULE_INIT_SCRIPT in $( ls $INIT_SCRIPT_DIR| grep kube.*); do service$MODULE_INIT_SCRIPTstart> /dev/null check_service_status done color_echogreen "Kubernetesminioninstallationcomplete." ;; *) echo "Usage:$0{master|minion}" exit 1 ;; esac 4)在minion主机root权限执行脚本安装minion端服务 操作命令:$ sudo bashkubernetes-install.shminion 脚本内容:同上 5)在minion主机root权限执行脚本创建GRE通道 操作命令:$ sudo bashconfig_gre_channel.sh 脚本内容:$ cat config_gre_channel.sh 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 #!/bin/bash #Description:configurationdockerhostbetweenGREChannel if [$( cat /etc/issue | awk '{print$1}' )!= "Ubuntu" ]; then echo "Onlysupportubuntuoperatingsystem!" exit 1 fi if [$USER!= "root" ]; then echo "Pleaseuserootaccountoperation!" exit 1 fi function color_echo(){ if [$1== "green" ]; then echo -e "\033[32;40m$2\033[0m" elif [$1== "red" ]; then echo -e "\033[31;40m$2\033[0m" fi } function check_ip(){ local IP=$1 local VALID_CHECK=$( echo $IP| awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print"yes"}' ) if echo $IP| grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" > /dev/null ; then if [${VALID_CHECK:-no}== "yes" ]; then return 0 else echo "IP$IPnotavailable!" return 1 fi else echo "IPformaterror!" return 1 fi } function docker_host_ip(){ color_echogreen "Notice:OnlysupporttwoDockerhostconfigurationGREChannel!" NUM=1 while [$NUM- le 2]; do local DOCKER_IP read -p "PleaseenterDockerhost$NUMIP:" DOCKER_HOST_IP check_ip$DOCKER_HOST_IP if [$?- eq 0]; then let NUM++ DOCKER_HOST_IP_ARRAY+=($DOCKER_HOST_IP) fi done } function local_nic_info(){ local NUMARRAY_LENGTH NUM=0 for NIC_NAME in $( ls /sys/class/net | grep -vE "lo|docker0" ); do NIC_IP=$( ifconfig $NIC_NAME| awk -F '[:]+' '/inetaddr/{print$4}' ) if [-n "$NIC_IP" ]; then NIC_IP_ARRAY[$NUM]= "$NIC_NAME:$NIC_IP" let NUM++ fi done ARRAY_LENGTH=${ #NIC_IP_ARRAY[*]} if [$ARRAY_LENGTH- eq 1]; then LOCAL_NIC=${NIC_IP_ARRAY[0]%:*} LOCAL_IP=${NIC_IP_ARRAY[0] #*:} return 0 elif [$ARRAY_LENGTH- eq 0]; then color_echored "Noavailablenetworkcard!" exit 1 else #multinetworkcardselect for NIC in ${NIC_IP_ARRAY[*]}; do echo $NIC done while true ; do read -p "Pleaseenterlocalusetonetworkcardname:" INPUT_NIC_NAME for NIC in ${NIC_IP_ARRAY[*]}; do NIC_NAME=${NIC%:*} if [$NIC_NAME== "$INPUT_NIC_NAME" ]; then LOCAL_NIC=${NIC_IP_ARRAY[0]%:*} LOCAL_IP=${NIC_IP_ARRAY[0] #*:} return 0 fi done echo "Notmatch!Pleaseinputagain." done fi } function check_pkg(){ if !$(dpkg-l$PKG_NAME> /dev/null 2>&1); then echo no else echo yes fi } function install_pkg(){ local PKG_NAME=$1 if [$(check_pkg$PKG_NAME)== "no" ]; then apt-get install $PKG_NAME-y if [$(check_pkg$PKG_NAME)== "no" ]; then color_echogreen "The$PKG_NAMEinstallationfailure!Trytoinstallagain." apt-getautoremove&&apt-getupdate apt-get install $PKG_NAME--force- yes -y [$(check_pkg$PKG_NAME)== "no" ]&&color_echored "The$PKG_NAMEinstallationfailure!" && exit 1 fi fi } function config_gre_channel(){ install_pkgopenvswitch-switch install_pkgbridge-utils if [${DOCKER_HOST_IP_ARRAY[0]}== "$LOCAL_IP" ]; then REMOTE_HOST_IP=${DOCKER_HOST_IP_ARRAY[1]} #remotehostip REMOTE_DOCKER_IP= "172.17.2.0/24" #remotedockerhostdefaultcontaineriprange LOCAL_DOCKER_IP= "172.17.1.0" #kbr0gateway elif [${DOCKER_HOST_IP_ARRAY[1]}== "$LOCAL_IP" ]; then REMOTE_HOST_IP=${DOCKER_HOST_IP_ARRAY[0]} REMOTE_DOCKER_IP= "172.17.1.0/24" LOCAL_DOCKER_IP= "172.17.2.0" else echo "IPnotmatch!Pleaseinputagain." exit 1 fi ovs-vsctladd-brobr02> /dev/null ovs-vsctladd-portobr0gre0-- set Interfacegre0 type =greoptions:remote_ip=$REMOTE_HOST_IP2> /dev/null brctladdbrkbr02> /dev/null brctladdifkbr0obr02> /dev/null iplink set devkbr0up if [$( grep -cE "kbr0" /etc/network/interfaces )- ne 2]; then echo " autokbr0 ifacekbr0inetstatic address$( echo $LOCAL_DOCKER_IP| sed 's/0$/1/' ) netmask255.255.255.0 gateway$LOCAL_DOCKER_IP bridge_portsobr0 ">> /etc/network/interfaces fi if $( ls /sys/class/net | grep docker0> /dev/null ); then servicedockerstop> /dev/null iplink set devdocker0down iplinkdeletedevdocker0 echo "DOCKER_OPTS=\"-b=kbr0\"" > /etc/default/docker servicedockerstart> /dev/null fi ifdownkbr0> /dev/null 2>&1;ifupkbr0> /dev/null 2>&1 #rebootinvalid #iprouteadd$REMOTE_DOCKER_IPvia$REMOTE_HOST_IPdev$LOCAL_NIC2>/dev/null #permanentvalid if !$( grep "$REMOTE_DOCKER_IPvia$REMOTE_HOST_IP" /etc/rc . local > /dev/null ); then sed -i "$i\iprouteadd$REMOTE_DOCKER_IPvia$REMOTE_HOST_IPdev$LOCAL_NIC" /etc/rc . local fi } #main docker_host_ip echo "--------------------------------------------" local_nic_info config_gre_channel color_echogreen "GREChannelconfigurationcomplete." brctlshow echo "-------------------------------------------------" ovs-vsctlshow 1、配置minion主机root SSH登录 先将config_gre_channel.sh、config_ssh_root_remote.sh和kubernetes-install.sh这三个文件上传各自的minion主机,以备后用。 说明:操作成功,root密码为123 2、配置master主机与minion主机SSH免交互认证 将kubernetes-install.sh、ssh_keypair_auth.sh和etcd-v2.2.2-linux-amd64.tar.gz、kubernetes.tar.gz安装包上传到master主机 3、安装master端 说明:先解压文件,在创建启动选项文件,再启动服务,最后将minion相关包通过scp工具拷贝过去。通过ps工具看到服务进程已经启动。 4、安装minion端 说明:Docker源秘钥注册失败,没关系,一般不会影响安装。当提示安装docker成功后才能继续,否则报错退出。通过ps 工具看到服务进程都已经启动。 5、配置GRE通道 说明:目前配置GRE通道脚本只支持两台Docker主机 6、查看集群是否配置成功 # 通过kubectl命令创建一个pod 说明:在master端可以看到两台minion端状态是Ready,并且创建的Pod已经成功运行。 注意:如果没有kubectl命令,先尝试执行source /etc/profile下看看,如果还没有,直接使用/opt/bin/kubectl 本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1738807,如需转载请自行联系原作者

优秀的个人博客,低调大师

关于hadoop 1.2.1版本不支持“dfs.support.append”参数的解决办法

最近在测试hadoop+fluentd方案,但是fluentd日志收集系统,需要append功能选择,写日志到HDFS中,官方给出的解决方案是: 修改hdfs-site.xml 文件,增加如下行: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <property> <name>dfs.webhdfs.enabled< /name > <value> true < /value > < /property > <property> <name>dfs.support.append< /name > <value> true < /value > < /property > <property> <name>dfs.support.broken.append< /name > <value> true < /value > < /property > 但是format namenode节点的时候会报错: [hadoop@node1 ~]$ hadoop namenode -format 14/12/25 10:35:25 INFO namenode.NameNode: STARTUP_MSG: /************************************************************ STARTUP_MSG: Starting NameNode STARTUP_MSG: host = node1.test.com/172.16.41.151 STARTUP_MSG: args = [-format] STARTUP_MSG: version = 1.2.1 STARTUP_MSG: build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1.2 -r 1503152; compiled by 'mattf' on Mon Jul 22 15:23:09 PDT 2013 STARTUP_MSG: java = 1.7.0_67 ************************************************************/ 14/12/25 10:35:25 INFO util.GSet: Computing capacity for map BlocksMap 14/12/25 10:35:25 INFO util.GSet: VM type = 64-bit 14/12/25 10:35:25 INFO util.GSet: 2.0% max memory = 932184064 14/12/25 10:35:25 INFO util.GSet: capacity = 2^21 = 2097152 entries 14/12/25 10:35:25 INFO util.GSet: recommended=2097152, actual=2097152 14/12/25 10:35:26 INFO namenode.FSNamesystem: fsOwner=hadoop 14/12/25 10:35:26 INFO namenode.FSNamesystem: supergroup=supergroup 14/12/25 10:35:26 INFO namenode.FSNamesystem: isPermissionEnabled=true 14/12/25 10:35:26 INFO namenode.FSNamesystem: dfs.block.invalidate.limit=100 14/12/25 10:35:26 WARN namenode.FSNamesystem: The dfs.support.append option is in your configuration, however append is not supported. This configuration option is no longer required to enable sync 14/12/25 10:35:26 INFO namenode.FSNamesystem: isAccessTokenEnabled=false accessKeyUpdateInterval=0 min(s), accessTokenLifetime=0 min(s) 14/12/25 10:35:26 INFO namenode.FSEditLog: dfs.namenode.edits.toleration.length = 0 14/12/25 10:35:26 INFO namenode.NameNode: Caching file names occuring more than 10 times 14/12/25 10:35:26 INFO common.Storage: Image file /usr/local/hadoop_tmp/dfs/name/current/fsimage of size 112 bytes saved in 0 seconds. 14/12/25 10:35:26 INFO namenode.FSEditLog: closing edit log: position=4, editlog=/usr/local/hadoop_tmp/dfs/name/current/edits 14/12/25 10:35:26 INFO namenode.FSEditLog: close success: truncate to 4, editlog=/usr/local/hadoop_tmp/dfs/name/current/edits 14/12/25 10:35:26 INFO common.Storage: Storage directory /usr/local/hadoop_tmp/dfs/name has been successfully formatted. 网上搜索半天,最好解决办法如下: 用一下语句: 1 2 3 4 <property> <name>dfs.support.broken.append< /name > <value> true < /value > < /property > 然后再次格式化namenode,就不报错了! [hadoop@node1 conf]$ hadoop namenode -format 14/12/25 10:47:18 INFO namenode.NameNode: STARTUP_MSG: /************************************************************ STARTUP_MSG: Starting NameNode STARTUP_MSG: host = node1.test.com/172.16.41.151 STARTUP_MSG: args = [-format] STARTUP_MSG: version = 1.2.1 STARTUP_MSG: build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1.2 -r 1503152; compiled by 'mattf' on Mon Jul 22 15:23:09 PDT 2013 STARTUP_MSG: java = 1.7.0_67 ************************************************************/ Re-format filesystem in /usr/local/hadoop_tmp/dfs/name ? (Y or N) Y 14/12/25 10:47:21 INFO util.GSet: Computing capacity for map BlocksMap 14/12/25 10:47:21 INFO util.GSet: VM type = 64-bit 14/12/25 10:47:21 INFO util.GSet: 2.0% max memory = 932184064 14/12/25 10:47:21 INFO util.GSet: capacity = 2^21 = 2097152 entries 14/12/25 10:47:21 INFO util.GSet: recommended=2097152, actual=2097152 14/12/25 10:47:21 INFO namenode.FSNamesystem: fsOwner=hadoop 14/12/25 10:47:21 INFO namenode.FSNamesystem: supergroup=supergroup 14/12/25 10:47:21 INFO namenode.FSNamesystem: isPermissionEnabled=true 14/12/25 10:47:21 INFO namenode.FSNamesystem: dfs.block.invalidate.limit=100 14/12/25 10:47:21 INFO namenode.FSNamesystem: isAccessTokenEnabled=false accessKeyUpdateInterval=0 min(s), accessTokenLifetime=0 min(s) 14/12/25 10:47:21 INFO namenode.FSEditLog: dfs.namenode.edits.toleration.length = 0 14/12/25 10:47:21 INFO namenode.NameNode: Caching file names occuring more than 10 times 14/12/25 10:47:21 INFO common.Storage: Image file /usr/local/hadoop_tmp/dfs/name/current/fsimage of size 112 bytes saved in 0 seconds. 14/12/25 10:47:21 INFO namenode.FSEditLog: closing edit log: position=4, editlog=/usr/local/hadoop_tmp/dfs/name/current/edits 14/12/25 10:47:21 INFO namenode.FSEditLog: close success: truncate to 4, editlog=/usr/local/hadoop_tmp/dfs/name/current/edits 14/12/25 10:47:21 INFO common.Storage: Storage directory /usr/local/hadoop_tmp/dfs/name has been successfully formatted. 14/12/25 10:47:21 INFO namenode.NameNode: SHUTDOWN_MSG: /************************************************************ SHUTDOWN_MSG: Shutting down NameNode at node1.test.com/172.16.41.151 ************************************************************/ 本文转自 shine_forever 51CTO博客,原文链接:http://blog.51cto.com/shineforever/1595776

优秀的个人博客,低调大师

嵌入式EasyHMI V0.1版终于推出,C#真是软件开发的利器

这段时间就和闭关一样,连续几周一直在开发嵌入式EasyHMI,从架构到具体的代码,有上位机的IDE开发环境到WinCE上的运行时环境,有TCP/IP、COM通信,有多线程,多缓存图形处理,XML序列化,有EVC,有C#,有.net2.0的框架集,有.net2.0精简框架集,一时云集,一阵天昏地暗之后终于成型,是可喜可贺,但腰酸背疼的不得了。 不过这里不得不要夸C#两句,C#真是我目前用到的最好,最精妙的语言,确令我爱不释手。想想如果不是基于C#开发,想在短短的一个半月是不可能完成到今天的程度的。 不过开发运行时,就没有这么爽,精简框架集有很多限制,比如图元不能直接支持双缓冲,不能绘渐变色,不能读取ini文件,不能二进制序列化,不能绘制扇形等等,有时间我会一一道来。 下面是程序的两个界图,效果还可以吧:) EasyHMI IDE开发环境 EasyHMI 模拟器 目前程序开发基本上告一段落,以后的时间相对会比较充裕一些,我会一一把遇到的问题写一下,否则时间长了,我也会忘记的。 本文转自yefanqiu51CTO博客,原文链接:,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android 屏幕适配(二)增强版百分比布局库(percent-support-lib)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/46767825; 本文出自:【张鸿洋的博客】 一 概述 上周一我们发布了Android 百分比布局库(percent-support-lib) 解析与扩展中对percent-support这个库进行了解析和添加了PercentLinearLayout的支持。 那么为什么本篇博客的存在的意义是什么呢? 首先我们回顾下百分比布局库的用法,提供了PercentRelativeLayout、PercentFrameLayout供大家在编写的时候,对于以下属性: layout_widthPercent、layout_heightPercent、layout_marginPercent、layout_marginLeftPercent、layout_marginTopPercent、layout_marginRightPercent、layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。 可以使用百分比进行设置宽、高、边距,的确给我们在适配上提供了极大的便利,但是在使用过程中,觉得存在一些场景无法得到满足。什么场景呢?下面我举几个例子。 当使用图片时,无法设置宽高的比例 比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。 很难使用百分比定义一个正方形的控件 比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到。 一个控件的margin四个方向值一致 有些时候,我设置margin,我希望四边的边距一致的,但是如果目前设置5%,会造成,上下为高度的5%,左右边距为宽度的5%。 综合上述这些问题,可以发现目前的percent-support-lib并不能完全满足我们的需求,所以我们考虑对其进行扩展。说白了,我们就希 望在布局的时候可以自己设定参考看度还是高度,比如上述2,我们对于宽高可以写成10%w,10%w。也就是在不改变原库的用法的前提下,添加一些额外的 支持。 二 扩展的功能 目前我初步对该库进行了改写,github地址:android-percent-support-extend,对于官方库,做了如下的改变: 不改变原有库的用法 添加了PercentLinearLayout 支持百分比指定特定的参考值,比如宽度或者高度。 例如:app:layout_heightPercent="50%w",app:layout_marginPercent="15%w",app:layout_marginBottomPercent="20%h". 支持通过app:layout_textSizePercent设置textView的textSize 对于外层套ScrollView的问题,目前可以在PercentLinearLayout的外层使用ScrollView,不过对于宽度的百分比参考的就是android.R.id.content的高度(因为,无法参考父控件的高度,父控件的高度理论上依赖于子View高度,且模式为UNSPECIFIED)。 对于如何导入,也是相当的简单,android studio的用户,直接: dependencies { //... compile 'com.zhy:percent-support-extends:1.0.1' } 不需要导入官方的percent-support-lib了。 对于的三个类分别为: com.zhy.android.percent.support.PercentLinearLayout com.zhy.android.percent.support.PercentRelativeLayout com.zhy.android.percent.support.PercentFrameLayout 对于eclipse的用户:github上自行下载源码,就几个类和一个attrs.xml,也可以在bintray.com/percent-support-extends下载相关文件。 下面看几个具体的示例。 三 具体的示例 Demo 1 xml: <?xml version="1.0" encoding="utf-8"?> <com.zhy.android.percent.support.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.zhy.android.percent.support.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="center" android:background="#ff44aacc" app:layout_heightPercent="50%w" app:layout_widthPercent="50%w"> <com.zhy.android.percent.support.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="center" android:background="#ffcc5ec7" app:layout_heightPercent="50%w" app:layout_widthPercent="50%w"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="#ff7ecc16" android:gravity="center" android:text="margin 15% of w" app:layout_marginPercent="15%w" /> </com.zhy.android.percent.support.PercentFrameLayout> </com.zhy.android.percent.support.PercentFrameLayout> <TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="bottom|right" android:background="#44ff0000" android:gravity="center" android:text="15%w,15%w" app:layout_heightPercent="15%w" app:layout_marginPercent="5%w" app:layout_widthPercent="15%w"/> </com.zhy.android.percent.support.PercentFrameLayout> Demo 2 xml: <?xml version="1.0" encoding="utf-8"?> <com.zhy.android.percent.support.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true"> <TextView android:id="@+id/row_one_item_one" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:background="#7700ff00" android:text="w:70%,h:20%" android:gravity="center" app:layout_heightPercent="20%" app:layout_widthPercent="70%"/> <TextView android:id="@+id/row_one_item_two" android:layout_width="0dp" android:layout_height="0dp" android:layout_toRightOf="@+id/row_one_item_one" android:background="#396190" android:text="w:30%,h:20%" app:layout_heightPercent="20%" android:gravity="center" app:layout_widthPercent="30%"/> <ImageView android:id="@+id/row_two_item_one" android:layout_width="match_parent" android:layout_height="0dp" android:src="@drawable/tangyan" android:scaleType="centerCrop" android:layout_below="@+id/row_one_item_one" android:background="#d89695" app:layout_heightPercent="70%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_below="@id/row_two_item_one" android:background="#770000ff" android:gravity="center" android:text="width:100%,height:10%" app:layout_heightPercent="10%" app:layout_widthPercent="100%"/> </com.zhy.android.percent.support.PercentRelativeLayout> ok,例子都比较简单,主要就一个布局文件,可以看出上述我们可以给宽度、高度,边距等指定参考值为宽度或者高度。这样的话,在保证图片宽、高比例、控件设置为正方形等需求就没问题了。 接下来还有个例子,功能主要是设置TextView对于textSize的百分比设置;以及对于ScrollView的支持。当然了,对于ScrollView的支持,这个理论上是不支持的,因为大家都清楚,如果PercentLinearLayout在ScrollView中,那么高度的模式肯定是UNSPECIFIED, 那么理论上来说高度是无限制的,也就是依赖于子View的高度,而百分比布局的高度是依赖于父View的高度的,所有是互斥的。而我们支持是:考虑到编写 代码的时候,大多参考的是屏幕高度(android.R.id.content)的高度,所以如果在ScrollView中,编写10%h,这个百分比是 依赖于屏幕高度的(不包括ActionBar的高度)。 Demo 3 xml: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.zhy.android.percent.support.PercentLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:60%,height:5%,ts:3%" android:textColor="#ffffff" app:layout_heightPercent="5%" app:layout_marginBottomPercent="5%" app:layout_textSizePercent="3%" app:layout_widthPercent="60%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff4400cc" android:gravity="center" android:text="width:70%,height:10%" android:textColor="#ffffff" app:layout_heightPercent="10%" app:layout_marginBottomPercent="5%" app:layout_widthPercent="70%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="w:80%,h:15%,textSize:5%" android:textColor="#ffffff" app:layout_heightPercent="15%" app:layout_marginBottomPercent="5%" app:layout_textSizePercent="5%" app:layout_widthPercent="80%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff4400cc" android:gravity="center" android:text="width:90%,height:5%" android:textColor="#ffffff" app:layout_heightPercent="20%" app:layout_marginBottomPercent="5%" app:layout_widthPercent="90%"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:100%,height:25%" android:textColor="#ffffff" app:layout_heightPercent="25%" app:layout_marginBottomPercent="5%" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:100%,height:30%" android:textColor="#ffffff" app:layout_heightPercent="30%" app:layout_marginBottomPercent="5%" /> </com.zhy.android.percent.support.PercentLinearLayout> </ScrollView> 上面的第三个TextView的字体设置的就是5%(默认参考容器高度)。整个PercentLinearLayout在ScrollView中。ok~ 姑且这样,由于源码比较简单,大家可以根据自己的实际需求去修改,前提尽可能不要改变原有的功能。 四 扩展的相关源码 (一) 关于attrs.xml 原库中所有的属性的format为fraction,但是由于我期望的写法有10%w,10%h,10%,没有找到合适的format,就直接定义为string了~string我可以自己去解析~ <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="PercentLayout_Layout"> <attr name="layout_widthPercent" format="string"/> <attr name="layout_heightPercent" format="string"/> <attr name="layout_marginPercent" format="string"/> <attr name="layout_marginLeftPercent" format="string"/> <attr name="layout_marginTopPercent" format="string"/> <attr name="layout_marginRightPercent" format="string"/> <attr name="layout_marginBottomPercent" format="string"/> <attr name="layout_marginStartPercent" format="string"/> <attr name="layout_marginEndPercent" format="string"/> <attr name="layout_textSizePercent" format="string"/> </declare-styleable> </resources> (二) 获取自定义属性的值及使用 如果看了上篇博文的话,应该清楚,对于自定义属性的值是在PercentLayoutHelper.getPercentLayoutInfo(c, attrs)中获取的。 简单看下修改后的代码: public static PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs) { PercentLayoutInfo info = null; TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PercentLayout_Layout); String sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_widthPercent); PercentLayoutInfo.PercentVal percentVal = getPercentVal(sizeStr, true); if (percentVal != null) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "percent width: " + percentVal.percent); } info = info != null ? info : new PercentLayoutInfo(); info.widthPercent = percentVal; } //省略了获取其他的类似属性 array.recycle(); return info; } private static final String REGEX_PERCENT = "^(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)%([wh]?)$"; /** * widthStr to PercentVal * <br/> * eg: 35%w => new PercentVal(35, true) * * @param percentStr * @param isOnWidth * @return */ private static PercentLayoutInfo.PercentVal getPercentVal(String percentStr, boolean isOnWidth) { //valid param if (percentStr == null) { return null; } Pattern p = Pattern.compile(REGEX_PERCENT); Matcher matcher = p.matcher(percentStr); if (!matcher.matches()) { throw new RuntimeException("the value of layout_xxxPercent invalid! ==>" + percentStr); } int len = percentStr.length(); //extract the float value String floatVal = matcher.group(1); String lastAlpha = percentStr.substring(len - 1); float percent = Float.parseFloat(floatVal) / 100f; boolean isBasedWidth = (isOnWidth && !lastAlpha.equals("h")) || lastAlpha.equals("w"); return new PercentLayoutInfo.PercentVal(percent, isBasedWidth); } 首先我们获取自定义属性的填写的值,通过getPercentVal方法,在该方法内部通过正则校验其合法性,如果合法,则将其拆解封装成 PercentVal对象,该对象中记录百分比值,已经知否参考宽度的布尔值(如果参考宽度则为true,否则为false)。对于没有后缀w|h的,和 原库的解析方式相同。 PercentVal对象如下: public static class PercentVal { public float percent = -1; public boolean isBaseWidth; public PercentVal(float percent, boolean isBaseWidth) { this.percent = percent; this.isBaseWidth = isBaseWidth; } } 对于定义的自定义属性获取完成之后,剩下的无非是测量时候对于原本的LayoutParams中的宽度和高度的赋值做简单的修改。参考上一篇的源码,我们直接看PercentLayoutInfo.fillLayoutParams(params, widthHint, heightHint);方法: public void fillLayoutParams(ViewGroup.LayoutParams params, int widthHint, int heightHint) { // Preserve the original layout params, so we can restore them after the measure step. mPreservedParams.width = params.width; mPreservedParams.height = params.height; /* if (widthPercent >= 0) { params.width = (int) (widthHint * widthPercent); } if (heightPercent >= 0) { params.height = (int) (heightHint * heightPercent); }*/ if (widthPercent != null) { int base = widthPercent.isBaseWidth ? widthHint : heightHint; params.width = (int) (base * widthPercent.percent); } if (heightPercent != null) { int base = heightPercent.isBaseWidth ? widthHint : heightHint; params.height = (int) (base * heightPercent.percent); } if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "after fillLayoutParams: (" + params.width + ", " + params.height + ")"); } } 原本的源码比较简单,只需要将widthHint/heightHint乘以百分比即可(见上代码注释),而我们修改的也比较容易,首先判断参考宽度还是高度,然后乘以百分比(根据我们的对象PercentVal的属性)。 ok,大概的源码修改就是上述的内容,有兴趣的可以直接查看源码。 当然了,上述库中肯定还存在或多或少的问题,大家可以fork完善下,或者直接留言提意见都可以。 github地址:android-percent-support-extend,用法参考上文,或者README。欢迎star and fork 。 ~~have a nice day ~~ ok~ 本文转自 一点点征服 博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/5293018.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

安卓客户端和服务器端的通信(勘误填坑版)

版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/78328405 一个很基础的socket链接示例,仅用作测试和学习使用 首先是服务器端的代码,一个标准的socket server,绑定本地的54321端口 package online.geekgalaxy.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SocketServer implements Runnable { public void run() { try { //创建ServerSocket ServerSocket serverSocket = new ServerSocket(54321); while (true) { //接受客户端请求 Socket client = serverSocket.accept(); System.out.println("accept"); try { //接收客户端消息 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = in.readLine(); System.out.println("read:" + str); //向服务器发送消息 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true); out.println("server message"); //关闭流 out.close(); in.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { //关闭 client.close(); System.out.println("close"); } } } catch (Exception e) { System.out.println(e.getMessage()); } } //main函数,开启服务器 public static void main(String a[]) { Thread desktopServerThread = new Thread(new SocketServer()); desktopServerThread.start(); } } 附加一个python的服务端 #!/usr/bin/python # -*- coding: UTF-8 -*- import socket s = socket.socket() host = socket.gethostname() port = 54321 s.bind((host, port)) s.listen(5) while True: c, addr = s.accept() print 'Connect from ', addr c.send('Welcome!') c.close() 然后是安卓客户端,很简单,但是连接时不能使用127.0.0.1,所以使用了局域网的10段地址 manifest文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="online.geekgalaxy.socketclient"> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="5" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>mainactivity文件,使用了一种不是特别推荐的方式在新的API基础上运行socket客户端命令 package online.geekgalaxy.socketclient; import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements Runnable{ // @Override // protected void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // } private final String DEBUG_TAG = "MainActivity"; private TextView mTextView = null; private EditText mEditText = null; private Button mButton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); mButton = (Button)findViewById(R.id.Button01); mTextView = (TextView)findViewById(R.id.TextView01); mEditText = (EditText)findViewById(R.id.EditText01); //登陆 mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { run(); } }); } @Override public void run() { Socket socket = null; String message = mEditText.getText().toString() + "/r/n"; try { //创建Socket // socket = new Socket("192.168.1.110",54321); socket = new Socket("10.9.3.132", 54321); //IP:10.14.114.127,端口54321 //向服务器发送消息 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); out.println(message); //接收来自服务器的消息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); if (msg != null) { mTextView.setText(msg); } else { mTextView.setText("数据错误!"); } //关闭流 out.close(); br.close(); //关闭Socket socket.close(); } catch (Exception e) { // TODO: handle exception Log.e(DEBUG_TAG, e.toString()); } } } mainactivity推荐的写法,使用runnable,是上面代码的替代方案 package online.geekgalaxy.socketclient; //import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity{ // @Override // protected void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // } private final String DEBUG_TAG = "MainActivity"; private TextView mTextView = null; private EditText mEditText = null; private Button mButton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); // StrictMode.setThreadPolicy(policy); mButton = (Button)findViewById(R.id.Button01); mTextView = (TextView)findViewById(R.id.TextView01); mEditText = (EditText)findViewById(R.id.EditText01); //登陆 mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // run(); new Thread(runnable).start(); } }); } Runnable runnable = new Runnable() { @Override public void run() { Socket socket; String message = mEditText.getText().toString() + "/r/n"; try { //创建Socket // socket = new Socket("192.168.1.110",54321); socket = new Socket("10.9.3.132", 54321); //IP:10.14.114.127,端口54321 //向服务器发送消息 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); out.println(message); //接收来自服务器的消息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); if (msg != null) { mTextView.setText(msg); } else { mTextView.setText("数据错误!"); } //关闭流 out.close(); br.close(); //关闭Socket socket.close(); } catch (Exception e) { // TODO: handle exception Log.e(DEBUG_TAG, e.toString()); } } }; } 然后是layout文件,里边的string被提取了出来,自己补全即可 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/servermessage" /> <EditText android:id="@+id/EditText01" android:text="@string/sendmessage" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/buttonsend" /> </LinearLayout> build.gradle还是贴出来吧 apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "online.geekgalaxy.socketclient" minSdkVersion 25 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }

优秀的个人博客,低调大师

《社交网站界面设计(原书第2版)》——1.15 让交互更有人情味

1.15 让交互更有人情味 为这些新物理接口创建自然的交互可以降低屏幕的重要性。随着物联网逐渐扩展到生活的空间、物体,甚至是周围的人,我们应该将交互移出屏幕并移动到我们周围的环境中。屏幕将人们彼此分隔。我们越是连通,我们与周围的人和场所越是分隔。在餐桌、床、办公室和街上,我们都是低头看着我们的设备,而不是抬头看着这个世界。我们可以做得更好。通过包含(结合)传感器,现在我们拥有了一个创造交互的新机会,它符合我们与世界交互的方式。——约什·克拉克(Josh Clark)

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码