对于弹簧云来说是新手。
实施Spring Cloud Feign并遇到问题。 Eureka服务器已启动并正在运行,并且正在其中注册以下服务。
GreetingApp
application.properties
server.port= 14000
server.context-path= /greetservices
spring.application.name=GreetingApp
management.endpoints.web.exposure.include=*
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
主班
@SpringBootApplication
@EnableEurekaClient
public class GreetingappApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingappApplication.class, args);
}
}
控制器
@RestController
@RequestMapping("/greeting")
public class GreetingController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
Environment environment;
@GetMapping(value = "/user")
public String greetUser() {
logger.debug(" **** In greetUser(): Server Port " + environment.getProperty("local.server.port"));
return "Hello " + "Abdul";
}
}
我能够正常访问此应用。
虚假客户端应用
application.properties
server.port= 15000
server.context-path= /gaconsumerservice
management.endpoints.web.exposure.include=*
spring.application.name=GreetingAppFeignConsumer
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
eureak.client.registerWithEureka=false
eureak.client.fetchRegistry=false
主班
@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableFeignClients
@EnableEurekaClient
public class GreetingappconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingappconsumerApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
伪装客户端界面
@FeignClient(name = "GreetingApp")
public interface GAConsumerService {
@RequestMapping(value = "/greeting/user", method = RequestMethod.GET)
public String greetUser();
}
控制器
@RestController
@RequestMapping("/greeting")
public class GAConsumerController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
GAConsumerService gaConsumerService;
@GetMapping(value = "/user")
public String greetUser() {
logger.debug(" *** In /feign/gaconsumer::greetUser()");
return gaConsumerService.greetUser();
}
}
在所有这些之后,当我尝试访问以下网址时,其抛出以下异常
成功: http://localhost:14000/greetservices/greeting/user
例外: http://localhost:15000/gaconsumerservice/greeting/user
2019-07-24 16:25:42,109 INFO [http-nio-15000-exec-3]
com.netflix.config.ChainedDynamicProperty$ChainLink: Flipping property: GreetingApp.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-07-24 16:25:42,117 INFO [http-nio-15000-exec-3]
com.netflix.loadbalancer.DynamicServerListLoadBalancer: DynamicServerListLoadBalancer for client GreetingApp initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=GreetingApp,current list of Servers=[localhost:14000],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:localhost:14000; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 05:30:00 IST 1970; First connection made: Thu Jan 01 05:30:00 IST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@363b7066
2019-07-24 16:25:42,569 ERROR [http-nio-15000-exec-3]
org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [/gaconsumerservice] threw exception [Request processing failed; nested exception is feign.FeignException: status 404 reading GAConsumerService#greetUser(); content:
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1></body></html>] with root cause
feign.FeignException: status 404 reading GAConsumerService#greetUser(); content:
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1></body></html>
at feign.FeignException.errorStatus(FeignException.java:62)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy95.greetUser(Unknown Source)
at org.hams.services.greetingappconsumer.controller.GAConsumerController.greetUser(GAConsumerController.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
请帮助。
答案 0 :(得分:0)
请在假客户端界面中添加@ResponseBody
public interface GAConsumerService {
@RequestMapping(value = "/greeting/user", method = RequestMethod.GET)
@ResponseBody public String greetUser();
}