2、撸一撸Spring-Cloud---创建Eureka注册中心

306 阅读1分钟

创建Eureka注册中心

本文需要对IDEA有一定的了解,清楚IDEA中项目与文件的创建,后续迭代补充基础知识

前期准备

  • 通过IDEA创建module dsz-eureka
  • 依赖项目dsz-root

Spring Cloud版本选型

  • Greenwich SR2
  • Spring Boot 2.1.6.RELEASE
  • Spring 5.1.8.RELEASE
  • jdk 1.8.0_172

最终展示pom.xml和项目结构

项目结构

项目结构

pom.xml

<?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">
    <parent>
        <artifactId>dsz-root</artifactId>
        <groupId>com.dsz.platform</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dsz-eureka</artifactId>

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

        <!-- 添加Spring Security认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

</project>

application.yml

server:
  port: 8761
spring:
  application:
    name: dsz-eureka
  security:
    user:
      name: admin
      password: admin
eureka:
  instance:
    hostname: 127.0.0.1
    ## 心跳间隔-5秒
    lease-renewal-interval-in-seconds: 5
    ## 没有心跳的淘汰时间-10秒
    lease-expiration-duration-in-seconds: 10
  client:
    ## 刷新本地缓存-5秒
    registry-fetch-interval-seconds: 5
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    ## 开发环境建议关闭自我保护
    enable-self-preservation: false
    ## 主动失效时间
    eviction-interval-timer-in-ms: 5000
    ## 不开启缓存
    use-read-only-response-cache: false
    ## 指定每分钟需要收到的续约次数的阈值
    renewal-percent-threshold: 0.85

com.dsz.base.eureka.EurekaApplication

package com.dsz.base.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {

        SpringApplication.run(EurekaApplication.class, args);

    }
}

com.dsz.base.eureka.security.WebSecurityConfig

package com.dsz.base.eureka.security;

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

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

下期预告《撸一撸Spring Cloud - 创建Config配置中心》