Eureka实现高可用及为Eureka设置登录账号和密码

本文通过两个eureka相互注册实现注册中心的高可用,同时为注册中心配置认证登录。

  • 需要用到的maven配置
        <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>
  • 第一个服务配置:myEureka
server:
  port: 9998 #服务注册中心端口号
eureka:
  instance:
    hostname: localhost #服务注册中心IP地址
  client:
    registerWithEureka: true #是否向服务注册中心注册自己
    fetchRegistry: true #是否检索服务
    serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone:  http://wangzhzh:wzz123@localhost:9999/eureka/

spring:
  application:
    name: eureka-server
  # 安全认证的配置
  security:
    user:
      name: wangzhzh  # 用户名
      password: wzz123   # 用户密码
  • 第二个服务配置:myEureka2
server:
  port: 9999 #服务注册中心端口号
eureka:
  instance:
    hostname: localhost #服务注册中心IP地址
  client:
    registerWithEureka: true #是否向服务注册中心注册自己
    fetchRegistry: true #是否检索服务
    serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://wangzhzh:wzz123@localhost:9998/eureka/

spring:
  application:
    name: eureka-server2
  # 安全认证的配置
  security:
    user:
      name: wangzhzh  # 用户名
      password: wzz123   # 用户密码
  • 开启eureka服务

注意:我所使用的事spring cloud 2.0以上的版本,还需要在两个服务加配置文件。

Spring Cloud 2.0 以上的security默认启用了csrf检验,要在eureka server端配置security的csrf检验为false。

/**
 * @Description
 * @Author wzz
 * @Date 2019/10/10 17:22
 */
@EnableWebSecurity
public class SecurityConf extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        super.configure(http);

        http.csrf().disable();

    }
}

 

posted @ 2019-10-11 10:58  龙牙xz  阅读(3193)  评论(0编辑  收藏  举报