编写一个高可用的Eureka Server

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.XXX</groupId>
        <artifactId>XXX</artifactId>
        <version>0.0.1</version>
    </parent>
    <groupId>com.XXX</groupId>
    <artifactId>reg-center</artifactId>
    <version>1.4</version>
    <name>reg-center</name>
    <description>注册中心</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>${project.name}</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${docker.plugin.version}</version>
                <configuration>
                    <!-- Dockerfile文件位置 -->
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!-- 镜像仓库:版本 -->
                    <imageName>${docker.image.prefix}/${project.artifactId}:${project.version}
                    </imageName>
                    <!-- 强制覆盖 -->
                    <forceTags>false</forceTags>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <!-- 资源 -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
                    <serverId>docker-aliyun-XXX</serverId>
                    <registryUrl>registry.cn-hangzhou.aliyuncs.com</registryUrl>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

Dockerfile


FROM registry.cn-hangzhou.aliyuncs.com/sherry/java:1.8.0_111
VOLUME /tmp
ADD target/reg-center.jar reg-center
RUN sh -c 'touch /reg-center'
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java -Xms$Xms -Xmx$Xmx $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /reg-center"]

logback-spring.xml

<configuration debug="true">

    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN"
              value="%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

application.yml

server:
  port: 10000
spring:
  profiles:
    active: prod
  application:
    name: @project.artifactId@
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:yd-reg-center:@project.version@

info:
  app:
    name: @project.artifactId@
    encoding: @project.build.sourceEncoding@
    java:
      source: @java.version@
      target: @java.version@

management:
  endpoint:
    shutdown:
      enabled: true

# 单机环境
---

spring:
  profiles: dev
  security:
    user:
      name: yiding
      password: yiding
eureka:
  environment: ${spring.profiles}
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000

# 集群环境
---

spring:
  profiles: test
  security:
    user:
      name: yiding
      password: yiding
eureka:
  environment: ${spring.profiles}
  client:
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@server1:${server.port}/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@server2:${server.port}/eureka/
  server:
    #    是否开启自我保护(开发测试时一般关闭,默认打开)
    enable-self-preservation: true
    #    清理无效节点的时间间隔,默认60*1000,即60秒
    eviction-interval-timer-in-ms: 5000
#  instance:
##    心跳检测时间
#    lease-renewal-interval-in-seconds: 5
##    心跳检测剔除时间
#    lease-expiration-duration-in-seconds: 15

# 集群环境
---

spring:
  profiles: prod
  security:
    user:
      name: XXX
      password: XXX
     
eureka:
  environment: ${spring.profiles}
  client:
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@server1:${server.port}/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@server2:${server.port}/eureka/
  server:
    #    是否开启自我保护(开发测试时一般关闭,默认打开)
    enable-self-preservation: true
    #    清理无效节点的时间间隔,默认60*1000,即60秒
    eviction-interval-timer-in-ms: 5000
#  instance:
##    心跳检测时间
#    lease-renewal-interval-in-seconds: 5
##    心跳检测剔除时间
#    lease-expiration-duration-in-seconds: 15

YdRegCenterApplication

package com.zhidianfan.ydregcenter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class YdRegCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(YdRegCenterApplication.class, args);
    }

}


WebSecurityConfig

package com.zhidianfan.ydregcenter.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }
}

代码目录结构

image-20190311222148683

说明

1、项目使用Docker的maven插件,最终打包成镜像进行部署

2、使用Rancher部署Docker镜像

3、WebSecurityConfig是为了解决SpringBoot2与SpringSecurity整合过程中,SpringSecurity拦截掉了Eureka注册中心之间的心跳检测

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值