keepalived實現nginx負載均衡機高可用

来源:https://www.cnblogs.com/Their-own/archive/2022/10/17/16800530.html
-Advertisement-
Play Games

keepalived實現nginx負載均衡機高可用 環境說明: | 系統 | 主機名 | IP | 服務 | | | | | | | centos8 | master | 192.168.111.141 | nginxkeepalived | | centos8 | backup | 192.168 ...


keepalived實現nginx負載均衡機高可用

目錄

環境說明:

系統 主機名 IP 服務
centos8 master 192.168.111.141 nginx
keepalived
centos8 backup 192.168.111.142 nginx
keepalived
centos8 apache 192.168.111.143 httpd
centos8 nginx 192.168.111.144 nginx

配置web界面

apache

//修改名字
[root@localhost ~]# hostnamectl set-hostname apache
[root@localhost ~]# bash
[root@apache ~]# 

//關閉防火牆和selinux
[root@apache ~]# setenforce 0
[root@apache ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@apache ~]# systemctl disable --now firewalld
[root@apache ~]# reboot

//配置yum源
[root@apache ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@apache ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//安裝apache,主頁內容為apache
[root@apache ~]# yum -y install httpd
[root@apache ~]# echo "apache" > /var/www/html/index.html
[root@apache ~]# systemctl enable --now httpd
[root@apache ~]# ss -anlt
State        Recv-Q        Send-Q               Local Address:Port               Peer Address:Port       Process       
LISTEN       0             128                        0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0             128                              *:80                            *:*                        
LISTEN       0             128                           [::]:22                         [::]:*                        
[root@apache ~]# curl 192.168.111.143
apache

nginx

//修改名字
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# bash
[root@nginx ~]# 

//關閉防火牆和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# reboot

//配置yum源
[root@nginx ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@nginx ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//安裝nginx,主頁內容為nginx
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# echo "nginx" > /usr/share/nginx/html/index.html
[root@nginx ~]# systemctl enable --now nginx
[root@nginx ~]# ss -anlt
State        Recv-Q        Send-Q               Local Address:Port               Peer Address:Port       Process       
LISTEN       0             128                        0.0.0.0:80                      0.0.0.0:*                        
LISTEN       0             128                        0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0             128                           [::]:80                         [::]:*                        
LISTEN       0             128                           [::]:22                         [::]:*                        
[root@nginx ~]# curl 192.168.111.144
nginx

配置nginx負載均衡

master

//修改名字
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]# 

//關閉防火牆和selinux
[root@master ~]# setenforce 0
[root@master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# reboot

//配置yum源
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//創建用戶
[root@master ~]# useradd -rMs /sbin/nologin nginx

//安裝依賴包
[root@master ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim

//編譯安裝nginx
[root@backup ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@master ~]# tar xf nginx-1.22.0.tar.gz 
[root@master ~]# cd nginx-1.22.0
[root@master nginx-1.22.0]# ./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-debug \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module
 [root@master nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置環境變數
[root@master ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@master ~]# source /etc/profile.d/nginx.sh
                
//配置system啟動服務
[root@master ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@nginx ~]# systemctl daemon-reload

//啟動nginx
[root@master ~]# systemctl enable --now nginx
[root@master ~]# ss -anlt
State        Recv-Q       Send-Q               Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                        0.0.0.0:80                      0.0.0.0:*                        
LISTEN       0            128                        0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                           [::]:22                         [::]:*               

//修改配置文件,配置負載均衡
[root@master ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream web {				//http欄位內
    server 192.168.111.143;
    server 192.168.111.144;
    }
    .........
        location / {				//修改配置文件
        root html;
        proxy_pass   http://web;
        }
[root@master ~]# systemctl restart nginx

//查看負載均衡效果
[root@master ~]# curl 192.168.111.141
apache
[root@master ~]# curl 192.168.111.141
nginx
[root@master ~]# curl 192.168.111.141
apache
[root@master ~]# curl 192.168.111.141
nginx

backup

//修改名字
[root@localhost ~]# hostnamectl set-hostname backup
[root@localhost ~]# bash
[root@backup ~]# 

//關閉防火牆和selinux
[root@backup ~]# setenforce 0
[root@backup ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@backup ~]# systemctl disable --now firewalld
[root@backup ~]# reboot

//配置yum源
[root@backup ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@backup ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//創建用戶
[root@backup ~]# useradd -rMs /sbin/nologin nginx

//安裝依賴包
[root@backup ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim

//編譯安裝nginx
[root@backup ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@backup ~]# tar xf nginx-1.22.0.tar.gz 
[root@backup ~]# cd nginx-1.22.0
[root@backup nginx-1.22.0]# ./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-debug \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module
[root@backup nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置環境變數
[root@backup ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@backup ~]# source /etc/profile.d/nginx.sh
                
//配置system啟動服務
[root@backup ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@backup ~]# systemctl daemon-reload

//啟動nginx
[root@backup ~]# systemctl enable --now nginx
[root@backup ~]# ss -anlt
State        Recv-Q        Send-Q               Local Address:Port               Peer Address:Port       Process       
LISTEN       0             128                        0.0.0.0:80                      0.0.0.0:*                        
LISTEN       0             128                        0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0             128                           [::]:22                         [::]:*                        

//修改配置文件,配置負載均衡
[root@backup ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream web {				//http欄位內
    server 192.168.111.143;
    server 192.168.111.144;
    }
    .........
        location / {				//修改配置文件
        root html;
        proxy_pass   http://web;
        }
[root@backup ~]# systemctl restart nginx

//查看負載均衡效果
[root@backup ~]# curl 192.168.111.142
apache
[root@backup ~]# curl 192.168.111.142
nginx
[root@backup ~]# curl 192.168.111.142
apache
[root@backup ~]# curl 192.168.111.142
nginx

配置keepalived高可用

master

//首先安裝keepalived
[root@master ~]# yum -y install keepalived
 
//編輯配置文件,並啟動服務
[root@master ~]# mv /etc/keepalived/keepalived.conf{,.bak}
[root@master ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
 
global_defs {
   router_id lb01
}
 
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.111.250
    }
}
 
virtual_server 192.168.111.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.111.141 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 192.168.111.142 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@master ~]# systemctl enable --now keepalived

//通過虛擬IP訪問
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 1500sec preferred_lft 1500sec
    inet 192.168.111.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
 
//通過虛擬ip訪問
//一定要把backup端的nginx服務關掉才能訪問,

[root@master ~]# curl 192.168.111.250
apache
[root@master ~]# curl 192.168.111.250
nginx
[root@master ~]# curl 192.168.111.250
apache
[root@master ~]# curl 192.168.111.250
nginx

backup

//首先安裝keepalived
[root@backup ~]# dnf -y install keepalived
 
//編輯配置文件,並啟動服務
[root@backup ~]# mv /etc/keepalived/keepalived.conf{,.back}
[root@backup ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id lb02
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 50
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.111.250
    }
}
 
virtual_server 192.168.111.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.111.141 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 192.168.111.142 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@backup ~]# systemctl enable --now keepalived

編寫腳本

master

[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_nginx.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
    systemctl stop keepalived
fi

[root@master scripts]# vim notify.sh
#!/bin/bash
VIP=$2
case "$1" in
  master)
        nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
        if [ $nginx_status -lt 1 ];then
            systemctl start nginx
        fi
  ;;
  backup)
        nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
        if [ $nginx_status -gt 0 ];then
            systemctl stop nginx
        fi
  ;;
  *)
        echo "Usage:$0 master|backup VIP"
  ;;
esac
[root@master scripts]# chmod +x check_haproxy.sh notify.sh 

backup

[root@backup ~]# mkdir /scripts
[root@backup ~]# cd /scripts/
[root@backup scripts]# scp [email protected]:/scripts/notify.sh .

配置keepalived加入監控腳本的配置

master

[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id lb01
}
 
vrrp_script nginx_check {				//添加
    script "/scripts/check_nginx.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.111.250
    }
    track_script {			//添加
        haproxy_check
    }
    notify_master "/scripts/notify.sh master 192.168.111.250"
}
 
virtual_server 192.168.111.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.111.141 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 192.168.111.142 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@master scripts]# systemctl restart keepalived

backup

[root@backup ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id lb02
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 50
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.111.250
    }
    notify_master "/scripts/notify.sh master 192.168.111.250"		//添加
    notify_backup "/scripts/notify.sh backup 192.168.111.250"
}
 
