【java后端】采坑合集

bug

    • 【java】java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    • 【联调】`{$ref: “$[0]“}`
    • 【联调】Required String parameter ‘id‘ is not present
    • 【mysql】{dataSource-1} closed
    • 【mysql】LocalDateTime写入数据库有时差
    • 【mysql】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
    • 【mybatis】``中`test`的`==`判断不生效
    • 【mybatis】传参到xml后不报错但查询失败
    • 【mybatis plus】mybatis plus Invalid bound statement (not found):
    • 【gateway】Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigure
    • 【RabbitMQ】springcloud config bus 刷新配置报错
    • 【feign-hystrix】feign hystrix enabled设置无效
    • 【feign】feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo

【java】java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

原因:该数组值不存在,if判断或try catch

List<SysOrg> orgList=sysOrgService.getByUserId(su.getUserId());
 String orgName="";
 if (orgList!=null&&orgList.size()>0) {                
     orgName=orgList.get(0).getOrgName();
     su.setOrgName(orgName);
     list2.add(su);
 }

【联调】{$ref: “$[0]“}

问题描述
后台接口代码

if(res1 != null){
   res.add(res1.get(0));
   res.add(res1.get(0));
}
return JSON.toJSONString(res)

前端请求返回

0: {loan_score: 97, absence_score: 77.5, night_score: 73.8, game_score: 86.5, video_score: 86.84}
1: {$ref: "$[0]"}

问题解决
SerializerFeature.DisableCircularReferenceDetect

import com.alibaba.fastjson.serializer.SerializerFeature;

return JSON.toJSONString(res,SerializerFeature.DisableCircularReferenceDetect);

【联调】Required String parameter ‘id‘ is not present

  1. 若后台用@RequestParam接收参数,contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
  2. 若后台用@RequestBody接收参数,contentType: 'application/json;charset=UTF-8',
//如果前端传入的是json数据那么后端使用,通过map.get(“id”)获取对应的数据
@RequestBody HashMap<String, String> map

//如果前端传入的是正常表单数据,那么后端使用
@RequestParam("id") String id
@RequestParam(value="id", required = false) String id

【mysql】{dataSource-1} closed

问题描述
好久不打开的程序打开,突然无法运行成功,没有其他报错信息,就是 {dataSource-1} init完很快就 {dataSource-1} closed,然后failed

[extShutdownHook] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed

问题分析

  1. 验证数据库本身无误可连接
  2. 验证application.yml无误
  3. pom的mysql-connector-java更换version也无效

问题解决
去掉pom.xml中的spring-boot-starter-tomcat就能运行了

猜测是和springboot的tomcat有冲突,也不知道之前是怎么运行无误的

【mysql】LocalDateTime写入数据库有时差

jdbc连接里边有这样一个参数&serverTimezone=UTC,改为&serverTimezone=CTT即可
CTT 指Asia/Shanghai

spring:
  datasource:
    url: jdbc:mysql://XXXX:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT

【mysql】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded

具体原因不清楚,尝试的多种方法都无效,应该是格式问题
删掉旧的application.yml重新创建application.yml配置就好了

【mybatis】test==判断不生效

问题
代码如下,一直以为是的问题,怎么改都没用

<sql id="List_Index">
        (select * from warning where event = #{event} group by phone,
        <if test="event!='1'">touch_time</if>
        <if test="event=='1'">substring(touch_time,1,10)</if>)
 </sql>
 <select id="getIndexByDate" resultMap="CardIndex">
 		select count(*) as total from <include refid="List_Index"/> as l
 </select>

解决
数字加单引号会被默认为数字,和字符串的event比较会不相等,从而导致==判断失效,可通过toString()转换

<if test="event!='1'.toString()">touch_time</if>
<if test="event=='1'.toString()">substring(touch_time,1,10)</if>)

【mybatis】传参到xml后不报错但查询失败

解决#{}改为${}#{}会为变量加"",当传送字符串到select,会查询不到结果

${}会把传入的参数直接显示,而#{}会把传入的参数加双引号当字符串处理

  1. #{}是预编译处理,${}是字符串替换
  2. Mybatis在处理#{}时,会将sql中的#{}替换为占位符‘?’,调用PreparedStatement的set方法来赋值
  3. Mybatis在处理${}时,就是把${}替换成变量的值
  4. 使用#{}可以有效的防止SQL注入,提高系统安全

【mybatis plus】mybatis plus Invalid bound statement (not found):

问题描述
配置都没错,打包后target/classes目录下没有生成xml
原因分析
使用idea打包时,会过滤一些文件
开始解决

  1. pox.xml文件配置

       <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.ymlinclude>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    
  2. maven必须先cleancampile重新编译,查看target下生成xml后,再运行程序。

