解决eureka集群报错Root name 'timestamp' does not match expected ('instance')
最近搭建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集群就可以正常运行了。