最近搭建eureka集群注冊中心時,單機版的沒有問題,但是在搭建集群版時,項目啟動后報錯:
Root name 'timestamp' does not match expected ('instance')
因為我給eureka添加了登錄認證,所以每個節點在相互注冊時需要加上用戶名和密碼,配置如下:
server.port=7902 spring.application.name=eureka #安全中心配置登錄用戶名密碼 spring.security.user.name=root spring.security.user.password=root #注冊中心配置 #禁止自己當做服務注冊 禁止自己注冊自己 集群模式時需要允許互相注冊 #eureka.client.register-with-eureka = false #屏蔽注冊信息 集群模式時不能屏蔽 #eureka.client.fetch-registry = false eureka.instance.hostname= eureka-7902 eureka.client.service-url.defaultZone = http://root:root@eureka-7901:7901/eureka/,http://root:root@eureka-7903:7903/eureka/
配置中的 http://root:root@eureka-7901:7901/eureka/
這種路徑,是eureka所支持的認證地址url,添加了安全認證后,集群的注冊地址就需要按照這格式寫:http://username:password@hostname:port/eureka/
但是添加了認證后,集群在相互注冊時,就會出現這個錯誤:Root name 'timestamp' does not match expected ('instance')
查了好久才知道,集群方式需要關閉spring的csrf認證才行,單獨寫一個配置類,將csrf認證設置為關閉即可,代碼如下:
/** * csrf配置類 * * @author Zhangzhiyong * @date 2021/7/23 16:29 */ @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.csrf().disable();//關閉csrf認證,否則會報錯Root name 'timestamp' does not match expected ('instance') super.configure(http); } }
然后eureka集群就可以正常運行了。