【SpringCloud 2.0】 Eureka集群 高可用的认证服务实现与搭建

SpringCloud2.0 Eureka集群 高可用的认证服务实现与搭建

1. 什么是Eureka Server高可用集群

SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。
Eureka Client会定时连接Eureka Server,获取服务注册表中的信息并缓存到本地,微服务在消费远程API的时候不用每次去Server端查询,而是使用本地缓存的数据,这样的话,一般来讲即使Server宕机,也不会影响微服务之间的调用。但是肯定会影响Client端服务的更新,所以生产环境中,Eureka Server可以通过运行多个实例并相互注册的方式来实现高可用。 Eureka Server实例会彼此增量的同步信息,确保所有节点数据一致。

2. 集群的搭建

这里我们在测试环境下搭建一个有三个节点的集群,生成环境我们我们用kubectl部署的,配置文件是shell脚本自动生成的,但是原理一样。

2.1 相关代码

2.1.1 pom.xml

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

引入eureka-server 和 安全组件

2.1.2 EurekaApplication.java

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

/**
 * @Auther: shiguanghui
 * @Date: 2019/5/27 20:14
 * @ClassName: EurekaApplication
 * @Description: TODO
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

2.1.3 WebSecurityConfig.java

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;

/**
 * @Auther: shiguanghui
 * @Date: 2019/5/29 14:12
 * @ClassName: WebSecurityConfig
 * @Description: TODO
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

Spring Security的CSRF保护默认是开启的,这里把他禁止掉,否则会报服务器无法连接的错误。

2.1.4 application.yaml

spring:
  profiles:
    active: prd
  application:
    name: ${EUREKA_APPLICATION_NAME:eureka-server}
  security:
    user:
      name: admin
      password: admin
---
spring:
  profiles: prd
server:
  port: 8761
eureka:
  instance:
    hostname: ${EUREKA_HOST_NAME:localhost} #服务主机名
    appname: ${spring.application.name} #服务名称,默认为 unknow 这里直接取 spring.application.name 了
  client:
    register-with-eureka: ${BOOL_REGISTER:false} # 是否把服务中心本身当做eureka client 注册。默认为true
    fetch-registry: ${BOOL_FETCH:false} # 是否拉取 eureka server 的注册信息。 默认为true
    service-url:
      defaultZone: ${EUREKA_URL_LIST:http://admin:admin@localhost:8761/eureka/} # 指定服务中心 eureka server的地址
  server:
    enable-self-preservation: ${SELF_PRESERVATION:true} # 是否开启自我保护。 默认为 true.
---
spring:
  profiles: test1
server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server1 #服务主机名
    appname: ${spring.application.name} #服务名称,默认为 unknow 这里直接取 spring.application.name 了
  client:
    register-with-eureka: ${BOOL_REGISTER:true} # 是否把服务中心本身当做eureka client 注册。默认为true
    fetch-registry: ${BOOL_FETCH:true} # 是否拉取 eureka server 的注册信息。 默认为true
    service-url:
      defaultZone: ${EUREKA_URL_LIST:http://admin:admin@eureka-server2:8762/eureka/,http://admin:admin@eureka-server3:8763/eureka/} # 指定服务中心 eureka server的地址
  server:
    enable-self-preservation: ${SELF_PRESERVATION:true} # 是否开启自我保护。 默认为 true.
---
spring:
  profiles: test2
server:
  port: 8762
eureka:
  instance:
    hostname: eureka-server2 #服务主机名
    appname: ${spring.application.name} #服务名称,默认为 unknow 这里直接取 spring.application.name 了
  client:
    register-with-eureka: ${BOOL_REGISTER:true} # 是否把服务中心本身当做eureka client 注册。默认为true
    fetch-registry: ${BOOL_FETCH:true} # 是否拉取 eureka server 的注册信息。 默认为true
    service-url:
      defaultZone: ${EUREKA_URL_LIST:http://admin:admin@eureka-server1:8761/eureka/,http://admin:admin@eureka-server3:8763/eureka/} # 指定服务中心 eureka server的地址
  server:
    enable-self-preservation: ${SELF_PRESERVATION:true} # 是否开启自我保护。 默认为 true.
---
spring:
  profiles: test3
server:
  port: 8763
eureka:
  instance:
    hostname: eureka-server3 #服务主机名
    appname: ${spring.application.name} #服务名称,默认为 unknow 这里直接取 spring.application.name 了
  client:
    register-with-eureka: ${BOOL_REGISTER:true} # 是否把服务中心本身当做eureka client 注册。默认为true
    fetch-registry: ${BOOL_FETCH:true} # 是否拉取 eureka server 的注册信息。 默认为true
    service-url:
      defaultZone: ${EUREKA_URL_LIST:http://admin:admin@eureka-server1:8761/eureka/,http://admin:admin@eureka-server2:8762/eureka/} # 指定服务中心 eureka server的地址
  server:
    enable-self-preservation: ${SELF_PRESERVATION:true} # 是否开启自我保护。 默认为 true.

说明:

a) spring.application.name: 保证一个集群中的所有节点都有同样的spring.application.name;
b) eureka.client.register-with-eureka: 是否把服务中心本身当做eureka client 注册。默认为true;(单机状态下需要设置为false: 单机状态下注册没意义)
c) eureka.client.fetch-registry: 是否拉取eureka server的注册信息。 默认为true;(单机状态下需要设置为false: 单机只有自己无需注册和拉取)
d) eureka.client.service-url.defaultZone: 设置eureka server的通信地址, 查询服务和注册服务都会用到这个地址;

3. 集群的的部署

可以参照:https://blog.csdn.net/shgh_2004/article/details/90672517

这里的实例是一个部署eureka集群到kubernetes集群中。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值