注意修改完后一定要campile重新编译,之前一直clean后直接运行,会报错找不到url配置,连yml文件也无法生成。

  1. 如果报数据库url错误,依然无法生成yml文件,可参考官方教程解决 mybatis-plus官网问答,注意配置文件mybatis-plus.mapper-locations根据自己实际位置
<build>
  <resources>
      <resource>
          
          <directory>src/main/javadirectory>
          <includes>
              <include>**/*.xmlinclude>
          includes>
      resource>
      
      <resource>
          <directory>src/main/resourcesdirectory>
      resource>
  resources>
build>

【gateway】Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigure

问题描述
nacos+gateway+sentinel,按网上的配置启动后总是报错
原因分析
多方尝试后发现问题主要在gateway的依赖里包含了spring-boot-starter-web,其他依赖里也有造成了冲突
解决方案
删除其他的spring-boot-starter-web依赖

【RabbitMQ】springcloud config bus 刷新配置报错

报错一:/bus-refresh刷新无效

RabbitMQ需要提前安装好

报错二:socket closed
原因
服务器安装完mq后认为别人配置中的port:5672写错了,应该写成RabbitMQ请求web的端口15672
解决
改回5672

报错三:bus mq connection error; protocol method: #method(reply-code=530, reply-text=NOT_ALLOWED - access to vhost ‘/’ refused for user ‘root’, class-id=10, method-id=40)

给用户的权限下添加虚拟主机

【java后端】采坑合集_第1张图片

一开时配置management.endpoints.web.exposure.include='*'总是没有自动提示(这个原因还没找到,总觉得是换了idea社区版的原因),只有加了spring-boot-starter-web才能正常配置,就导致了与gateway的冲突

【feign-hystrix】feign hystrix enabled设置无效

使用的是springboot的最新版本2.4.3和springcloud的最新版本2020.0.1,对应的feign版本不包含hystrix依赖

  • 更换springboot版本为2.1.6.RELEASE
  • 更换springclould版本Greenwich.SR1

【feign】feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo

Flipping property: nacos-authserver.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo
	at feign.FeignException.errorExecuting(FeignException.java:132)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
	at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
	at com.sun.proxy.$Proxy102.getUserInfo(Unknown Source)
	at com.example.producer.controller.UserController.getUserInfo(UserController.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
	at com.github.xiaoymin.swaggerbootstrapui.filter.SecurityBasicAuthFilter.doFilter(SecurityBasicAuthFilter.java:80)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at com.github.xiaoymin.swaggerbootstrapui.filter.ProductionSecurityFilter.doFilter(ProductionSecurityFilter.java:53)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:364)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
	at sun.net.www.http.HttpClient.New(HttpClient.java:308)
	at sun.net.www.http.HttpClient.New(HttpClient.java:326)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at feign.Client$Default.convertResponse(Client.java:143)
	at feign.Client$Default.execute(Client.java:68)
	at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:93)
	at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56)
	at com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:104)
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)
	at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:231)
	at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:228)
	at rx.Observable.unsafeSubscribe(Observable.java:10327)
	at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)
	at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144)
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
	at rx.Observable.unsafeSubscribe(Observable.java:10327)
	at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)
	at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)
	at rx.Observable.unsafeSubscribe(Observable.java:10327)
	at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber$1.call(OperatorRetryWithPredicate.java:127)
	at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73)
	at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52)
	at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79)
	at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45)
	at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
	at rx.Subscriber.setProducer(Subscriber.java:209)
	at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
	at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
	at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
	at rx.Observable.subscribe(Observable.java:10423)
	at rx.Observable.subscribe(Observable.java:10390)
	at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443)
	at rx.observables.BlockingObservable.single(BlockingObservable.java:340)
	at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112)
	at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:83)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:108)
	... 106 more

原因

如果服务都在远程服务器没问题,如果服务都在本地启动没问题,但nacos服务与被调用的nacos-authserver服务都运行在服务器,而本服务在本地开发,所以是因为注册服务ip问题,导致本地服务无法通过服务名字找到相应的服务

  • nacos服务与被调用的nacos-authserver服务都运行在服务器,所以通过服务器ip测试接口正常
  • 本服务在本地(127.0.0.1)开发调试,通过nacos服务器ip来注册服务,所以本服务注册正常
  • 本服务通过nacos服务找到nacos-authserver服务,nacos-authserver服务会告诉nacos服务本服务的ip是127.0.0.1,所以无法找到

解决
所以在本地开发时,可以指定fegin的url地址

@FeignClient(value = "nacos-authserver",url = "http://XXXX:18890/nacos-authserver",configuration = OAuth2FeignRequestInterceptor.class)
public interface UserRemote {
    @GetMapping("/user/userinfo")
    Object getUserInfo();
}

你可能感兴趣的:(后端,java,spring,boot)