常见问题解决方案:SpringBoot+SpringCloud篇

1、SpringCloud客户端Client启动时自动停止

启动日志如下:

2019-02-18 15:55:16.519  INFO 15976 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$2756c514] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-02-18 15:55:19.126  INFO 15976 --- [           main] c.hand.user.ShopServiceUserApplication   : No active profile set, falling back to default profiles: default
2019-02-18 15:55:19.529  INFO 15976 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d9a7a1a0-c71d-3128-b239-ea6e82c6b865
2019-02-18 15:55:19.575  INFO 15976 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$2756c514] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-02-18 15:55:19.592  WARN 15976 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-02-18 15:55:19.593  INFO 15976 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-02-18 15:55:19.601  WARN 15976 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-02-18 15:55:19.602  INFO 15976 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-02-18 15:55:23.326  INFO 15976 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2019-02-18 15:55:25.701  INFO 15976 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2019-02-18 15:55:25.806  INFO 15976 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2019-02-18 15:55:25.846  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2019-02-18 15:55:26.779  INFO 15976 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2019-02-18 15:55:26.779  INFO 15976 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2019-02-18 15:55:26.917  INFO 15976 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2019-02-18 15:55:26.917  INFO 15976 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2019-02-18 15:55:27.153  INFO 15976 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2019-02-18 15:55:27.154  WARN 15976 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Invalid eureka server URI: /; removing from the server pool
2019-02-18 15:55:27.822  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2019-02-18 15:55:27.822  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2019-02-18 15:55:27.822  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2019-02-18 15:55:27.822  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2019-02-18 15:55:27.822  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2019-02-18 15:55:27.823  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2019-02-18 15:55:27.823  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2019-02-18 15:55:28.089  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2019-02-18 15:55:28.093  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2019-02-18 15:55:28.096  INFO 15976 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2019-02-18 15:55:28.101  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1550476528099 with initial instances count: 3
2019-02-18 15:55:28.102  INFO 15976 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application USER-SERVICE with eureka with status UP
2019-02-18 15:55:28.103  INFO 15976 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1550476528103, current=UP, previous=STARTING]
2019-02-18 15:55:28.105  INFO 15976 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/localhost:user-service:8080: registering service...
2019-02-18 15:55:28.123  INFO 15976 --- [           main] c.hand.user.ShopServiceUserApplication   : Started ShopServiceUserApplication in 14.817 seconds (JVM running for 16.382)
2019-02-18 15:55:28.133  INFO 15976 --- [       Thread-5] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application USER-SERVICE with eureka with status DOWN
2019-02-18 15:55:28.133  WARN 15976 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1550476528133, current=DOWN, previous=UP]
2019-02-18 15:55:28.144  INFO 15976 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2019-02-18 15:55:28.161  INFO 15976 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/localhost:user-service:8080 - registration status: 204
2019-02-18 15:55:28.161  INFO 15976 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/localhost:user-service:8080: registering service...
2019-02-18 15:55:28.167  INFO 15976 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/localhost:user-service:8080 - registration status: 204
2019-02-18 15:55:28.168  INFO 15976 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2019-02-18 15:55:28.175  INFO 15976 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/localhost:user-service:8080 - deregister  status: 200
2019-02-18 15:55:28.186  INFO 15976 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient

解决方法:
pom.xml中添加如下依赖:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

2、SpringCloud Feign报错:PathVariable annotation was empty on param 0

错误日志如下:

Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
	at feign.Util.checkState(Util.java:128) ~[feign-core-9.5.1.jar:na]
	at org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor.processArgument(PathVariableParameterProcessor.java:51) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.cloud.openfeign.support.SpringMvcContract.processAnnotationsOnParameter(SpringMvcContract.java:238) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:110) ~[feign-core-9.5.1.jar:na]
	at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66) ~[feign-core-9.5.1.jar:na]
	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.5.1.jar:na]
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.5.1.jar:na]
	at feign.Feign$Builder.target(Feign.java:218) ~[feign-core-9.5.1.jar:na]
	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:39) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:223) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:244) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	... 30 common frames omitted

写Feign调用的时候@PathVariable中的value不能省略

在这里插入图片描述

注意:

  • 其他标注在方法上的SpringMVC注解不能省略(如@RequestParam、@RequestBody等)
  • @RequestBody的value可以省略,其他的不要省略
  • 禁止在标注@FeignClient的类上标注@RequestMapping注解

3、SpringCloud Feign报错:Method has too many Body parameters

错误日志如下:

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract void com.hand.shop.order.service.api.OrderServiceApi.addOrder(com.hand.shop.pojo.Order,java.util.List)
	at feign.Util.checkState(Util.java:128) ~[feign-core-9.5.1.jar:na]
	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:117) ~[feign-core-9.5.1.jar:na]
	at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66) ~[feign-core-9.5.1.jar:na]
	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.5.1.jar:na]
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.5.1.jar:na]
	at feign.Feign$Builder.target(Feign.java:218) ~[feign-core-9.5.1.jar:na]
	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:39) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:223) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:244) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	... 30 common frames omitted

写Feign调用方法的时候可以有多个@RequestParam,但只能有不超过一个@RequestBody

4、SpringBoot项目使用Maven打包Test时失败

错误日志如下:

-------------------------------------------------------------------------------
Test set: com.hand.user.ShopServiceUserApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.909 s <<< FAILURE! - in com.hand.user.ShopServiceUserApplicationTests
initializationError(com.hand.user.ShopServiceUserApplicationTests)  Time elapsed: 0.004 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

原因:建好SpringBoot项目之后修改了主启动类的名字,需要在测试类上的@SpringBootTest注解指定主启动类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShopServiceUser8080Application.class)
public class ShopServiceUser8080ApplicationTests {

	@Test
	public void contextLoads() {
	}

}

5、EurekaClient自定义服务名称信息和访问路径可以显示IP地址

eureka:
  client:
    service-url:
      defaultZone: ...
  instance: 
    instance-id: shop-service-cart-8100 #自定义服务名称信息
    prefer-ip-address: true #访问路径可以显示IP地址     

6、解决Error resolving template template might not exist or might not be accessible问题

Thymeleaf + Spring Boot在开发环境正常,但用jar运行时报错Error resolving template template might not exist or might not be accessible

这个问题我们都很好明白,就是模板页不存在,但是实际上它能找到模板页,但是在使用th:include标签的时候才会出错,在引用模板文件的时候不用”/”打头

猜你喜欢

转载自blog.csdn.net/qq_40378034/article/details/87862525