virtual_server 192.168.111.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.111.141 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 192.168.111.142 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@backup ~]# systemctl restart keepalived

測試

模擬nginx服務故障

//master端
[root@master ~]# curl 192.168.111.250
apache
[root@master ~]# curl 192.168.111.250
nginx
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 1601sec preferred_lft 1601sec
    inet 192.168.111.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@master ~]# systemctl stop nginx
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 1591sec preferred_lft 1591sec
    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

//backup端
[root@backup ~]# systemctl start nginx    //前面把服務關了這裡啟動一下
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:07:42:65 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.142/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 947sec preferred_lft 947sec
    inet 192.168.111.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe07:4265/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@backup ~]# curl 192.168.111.250
apache
[root@backup ~]# curl 192.168.111.250
nginx
[root@backup ~]# curl 192.168.111.250
apache
[root@backup ~]# curl 192.168.111.250
nginx

啟動master端的nginx服務

//master端
[root@master scripts]# systemctl start nginx
[root@master scripts]# systemctl restart keepalived
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 1223sec preferred_lft 1223sec
    inet 192.168.111.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

//backup端
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:07:42:65 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.142/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33
       valid_lft 1632sec preferred_lft 1632sec
    inet6 fe80::20c:29ff:fe07:4265/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在上一篇文章`《驅動開發:內核枚舉DpcTimer定時器》`中我們通過枚舉特征碼的方式找到了`DPC`定時器基址並輸出了內核中存在的定時器列表,本章將學習如何通過特征碼定位的方式尋找`Windows 10`系統下麵的`PspCidTable`內核句柄表地址。 ...
  • 1.字元串capitalize函數 (capitalize vt. 資本化,用大寫字母書寫(或印刷); 把…首字母大寫;) 將字元串的首字母大寫,其它字母小寫; 用法:newstr = string.capitalize() 修改後生成一個新字元串(因為字元串是不可更改數據類型); ''.capit ...
  • 大家好,我是陶朱公Boy,又和大家見面了。 前言 在文章開始前,想先問大家一個問題,大家平時在項目需求評審完後,是直接開始編碼了呢?還是會先寫詳細設計文檔,後再開始進行編碼開發? ☆現實 這個時候可能有部分小伙伴會出來反駁:還詳細設計呢連給開發的時間都不夠,項目經常被倒排期。 作者其實能感同身受上述 ...
  • 學會了技術就要使用,否則很容易忘記,因為自然界壓根就不存在什麼代碼、變數之類的玩意,這都是一些和生活常識格格不入的東西。只能多用多練,形成肌肉記憶才行。 在一次實際的產品開發中,由於業務需求的緣故,需要使用Elasticsearch搜索引擎。搜索引擎是通過索引和文檔檢索數據的,索引類似於MySQL的 ...
  • 首先依賴註入 懶得下載autofac了 直接用程式集進行批量註入 private static WebApplicationBuilder builder; internal static void Load(WebApplicationBuilder web) { builder = web; b ...
  • 前面簡單介紹了騰訊雲的由來和定義,本編介紹騰訊的簡單使用(我這個展示的是把最近寫的vs項目部署到雲服務中): 1、首先根據前面的定義註冊個騰訊雲賬戶,然後購買個雲伺服器,當然首次註冊的都有免費體驗資格,我這裡用到的是免費體驗輕量式伺服器: 進入購買然後開通就會顯示在你的輕量伺服器上,會根據你選的wi ...
  • 有這個想法的初衷 喜歡電子和DIY硬體的朋友對稚暉君應該都不陌生,他定期都會分享一些自己做的好玩的硬體,他之前做了一個ElectronBot桌面機器人我就很感興趣,所以就自己也做了一個。 起初我只是自己開發了一個叫電子腦殼的上位機軟體,大家在之前的博客里應該也有見到,是個用WinUI(WASDK)開 ...
  • 一、 先決條件 1.Azure Repos Git/Git和項目上傳 把本地的Net Core項目上傳至Azure Repos Git/Git 2.Docker Registry Service Connection/Azure subscription和Azure Container Regist ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...