SpringCould Eureka Server 运行时遇到问题记录

问题1:Type javax.xml.bind.JAXBContext not present 和 Unable to start embedded Tomcat
  • 原因
    在使用 IDEA 构建 SpringCloud 的项目时,我使用了 JDK_11.0.8,但是在 JDK9 及其以后的版本,都没有自动把 JAXBContext 自动加载到依赖包里面。
  • 解决方案
    第一步,把项目修改回 JDK 1.8,点击 File -> Project Structure...,修改 Project 的 JDK 为 1.8
    Project Structure.Project

    第二步,修改 Modules 为 Project SDK 或者直接修改为 JDK 1.8
    Project Structure.Modules
问题2:Cannot execute request on any known server

要注意 application.yml 配置一定要配置 register-with-eureka 和 fetch-registry 都为 false

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
  • register-with-eureka:是否要向 Eureka Server 注册,而当前服务本来就是注册中心,只有微服务提供者才需要向注册中心注册,所以此处不能为 true
  • fetch-registry:是否要向 Eureka Server 拉取注册信息,只有微服务消费者才需要拉取服务注册信息,所以此处也不能为 true
问题3:在集群和需要安全认证的例子中出现问题:Cannot execute request on any known server

在安全认证的机制中,默认情况下,它将要求在每次向应用程序发送请求时都发送一个有效的CSRF(Cross Site Request Forgery:跨站请求伪造)Token。

但是呢,由于 Eureka Client 通常不会提供这样的 Token,所以需要我们人为地把注册的地址给排除在 CSRF 范围之外。具体做法是在 Eureka 注册中心的模块中,添加以下的 Class:

package org.example.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容