问题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);
}
}

