Skip to content

Commit acff125

Browse files
mysql
1 parent 9e595f4 commit acff125

21 files changed

Lines changed: 1490 additions & 73 deletions

File tree

7.75 KB
Loading
59.7 KB
Loading
26.4 KB
Loading

notes/IO、网络编程、NIO、AIO/NIO.md

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ public class ChatServer {
755755
客户端
756756

757757
```java
758-
package chat;
758+
package manybuffers;
759759

760760
import java.io.BufferedReader;
761761
import java.io.InputStreamReader;
@@ -774,72 +774,72 @@ public class ChatClient {
774774
public static void main(String[] args) {
775775
try {
776776
SocketChannel socketChannel = SocketChannel.open();
777-
socketChannel.configureBlocking(false) ;
777+
socketChannel.configureBlocking(false);
778778
Selector selector = Selector.open();
779779
//注册“连接事件”
780-
socketChannel.register( selector , SelectionKey.OP_CONNECT) ;
780+
socketChannel.register(selector, SelectionKey.OP_CONNECT);
781781

782-
int[] ports = {7777,8888,9999} ;
783-
int port = ports[ (int)(Math.random()*3) ] ;
784-
socketChannel.connect(new InetSocketAddress("127.0.0.1",port)) ;
782+
int[] ports = {7777, 8888, 9999};
783+
int port = ports[(int) (Math.random() * 3)];
784+
socketChannel.connect(new InetSocketAddress("127.0.0.1", port));
785785

786-
while(true){
787-
selector.select() ;
786+
while (true) {
787+
selector.select();
788788
//selectionKeys:包含了所有的事件
789789
Set<SelectionKey> selectionKeys = selector.selectedKeys();
790790

791791
Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
792-
while(keyIterator.hasNext() ){
792+
while (keyIterator.hasNext()) {
793793

794794
SelectionKey selectionKey = keyIterator.next();//每个事件
795795
//真实的发生“连接事件”
796-
if(selectionKey.isConnectable()){ //连接完毕?接收(读)、发送(写)
796+
if (selectionKey.isConnectable()) { //连接完毕?接收(读)、发送(写)
797797
//buffer + channel
798798
ByteBuffer sendBuffer = ByteBuffer.allocate(1024);
799-
SocketChannel clientChannel = (SocketChannel)selectionKey.channel();
799+
SocketChannel clientChannel = (SocketChannel) selectionKey.channel();
800800

801801

802-
if(clientChannel.isConnectionPending()){//正在连接
802+
if (clientChannel.isConnectionPending()) {//正在连接
803803

804-
if(clientChannel.finishConnect()){
805-
System.out.println("连接服务端成功,连接的端口是:"+port);
804+
if (clientChannel.finishConnect()) {
805+
System.out.println("连接服务端成功,连接的端口是:" + port);
806806
//向服务端 发送一条测试数据
807-
sendBuffer.put("connecting".getBytes()) ;
808-
sendBuffer.flip() ;
809-
clientChannel.write(sendBuffer) ;
810-
807+
sendBuffer.put("connecting".getBytes());
808+
sendBuffer.flip();
809+
clientChannel.write(sendBuffer);
811810
}
812811
}
813812
//在客户端看来,“写操作”不需要注册到通道中,再去使用?
814813

815814
//客户端,每次写操作,创建一个线程
816-
new Thread( ()->{
817-
try {
818-
sendBuffer.clear();
819-
//写数据: 接收用户从控制台输入的内容
820-
InputStreamReader reader = new InputStreamReader(System.in);
821-
BufferedReader bReader = new BufferedReader(reader);
822-
String message = bReader.readLine();
823-
824-
sendBuffer.put(message.getBytes()) ;
825-
sendBuffer.flip() ;
826-
clientChannel.write( sendBuffer) ;
827-
828-
//发送数据
829-
}catch (Exception e){
830-
e.printStackTrace();
815+
new Thread(() -> {
816+
while (true) {
817+
try {
818+
sendBuffer.clear();
819+
//写数据: 接收用户从控制台输入的内容
820+
InputStreamReader reader = new InputStreamReader(System.in);
821+
BufferedReader bReader = new BufferedReader(reader);
822+
String message = bReader.readLine();
823+
824+
sendBuffer.put(message.getBytes());
825+
sendBuffer.flip();
826+
clientChannel.write(sendBuffer);
827+
828+
//发送数据
829+
} catch (Exception e) {
830+
e.printStackTrace();
831+
}
831832
}
832-
833-
} ) .start();
833+
}).start();
834834

835835

836836
//发送数据(写)
837-
clientChannel.register( selector,SelectionKey.OP_READ) ;
838-
}else if(selectionKey.isReadable() ){//
837+
clientChannel.register(selector, SelectionKey.OP_READ);
838+
} else if (selectionKey.isReadable()) {//
839839
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
840-
SocketChannel clientChannel = (SocketChannel)selectionKey.channel();
840+
SocketChannel clientChannel = (SocketChannel) selectionKey.channel();
841841
int len = clientChannel.read(readBuffer);//
842-
if(len>0){
842+
if (len > 0) {
843843
String receive = new String(readBuffer.array(), 0, len);
844844
System.out.println(receive);
845845
}
@@ -848,7 +848,7 @@ public class ChatClient {
848848

849849
selectionKeys.clear();
850850
}
851-
}catch (Exception e){
851+
} catch (Exception e) {
852852
e.printStackTrace();
853853
}
854854
}
@@ -866,7 +866,66 @@ public class ChatClient {
866866

867867

868868

869+
### 用同一个channel读取多个缓冲区
870+
871+
注意:如果有2个缓冲区a\b; 先把a读满,再去读取b
872+
873+
![1582010264341](NIO.assets/1582010264341.png)
874+
875+
```java
876+
package manybuffers;
877+
878+
import java.net.InetSocketAddress;
879+
import java.net.ServerSocket;
880+
import java.nio.ByteBuffer;
881+
import java.nio.channels.ServerSocketChannel;
882+
import java.nio.channels.SocketChannel;
883+
884+
/*
885+
* Created by 颜群
886+
*/
887+
public class NIOServer {
888+
public static void main(String[] args) throws Exception{
889+
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
890+
ServerSocket serverSocket = serverSocketChannel.socket();
891+
serverSocket.bind( new InetSocketAddress(8888)) ;
892+
//定义两个缓冲区,接受客户端的数据
893+
ByteBuffer[] buffers = new ByteBuffer[2] ;
894+
buffers[0] = ByteBuffer.allocate(4) ;
895+
buffers[1] = ByteBuffer.allocate(8) ;
896+
int bufferSize = 4 + 8 ;//总容量
897+
898+
SocketChannel socketChannel = serverSocketChannel.accept();
899+
System.out.println("服务端:接收到客户端连接。。。");
900+
while(true){//接收数据
901+
int totalRead = 0 ;//每一次实际使用
902+
//如果buffer没满,继续读
903+
while(totalRead < bufferSize){
904+
long read = socketChannel.read(buffers);
905+
totalRead += read ;
906+
System.out.println("【每一次】,实际读取到的数据大小:"+read);
907+
// System.out.println("实际读取到的【总数据】大小:"+totalRead);
908+
}
909+
//如果buffer已满, flip() ;
910+
for(ByteBuffer buffer:buffers){
911+
buffer.flip() ;
912+
}
913+
}
914+
915+
}
916+
}
917+
918+
```
919+
920+
客户端沿用之前的client
921+
922+
buffer1: 4
923+
buffer2: 8
924+
服务端buffers:共12
869925

870926

927+
客户端发送: 10 ,11(2+9) + 4(3xxxx+1)
871928

929+
![1582011476690](NIO.assets/1582011476690.png)
872930

931+
![1582011490116](NIO.assets/1582011490116.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
A=`ps -C haproxy --no-header |wc -l`
3+
if [ $A -eq 0 ];then
4+
systemctl start haproxy.service
5+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#---------------------------------------------------------------------
2+
# Example configuration for a possible web application. See the
3+
# full configuration options online.
4+
#
5+
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
6+
#
7+
#---------------------------------------------------------------------
8+
9+
#---------------------------------------------------------------------
10+
# Global settings
11+
#---------------------------------------------------------------------
12+
global
13+
# to have these messages end up in /var/log/haproxy.log you will
14+
# need to:
15+
#
16+
# 1) configure syslog to accept network log events. This is done
17+
# by adding the '-r' option to the SYSLOGD_OPTIONS in
18+
# /etc/sysconfig/syslog
19+
#
20+
# 2) configure local2 events to go to the /var/log/haproxy.log
21+
# file. A line like the following can be added to
22+
# /etc/sysconfig/syslog
23+
#
24+
# local2.* /var/log/haproxy.log
25+
#
26+
log 127.0.0.1 local2
27+
28+
chroot /var/lib/haproxy
29+
pidfile /var/run/haproxy.pid
30+
maxconn 4000
31+
user haproxy
32+
group haproxy
33+
daemon
34+
35+
# turn on stats unix socket
36+
stats socket /var/lib/haproxy/stats
37+
38+
#---------------------------------------------------------------------
39+
# common defaults that all the 'listen' and 'backend' sections will
40+
# use if not designated in their block
41+
#---------------------------------------------------------------------
42+
defaults
43+
mode tcp
44+
log global
45+
option tcplog
46+
option dontlognull
47+
option http-server-close
48+
option forwardfor except 127.0.0.0/8
49+
option redispatch
50+
retries 3
51+
timeout http-request 10s
52+
timeout queue 1m
53+
timeout connect 10s
54+
timeout client 1m
55+
timeout server 1m
56+
timeout http-keep-alive 10s
57+
timeout check 10s
58+
maxconn 3000
59+
60+
#---------------------------------------------------------------------
61+
# main frontend which proxys to the backends
62+
#---------------------------------------------------------------------
63+
frontend mycat
64+
bind 0.0.0.0:8066
65+
bind 0.0.0.0:9066
66+
mode tcp
67+
log global
68+
default_backend mycat_server
69+
70+
#---------------------------------------------------------------------
71+
# static backend for serving up images, stylesheets and such
72+
#---------------------------------------------------------------------
73+
backend mycat_server
74+
balance roundrobin
75+
server mycat1 192.168.2.129:8066 check inter 5s rise 2 fall 3
76+
server mycat2 192.168.2.130:8066 check inter 5s rise 2 fall 3
77+
server mycatadmin1 192.168.2.129:9066 check inter 5s rise 2 fall 3
78+
server mycatadmin2 192.168.2.130:9066 check inter 5s rise 2 fall 3
79+
listen stats
80+
mode http
81+
bind 0.0.0.0:5000
82+
stats enable
83+
stats hide-version
84+
stats uri /haproxy
85+
stats realm Haproxy\ Statistics
86+
stats auth admin:admin
87+
stats admin if TRUE
88+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
global_defs {
2+
router_id NodeA
3+
}
4+
5+
vrrp_script chk_haproxy {
6+
script "/etc/check_haproxy.sh"
7+
interval 4
8+
weight 3
9+
}
10+
vrrp_instance VI_1 {
11+
state MASTER
12+
interface ens33
13+
virtual_router_id 10
14+
priority 100
15+
advert_int 1
16+
track_script {
17+
chk_haproxy
18+
}
19+
20+
authentication {
21+
auth_type PASS
22+
auth_pass 1234
23+
}
24+
virtual_ipaddress {
25+
192.168.2.222/24
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
����mysql��һ��Ȩ��(root)
2+
3+
mycat-> a ,b ,��a,b������е�Mysql��ͨ������ִ�� ����Ȩ��(root)
4+
5+
mycat-> bigdata/bigdata01
6+
use mysql ;
7+
update user set
8+
`Select_priv` = 'Y', `Insert_priv` = 'Y',`Update_priv` = 'Y',`Delete_priv` = 'Y',
9+
`Create_priv` = 'Y',`Drop_priv` = 'Y',`Reload_priv` = 'Y',`Shutdown_priv` = 'Y',
10+
`Process_priv` = 'Y',`File_priv` = 'Y',`Grant_priv` = 'Y',`References_priv` = 'Y',
11+
`Index_priv` = 'Y',`Alter_priv` = 'Y',`Show_db_priv` = 'Y',`Super_priv` = 'Y',
12+
`Create_tmp_table_priv` = 'Y',`Lock_tables_priv` = 'Y',`Execute_priv` = 'Y',
13+
`Repl_slave_priv` = 'Y',`Repl_client_priv` = 'Y',`Create_view_priv` = 'Y',
14+
`Show_view_priv` = 'Y',`Create_routine_priv` = 'Y',`Alter_routine_priv` = 'Y',
15+
`Create_user_priv` = 'Y',`Event_priv` = 'Y',`Trigger_priv` = 'Y',`Create_tablespace_priv` = 'Y'
16+
where user="root" ;
17+
18+
flush privileges;
272 KB
Loading

0 commit comments

Comments
 (0)