Spring Cloud踩坑记录

Spring Wu 213 2021-02-03

报错 Whitelabel Error Page

原因是把Application.java放在了如下所示:

解决: 把Application.java放到最外层即可解决

启动报错ClassNotFoundException: feign.Feign$Builder

原因: 包的问题。
解决方案, 修改pom文件

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

搭建Spring Cloud Bus Demo时,启动报错。

Caused by: java.lang.NoClassDefFoundError: org/springframework/security/config/http/SessionCreationPolicy
	at org.springframework.boot.actuate.autoconfigure.ManagementServerProperties$Security.<init>(ManagementServerProperties.java:186) ~[spring-boot-actuator-1.4.0.RELEASE.jar:1.4.0.RELEASE]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_162]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_162]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_162]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_162]
	at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_162]
	at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
	at org.springframework.beans.AbstractNestablePropertyAccessor.newValue(AbstractNestablePropertyAccessor.java:914) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
	... 125 common frames omitted

报错信息如图所示:

出错原因:
因为没有找到该类。所以就出现这个问题,因为我们在配置消息总线时加入了该配置:management.security.enable=false,该配置需要对Spring的security包产生依赖。但在项目pom中并没有去依赖Spring的security包。

解决方案:
在pom文件中增加对security包的依赖:

<!-- 对Spring security安全包的依赖,否则会报错 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

报错:

DiscoveryClient_SPRINGCLOUD-QUERYDSL/10.0.2.140:springcloud-querydsl:4445 - was unable to refresh its cache! status = Cannot execute request on any known server

原因:
没有启动注册中心,导致服务无法进行注册,所以会报Cannot execute request on any known server
解决方案:
启动注册中心

报错:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: teacher, for columns: [org.hibernate.mapping.Column(students)]

原因:
因为Teacher与Student是一对多关系,在Teacher里面有List这么一个引用,所以需要指定它们之间的关系、比如@OneToMany、@ManyToOne等。
解决方案:
指定相应的关系即可。