• spring cloud 学习(2)


    上节继续,注册中心单点肯定是不牢靠的,可以参考下面的方案做成注册中心集群:

    弄成3个节点,每个节点向其它节点注册,这样只要集群中有一个节点正常工作即可。为了方便在本机弄出这种效果,我们先修改下host文件

    127.0.0.1  localhost server1 server2 server3

    相当于给本机ip绑了3个hostname。然后在项目中,创建3个profile,参考下图:

    application.yml:

    spring:
      application:
        name: eureka-server-cluster
      profiles:
        active: server1
    

    application-server1.yml:

    server:
      port: 8100
    
    eureka:
      instance:
        hostname: server1
        instance-id: ${spring.application.name}:${server.port}
      client:
        service-url:
          defaultZone: http://server2:8200/eureka,http://server3:8300/eureka
    

    application-server2.yml:

    server:
      port: 8200
    
    eureka:
      instance:
        hostname: server2
        instance-id: ${spring.application.name}:${server.port}
      client:
        service-url:
          defaultZone: http://server1:8100/eureka,http://server3:8300/eureka
    

    application-server3.yml:

    server:
      port: 8300
    
    eureka:
      instance:
        hostname: server3
        instance-id: ${spring.application.name}:${server.port}
      client:
        service-url:
          defaultZone: http://server1:8100/eureka,http://server2:8200/eureka  

    其它不用改,idea中启用时,指定3个不同的profile,参考下图:

    启用3个实例,分别应对server1,server2,server3 这三个profile后,看下UI

    可以看到,每个eureka server都向其它节点注册了。

    其它服务向eureka server集群注册时,参考下面的配置:

    server:
      port: 8001
    
    spring:
      application:
        name: "service-provider-demo"
    eureka:
      instance:
        prefer-ip-address: true
      client:
        service-url:
          defaultZone: http://localhost:8100/eureka/,http://localhost:8200/eureka/,http://localhost:8300/eureka/
    

    再来看另一个问题:

    注册中心的管理界面以及服务注册时,没有任何认证机制,安全性比较差,如果其它服务恶意注册一个同名服务,但是实现不同,可能就有风险了,可以参考下面的配置改进:

    先添加:compile 'org.springframework.boot:spring-boot-starter-security' 依赖,然后在eureka server的application.yml中增加

    security:
      basic:
        enabled: true
      user:
        name: yjmyzz #用户名
        password: 123456 #密码
    

    这样就添加了1个用户名及密码(注:其原理就是spring-security,熟悉spring-security的朋友,也可以改成把用户名/密码存储在数据库中)

    启动后,再浏览eureka server就用输入用户名,密码了。

    然后其它服务注册时,相应的defautZone也要改成类似:

    defaultZone: http://yjmyzz:123456@server1:8100/eureka,http://yjmyzz:123456@server2:8200/eureka 这种格式。  

  • 相关阅读:
    C#判断是否运行在调试模式下
    [php] Interface abstract里面的私有方法 private method of interface and abstract class
    [html] Javascript warning and error of w3c
    [html] <a> and <input> can not click in IE6 when use png fixed IE6下png图片和png背景透明导致该区域的链接和按钮无效
    [Ubuntu] invalid environment block
    [Ubuntu] 分割与合并文件 Cut and mix the file
    [php] Treat an object like an array
    [eZ publish] How to add a element to an array.
    [html] PHP使用Google map web services来计算两点间的距离 Compute the distance between two place via Google map services in PHP
    [html] symbol of <b> and <strong>
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/spring-cloud-eureka-ha-and-security.html
Copyright © 2020-2023  润新知