【Spring cloud 系列】:构建Eureka Server (02)

一、概述

  上篇已经构建出了Spring cloud 的基本骨架,在本文中将介绍如何快速构建 Eureka Server端口 服务注册于发现,后续的文章都会使用 microserver-parent项目作为parent信息,不了解的朋友建议从上一章开始看起  ps:作者能力有限,表达能力不足,这里将不会讲述概念性的讲解,想了解概念的朋友可自行百度查阅或官网查阅自行理解,同时尽可能的照顾到新人朋友,教大家如何从官网中学习一门技术。

  本文所有Demo都将上传至GitHub中方便各位朋友查看学习,如您对本人的编码风格或其他建议可以联系博主。

二、目录结构

  

 

三、构建Eureka Server

  1)使用IDEA 快速构建

  

  2)Spring cloud 官网看下是如何将 Eureka 添加到项目中去。

    

 从官方这段话的意思来看,需要我们为Spring boot 项目 引入jar 包

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

四、配置 Eureka Server 与启动 Server 服务

  1)将项目中生成的 application.properties 修改为 yml  格式

  2)从官网的说明,我们看到有个 @EnableEurekaServer 注解,也就是说我们需要对我们项目的启动类添加该注解作为Server端

  

  3)如何配置 Eureka Server信息,从官网中我们可以看到,Eureka 服务 是分为高可用和单机的两种模式,从官方给的配置信息来看,有两处不同的地方

  左图单机版本,右图高可用版

             

  从上图中我们看到 eureka.client 发现 registerWithEureka 与 fetchRegistry 从字面上不难理解,registerWithEureka 是否将自身注册到Eureka Server 中,fetchRegistry 是否从Server 获取注册表,本文将采用单机版的作为讲解,同时可以看看官方配置 Eureka的其他配置,由于配置信息过多也不做介绍了,我们在这里直接直接使用单机版的配置。启动项目看看效果

  

 

五、添加用户认证

  1)从官网看到如果需要添加安全认证的话就需要对 defaultZone 这里进行改造 为 user:password@ip:port 这种格式

 

  2)引入认证jar包,同时需要在我们的启动类上添加新的注解 @EnableWebSecurity

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>

  3)改造yml 在这里说明下,配置信息这块碰到坑了,折腾了一会才搞清楚什么原因,只能说尝鲜需谨慎

server:
  port: 8761

spring:
  application:
    name: @project.artifactId@
  security:
    user:
      name: user
      password: password123
logging:
  level:
    root: info

eureka:
  instance:
    prefer-ip-address: true  #  将自身ip注册到Eureka中
    instance-id:  ${spring.application.name}(${spring.cloud.client.ip-address}:${server.port})  # 自定义显示方式 项目名称(ip:port)

  client:
    register-with-eureka: false #  是否注册到服务中 ,由于是单机版本,设置为 false , 如果需要配置高可用的 Eureka Server 需要设置为 true
    fetch-registry: false  #  是否从 Server 中获取服务列表 ,设置为 false ,如果需要配置高可用的 Eureka Server 需要设置为 true
    service-url:
      defaultZone:  http://user:password123@localhost:8761/eureka/ # Eureka Server 地址 高可用的 需要相互注册 多个地址用,号分割

  server:
    enable-self-preservation: false  # 关闭Eureka 自我保护功能,生产环境不建议关闭
    eviction-interval-timer-in-ms: 4000 # 设置清理间隔时间 单位 (毫秒) 默认60*1000

 ${spring.cloud.client.ip-address}  这段配置做个说明吧,在老版本中 是这样写的 ${spring.cloud.client.ipAddress} 不知道在什么版本中更新了,最后翻看源码看到这里的配置发生了细微的变动。

 

 

  4)解决 Eureka client 端口向服务端注册时遇到的问题,问题说明:Eureka client 向Server注册时,无用户认证时可以正常获取Server端进行注册,添加用户认证后失败,困扰作者很久,最终无奈只能上 GitHub 上寻求答案,看到贡献者在问题下方有做出这样的回答,大概意思就是 Security 默认是开启了 CSRF保护,同时也告诉我们解决方案就是关闭 CSRF保护,并且给出了关键性代码,在这里感谢下可爱的贡献者们,作者的话痨病又犯了 - 。-   再次强调一下 (尝鲜需谨慎),废话少说点吧,赶紧上关键性代码

  

public class EurekaSecurityConfiguration {

    @Configuration
    @Order(SecurityProperties.BASIC_AUTH_ORDER)
    static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http.csrf().disable();
        }
    }
}

  5)启动client端口看看能不能注册,从下图可以看到已经成功。

 

posted on 2018-04-19 16:46  淡淡丶奶油味  阅读(880)  评论(0编辑  收藏  举报