Eureka客户端注册失败:Cannot execute request on any known server

悠悠 2023-03-01 11:40 99阅读 0赞

1、问题

spring boot启动异常如下:

Cannot execute request on any known server

2、原因分析

开启Eureka安全Security认证后,security默认加上了 csrf 拦截,

applicaton.yml

  1. server:
  2. port: 8888
  3. spring:
  4. application:
  5. name: eureka-server
  6. security:
  7. user:
  8. name: admin
  9. password: admin
  10. eureka:
  11. instance:
  12. hostname: 127.0.0.1
  13. preferIpAddress: true
  14. client:
  15. registerWithEureka: false
  16. fetchRegistry: false
  17. defaultZone: http://127.0.0.1:8888/eureka/
  18. server:
  19. waitTimeInMsWhenSyncEmpty: 0
  20. peer-eureka-nodes-update-interval-ms: 10000
  21. enableSelfPreservation: false

pom.xml

  1. <properties>
  2. <eureka-server.version>2.2.2.RELEASE</eureka-server.version>
  3. </properties>
  4. <dependency>
  5. <groupId>org.springframework.cloud</groupId>
  6. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  7. <version>${eureka-server.version}</version>
  8. </dependency>

3、解决方案

  1. 在注册中心,重写WebSecurityConfigurerAdapter中得方法configure(),忽略对注册中心url相对路径/eureka的拦截。
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. @EnableWebSecurity
  6. class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  7. @Override
  8. protected void configure(HttpSecurity http) throws Exception {
  9. http.csrf().ignoringAntMatchers("/eureka/**");
  10. super.configure(http);
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读