In the distributed environment, Eureka exists as a registry, which is responsible for the registration and discovery of various services, and is a very core component. Therefore, if the Eureka environment fails, our entire system will be unstable. Therefore, we must ensure that our Eureka is highly available.
Had a high availability
Create a project
Create a SpringBoot project. The single-node Eureka server was covered in the previous article, so this article highlights where the cluster environment needs to be configured.
1. Configuration file
When setting up Eureka cluster, you need to add multiple configuration files and use SpringBoot multi-environment configuration mode. Add as many configuration files as you need in the cluster.
Eureka1 configuration file
Name =eureka-server server.port=8761 Hostname =eureka1 # set service registry address, Point to another registry eureka. Client. ServiceUrl. DefaultZone = http://eureka2:8761/eureka/Copy the code
Eureka2 configuration file
Name =eureka-server server.port=8761 Hostname =eureka2 # set service registry address, Point to another registry eureka. Client. ServiceUrl. DefaultZone = http://eureka1:8761/eureka/Copy the code
2. Add the Logback log configuration file
<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <! <property name="LOG_HOME" value="${catalina.base}/logs/" /> <! - the console output - > < appender name = "Stdout" class = "ch. Qos. Logback. Core. ConsoleAppender" > <! - log output coding - > < layout class = "ch. Qos. Logback. Classic. PatternLayout" > <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: Log messages, SSS} [%thread] %-5level % Logger {50} - % MSG %n </pattern> </layout> </appender> <! Generated according to the daily log file - > < appender name = "RollingFile" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > < rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! ${LOG_HOME}/server.%d{YYYY-MM-DD}. Log </FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n </pattern> </layout> <! -- the largest size in the log file -- > < triggeringPolicy class = "ch. Qos. Logback. Core. Rolling. SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <! <root level="DEBUG"> <appender-ref ref="Stdout" /> <appender-ref ref="RollingFile" /> </root> <! -- Log asynchrony to database --> <! - < appender name = "DB" class = "ch. Qos. Logback. Classic. DB. DBAppender" > asynchronous to the database log < connectionSource Class = "ch. Qos. Logback. Core. The DriverManagerConnectionSource" < > connection pool dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource"> <driverClass>com.mysql.jdbc.Driver</driverClass> < url > JDBC: mysql: / / 127.0.0.1:3306 / databaseName < / url > < user > root < / user > < password > root < / password > < / a dataSource > </connectionSource> </appender> --> </configuration>Copy the code
3. Package the project
Package the project into a JAR package
Second, cluster environment construction
Deployment environment: Install JDK1.8 and configure environment variables correctly. Note: You need to turn off the Linux firewall or open port 8761
1. Upload the instance
Create a eureka directory in /usr/local/to copy the jar package of your project to /usr/local/eureka
2. Create a startup script file
Project name: replace with jar package name Configuration file variable name: replace with corresponding Eureka1 and Eureka2
#! /bin/bash CD 'dirname $0' CUR_SHELL_DIR= 'PWD' CUR_SHELL_NAME= 'basename ${BASH_SOURCE}' JAR_NAME=" project name" JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME #JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m" JAVA_MEM_OPTS="" SPRING_PROFILES_ACTIV=" -dspring.profiles. Active = profile variable name "#SPRING_PROFILES_ACTIV="" LOG_DIR=$CUR_SHELL_DIR/logs LOG_PATH=$LOG_DIR/${JAR_NAME%.. log echo_help() { echo -e "syntax: sh $CUR_SHELL_NAME start|stop" } if [ -z $1 ]; then echo_help exit 1 fi if [ ! -d "$LOG_DIR" ]; then mkdir "$LOG_DIR" fi if [ ! -f "$LOG_PATH" ]; then touch "$LOG_DIR" fi if [ "$1" == "start" ]; then # check server PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'` if [ -n "$PIDS" ]; then echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}." exit 1 fi echo "Starting the $JAR_NAME..." # start nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 & COUNT=0 while [ $COUNT -lt 1 ]; do sleep 1 COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l` if [ $COUNT -gt 0]; then break fi done PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'` echo "${JAR_NAME} Started and the PID is ${PIDS}." echo "You can check the log file in ${LOG_PATH} for details." elif [ "$1" == "stop" ]; then PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'` if [ -z "$PIDS" ]; then echo "ERROR:The $JAR_NAME does not started!" exit 1 fi echo -e "Stopping the $JAR_NAME..." for PID in $PIDS; do kill $PID > /dev/null 2>&1 done COUNT=0 while [ $COUNT -lt 1 ]; do sleep 1 COUNT=1 for PID in $PIDS ; do PID_EXIST=`ps --no-heading -p $PID` if [ -n "$PID_EXIST" ]; then COUNT=0 break fi done done echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}." else echo_help exit 1 fiCopy the code
3. Set the run permission for the startup script
chmod -R 755 server.sh
Copy the code
4. Modify the host file in Linux
Modify the hosts files respectively
vim /etc/hosts
Copy the code
5. Start services separately
Start the service
./server.sh start
Copy the code
Close the service
./server.sh stop
Copy the code
Separate access tests
Deployment successful ~