系统相关
首页 > 系统相关> > 微服务架构实战:Nginx的安装及负载均衡配置、开机启动

微服务架构实战:Nginx的安装及负载均衡配置、开机启动

作者:互联网

编辑下列各项内容:

#日志目录

base path=/tmp

#跟踪服务器

tracker server=10.10.10.22:22122tracker server=10.10.10.32:22122#URL中是否有group名称

url have group name = true#storage path的个数

store_path_count=2

store_path0=/data/fdfs_storage/storage0store_pathl=/data/fdfs_storage/storage1

使用如下命令编辑Nginx配置文件:

vi /data/nginx/ conf/nginx.conf

文件的内容如下所示:

user nginx nginx;

worker processes 2;

#pid/usr/local/nginx/logs/nginx.pid;worker rlimit_nofile 1024;

events {

use epoll;

worker connections 1024;

}

http {

include mime.types;

server names hash bucket size 128;client header buffer_size 32k;

largeclient header buffers 432k;client max body size 20m;

limit rate 1024k;

default type application/octet-stream;

log format main ‘$remote addr - r e m o t e u s e r [ remote user [ remoteuser[time local] “Sreauest” 1’$status b o d y b y t e s s e n t " body bytes sent " bodybytessent"http referer",

I"Shttp user agent" “Shttp x forwarded for”’;

access log/data/nginx/logs/access.log main;

server {

listen 80;

server name localhost;

location ~/group[0-9]/MO0{

#root /data/fdfs storage;ngx fastdfs module;

}

}

}

保存文件后,使用如下命令创建两个软链接:

ln -s /data/fdfs storage/storage0 /data/fdfs storage/storage0/M00

ln -s /data/fdfs storage/storagel /data/fdfs storage/storage1/M00

使用如下命令启动Nginx:

/data/nginx/sbin/nginx

微服务架构实战:Nginx的安装及负载均衡配置、开机启动

开机启动

====

为了方便运维管理,前面安装的各个服务都可以设置为开机启动。

开机启动Tracker

在两台Tracker Server机器上,创建服务启动文件:

vi/etc/rc.d/init.d/fdfs trackerd

输入如下内容:

#!/bin/bash

#fdfs trackerd Starts fdfs trackerd#

#chkconfig: 234599 01

#description: FastDFS tracker server### BEGIN INIT INFO

Provides: $fdfs trackerd###END INIT INFO

#Source function library.

. /etc/init.d/functions

PRG=/usr/bin/fdfs trackerdCONF=/etc/fdfs/tracker.conf

if[!-f $PRG ];then

echo "file $PRG does not exist!"exit 2

fi

if[!-f $CONF ]; then

echo "file $CONF does not exist! "

exit 2

fi

CMD="$PRG $CONF"RETVAL=0

start( {

echo -n " S t a r t i n g F a s t D F S t r a c k e r s e r v e r : " "Starting FastDFS tracker server: " "StartingFastDFStrackerserver:"CMD &

RETVAL=$?echo

return $RETVAL

}

stop () {

C M D s t o p R E T V A L = CMD stopRETVAL= CMDstopRETVAL=?

return $RETVAL

}

rhstatus() {

status fdfs trackerd

}

restart( {

$CMD restart &

case “$1” in

start)

start;;

stop)

stop;;

status)

rhstatus

;;

restart|reload)

restart

;;

condrestart)

restart;;

echo $"Usage: $0 {start IstopIstatus|restart l.condrestart}"exit 1

esac

exit $?

按顺序执行下列命令,把 Tracker设置为开机启动:

chmod 755 /etc/rc.d/init.d/fdfs trackerd

chkconfig --add fdfs_trackerd

chkconfig fdfs_trackerd on

开机启动 Storage

在两台Storage机器上,创建服务启动文件:

vi /etc/init.d/fdfsstoraged

输入如下内容:

#!/bin/bash#

#fdfs storaged Starts fdfs storaged#

#chkconfig:2345 99 01

description: FastDFS storage server### BEGIN INIT INFO

Provides: $fdfs storaged### END INIT INFO

#Source function library…/etc/init.d/ functions

PRG=/usr/bin/fdfs_storaged

CONF=/etc/fdfs/storage.conf

if [!-f $PRG ];then

echo "file $PRG does not exist! "exit 2

fi

if [!-f $CONF J;then

echo "file $CONF does not exist! "exit 2

fi

CMD="SPRG $CONF"RETVAL=0

start({

echo -n "Starting FastDES storage server: "$CMD &

RETVAL=$?echo

return $RETVAL

stop(){

C M D s t o p R E T V A L − CMD stopRETVAL- CMDstopRETVAL−?

return $RETVAL

rhstatus() {

status fdfs_storaged

}

restart({

$CMD restart &

}

case “$1” instart)

start;

;

stop)

stop

status)

rhstatus

;;

restartlreload)

restart

;;

condrestart)

restart;;

*)

echo "Usage: $0{startlstoplstatus| restartl condrestart}

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

"exit 1

esac

exit $?

按顺序执行下列命令,把 Storage设置为开机启动:

chmod 755 /etclrc.d/init.d/fdfs storaged

chkconfig --add fdfs storaged

chkconfig fdfs storaged on

开机启动Nginx

在四台机器中各创建一个Nginx启动文件:

vi /etc/init.d/nginx

输入如下内容:#!/bin/bash

chkconfig: -8515PATH=/data/nginx

DESC="nginx daemon"NAME=nginx

DAEMON= P A T H / s b i n / PATH/sbin/ PATH/sbin/NAME

CONFIGFILE= P A T H / c o n f / PATH/conf/ PATH/conf/NAME.confPIDFILE= P A T H / l o g s / PATH/logs/ PATH/logs/NAME.pid

SCRIPTNAME=/etc/init.d/$NAMEset -e

[-x “$DAEMON” 1llexit 0do start( {

$DAEMON -c $CONFIGFILE I echo -n “nginx already running”}

do_stop ({

$DAEMON -s stop ll echo -n “nginx not running”}

do reload() {

$DAEMON-s reload ll echo -n “nginx can’t reload”)

case “$1” instart)

echo -n "Starting $DESC: $NAME"do_start

echo “.”;;

stop)

echo -n “Stopping D E S C : DESC: DESC:NAME”"do_stop

echo “.”;;

reloadl graceful)

echo -n "Reloading $DESC configuration. . ."do reload

echo “.”;

restart)

echo -n "Restarting D E S C : DESC: DESC:NAME"do stop

do_startecho “.”;;

*)

echo “Usage: $SCRIPTNAME {startlstopl reloadlrestart}”>&2exit 3

;;

esacexit 0

按顺序执行下列命令,把 Nginx设置为开机启动:chmod 755 /etc/rc.d/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

小结

==

本章使用开源的FastDFS搭建了一个高可用的分布式文件系统,并通过Nginx为文件的访问设置了负载均衡服务,从而为微服务应用提供一个高性能的文件服务器。

在完成安装并测试正常之后,即可在库存管理项目中配置分布式文件系统的链接地址,使用微服务与分布式文件系统进行联调。

本文给大家讲解的内容是微服务架构实战:Nginx的安装及负载均衡配置、开机启动

  1. 下篇文章给大家讲解的是springcloud微服务架构实战:使用Jenkins实现自动化构建;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!

标签:负载,nginx,storage,stop,echo,Nginx,开机,fdfs,restart
来源: https://blog.csdn.net/m0_65484838/article/details/122134119