zoukankan      html  css  js  c++  java
  • Eureka服务端

    pom中添加依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    application.yml

    #服务名称
    spring:
      application:
        name: eureka-server
      security:
        user:
          name: eureka
          password: eureka
    #端口
    server:
      port: 8761
    #eureka服务,服务器名称
    eureka:
      instance:
        hostname: eureka2
      client:
        service-url:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1:8761/eureka

    EurekaServerApplication.java 启动类加注解

    package com.example.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }

    配置springsecurity

    SpringSecurityConfiguration.java

    package com.example.eurekaserver.service;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    
    /**
     * @Author: shaoxin
     * @Date: 2019/6/28 13:05
     * @Email:airvicii@163.com
     */
    @Configuration
    public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            //关闭csrf
            http.csrf().disable();
        }
    }

    centos 中自定义制 jar 启动工具

    server.sh

    #!/bin/bash
     
    cd `dirname $0`
     
    CUR_SHELL_DIR=`pwd`
    CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
     
    JAR_NAME="eureka-server-0.0.1-SNAPSHOT.jar"
    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=application.yml"
    #SPRING_PROFILES_ACTIV=""
    LOG_DIR=$CUR_SHELL_DIR/logs
    LOG_PATH=$LOG_DIR/server.log
     
    echo_help()
    {
        echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
    }
     
    if [ -z $1 ];then
        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
        exit 1
    fi
  • 相关阅读:
    【Codeforces】Codeforces Round #680 Div2
    PS1 长命令回到行首进行覆盖
    vue 跟路径加载缺少跟前缀
    Mac OS Virtualbox 倒入 ova 镜像文件
    笔记本电脑扩展屏幕或设备后不能播放声音
    git clone 后使用子分支
    laravel 环境自编译过程
    virtual Box centos7 公司网络环境下不能联网的解决方案
    CentOS7 php7 安装 curl 扩展
    CentOS 7 安装 Nodejs npm 及版本冲突解决
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/11102144.html
Copyright © 2011-2022 走看看