預期部署方案:兩個eureka+三個相關應用
注冊時應用出現:Request execution failed with message: Cannot invoke “Object.getClass()” because “authScheme” is null,一開始認為未正確傳遞eureka配置的賬戶+密碼,例:defaultZone: http://username:password@dev.bab.cn:8761/eureka/,檢查后發現一切正常,后續排查后得出結論 hostname不能是127.0.0.1,必須為容器內ip(本次直接配置的是外部轉發域名),調整為hostname: ${POD_IP},當然也可能是受多個因素影響,如集群網絡不通、端口或eureka啟動端口配置不爭氣等:
spring:security:basic:enabled: trueuser:name: eurekapassword: abc123456eureka:datacenter: user-serverenvironment: prodinstance:hostname: ${POD_IP} # 容器部署必須填寫docker容器的ipinstance-id: ${random.uuid}client:# For stand-alone deployment, the following is false 單機部署的話以下為falseregister-with-eureka: ${IS_CLUSTERED:false}fetch-registry: ${IS_CLUSTERED:false}service-url:defaultZone: http://eureka:abc123456@bab.com/eureka/
網上其他說法總結:
- 降級jdk到8(本次是openjdk17)
- 升級spring core(本次spring boot3.0,core已經達到6.0)
- 添加禁用csrf:
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.csrf().disable().httpBasic();return http.build(); }
- 禁用csrf方案2(來自stack overflow):
@Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(authConfig -> authConfig.anyRequest().authenticated()).httpBasic(httpBasic -> {}).logout(logout -> {logout.logoutUrl("/logout");logout.addLogoutHandler((request, response, authentication) -> authentication.setAuthenticated(false));});return http.build(); }
- 禁用csrf方案3:
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.csrf().disable().authorizeHttpRequests().anyRequest().authenticated() // 任何請求都不需要認證:.anyRequest().permitAll().and().httpBasic();return http.build(); }
以上辦法都沒有成功,而且還導致額外出現Root name ‘timestamp’ does not match expected錯誤。
遇到的其他問題:eureka存在兩個實例時相關服務注冊完成后自動斷開,排查后因為兩個eureka沒有將register-with-eureka和etch-registry同時設置為true,且defaultZone的地址不是相互注冊狀態eureka1填寫eureka2的ip、eureka2填寫eureka1的ip,可減少為1臺或重新配置defaultZone的ip