SpringCloud整理(1)Eureka篇

本文主要记录一些总结,入门教程可以参考其他资料。本文基于Greenwich.SR5版本

一 集群搭建

正常情况下,我们肯定是要搭建集群的。搭建集群时,注意defaultZone配置项的地址,不能使用localhost。否则Eureka管理界面就不会有下面的信息
在这里插入图片描述
server1

server:
  port: 8001 #指定运行端口
spring:
  application:
    name: eureka-server #指定服务名称
eureka:
  instance:
    hostname: server1 #指定主机地址
  client:
#    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
#    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
    serviceUrl:
      defaultZone: http://server2:8002/eureka/
  server:
    enable-self-preservation: false #关闭保护模式

server2

server:
  port: 8002 #指定运行端口
spring:
  application:
    name: eureka-server #指定服务名称
eureka:
  instance:
    hostname: server2 #指定主机地址
  client:
#    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
#    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
    service-url:
      defaultZone: http://server1:8001/eureka/
  server:
    enable-self-preservation: false #关闭保护模式

二 安全认证

有时,我们想要提高服务的安全性。则可以对Eureka服务器添加SpringSecurity。

需要添加Spring security的配置类,以取消csrf检查


@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

节点出现在 unavailable-replicas 下的问题
参考:https://blog.csdn.net/liupeifeng3514/article/details/85273961

三 zone和region

这个我没有实际用过,先记录下。一般情况下用不到,只有在集群很大,并且服务部署在不同的物理机房时可以使用这个配置来优化。
eureka分区的深入讲解,region和zone的使用

四 客户端配置

新建一个服务eureka客户端(也就是服务提供者

pom

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

配置文件

server:
  port: 8102
  servlet:
    context-path: /eurekaClient
spring:
  application:
    name: eureka-client
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://user:123456@server1:8001/eureka/,http://user:123456@server2:8002/eureka/ #同时注册到两个注册中心

五 优雅地关闭

有时服务已经关闭了,但eureka服务器不能马上感应到。默认情况下,最多会有90s延迟.

客户都安使用actuator的shutdown命令。

客户端配置添加

management:
  endpoint:
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: shutdown,health,info

命令
在这里插入图片描述
问题
生产环境下,这些端点肯定是要鉴权的。那如何鉴权通过是个问题。目前想法,可能需要自定义SpringSecurity的部分逻辑

参考

  1. Spring Cloud Eureka:服务注册与发现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值