我使用 Eureka
注册服务并使用Feign
来发现服务,但如果我不添加 URL 参数,系统将报告 404 异常。我下面的代码 ID 不起作用:
@FeignClient(name = "CRAWLER-SERVICE")
下面的代码工作:
@FeignClient(name = "crawler-service", url = "http://192.168.199.229:8091/crawler")
堆栈跟踪:
feign.FeignException: status 404 reading CrawlerServiceApi#queryAllConditions(String,String)
at feign.FeignException.errorStatus(FeignException.java:62) ~[feign-core-9.5.0.jar:na]
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91) ~[feign-core-9.5.0.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-9.5.0.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) ~[feign-core-9.5.0.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103) ~[feign-core-9.5.0.jar:na]
at com.sun.proxy.$Proxy120.queryAllConditions(Unknown Source) ~[na:na]
at com.gezizhi.boss.controller.CrawlerConditionController.queryAllSource(CrawlerConditionController.java:29) ~[classes/:na]
at com.gezizhi.boss.controller.CrawlerConditionController$$FastClassBySpringCGLIB$$bd843b81.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
尤里卡配置是:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false
wait-time-in-ms-when-sync-empty: 0
客户端配置为:
eureka:
instance:
lease-expiration-duration-in-seconds: 2
lease-renewal-interval-in-seconds: 1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
服务器代码:
@RestController
@RequestMapping("/api/crawler")
public class CrawlerConditonApi {
private final CrawlerConditonService crawlerConditonService;
@Autowired
public CrawlerConditonApi(CrawlerConditonService crawlerConditonService) {
this.crawlerConditonService = crawlerConditonService;
}
@GetMapping("/conditions")
public String queryAllConditions(String belongDatasource, String conditionStatus) {
return crawlerConditonService.queryAllCondition(belongDatasource, conditionStatus);
}
@PostMapping("/conditions")
public String insertConditon(String params, String belongDatasource) {
return crawlerConditonService.insertConditon(params, belongDatasource);
}
@PutMapping("/conditions/{id}/status/{status}")
public String updateCondition(@PathVariable("id") String id, @PathVariable("status") String status) {
return crawlerConditonService.updateCondition(id, status);
}
}
客户端代码:
@FeignClient(name = "CRAWLER-SERVICE")
public interface CrawlerServiceApi {
@RequestMapping(value = "/api/papersources/", method = RequestMethod.GET)
String queryAllSource();
@RequestMapping(value = "/api/papersources/{id}/status/{status}", method = RequestMethod.PUT)
String updateSource(@PathVariable("id") String id, @PathVariable("status") String status);
@RequestMapping(value = "/api/crawler/conditions", method = RequestMethod.GET)
String queryAllConditions(@RequestParam("belongDatasource") String belongDatasource,
@RequestParam("conditionStatus") String conditionStatus);
@RequestMapping(value = "/api/crawler/conditions", method = RequestMethod.POST)
String insertConditon(@RequestParam("params") String params,
@RequestParam("belongDatasource") String belongDatasource);
@RequestMapping(value = "/api/crawler/conditions/{id}/status/{status}", method = RequestMethod.PUT)
String updateCondition(@PathVariable("id") String id, @PathVariable("status") String status);
}
我在控制器中使用:
@RestController
public class CrawlerConditionController {
private final CrawlerServiceApi crawlerServiceApi;
@Autowired
public CrawlerConditionController(CrawlerServiceApi crawlerServiceApi) {
this.crawlerServiceApi = crawlerServiceApi;
}
@GetMapping("/conditions/query")
public AbstractResult queryAllSource(@RequestParam("belongDatasource") String belongDatasource,
@RequestParam("conditionStatus") String conditionStatus) {
return ApiInvokeRspUtil.returnDataRowsInvokeRsp("CRL00000",
crawlerServiceApi.queryAllConditions(belongDatasource, conditionStatus));
}
@PostMapping("/conditions/add")
public AbstractResult addConditon(@RequestParam("paramJson") String paramJson,
@RequestParam("belongDataSource") String belongDataSource) {
return ApiInvokeRspUtil.returnDataRowsInvokeRsp("CRL00000",
crawlerServiceApi.insertConditon(paramJson, belongDataSource));
}
@PostMapping("/conditions/{id}/status/{status}")
public AbstractResult updateConditon(@PathVariable("id") String id,
@PathVariable("status") String status) {
return ApiInvokeRspUtil.returnDataRowsInvokeRsp("CRL00000",
crawlerServiceApi.updateCondition(id, status));
}
}
application.yml,其中包含CrawlerConditonApi的项目
server:
context-path: /crawler
port: 8091
eureka:
instance:
lease-expiration-duration-in-seconds: 2
lease-renewal-interval-in-seconds: 1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
spring:
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mysql://localhost:3306/gezizhi?useUnicode=true&characterEncoding=utf-8
username: root
password: password
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
redis:
database: 0
host: 127.0.0.1
port: 6379
password: password
pool:
max-idle: 10
max-wait: 10000
max-active: 1024
min-idle: 1
timeout: 3000
application:
name: crawler-service
mybatis:
type-aliases-package: com.gezizhi.crawler.dao.domain
mapper-locations: classpath:mybatis/*.xml
这是一个老问题,仍然发布我的答案,所以它可能会帮助其他人,因为我遇到了同样的问题,最终能够解决它。
它的发生是由于server.context
设置为crawler
。由于这有效地添加了一个新的 url 段,因此您需要将假客户端更改为下面(请注意在服务名称之后添加的上下文(
@FeignClient(name = "CRAWLER-SERVICE/crawler")
或者您需要将RequestMapping
更改为如下所示的内容(再次注意在服务 url 之前添加的上下文(:
@RequestMapping("crawler/<rest of the url>")