下面是我的 bom 微服务:
@RestController
@Configuration
@ComponentScan
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableAutoConfiguration
public class PLMBommsApplication {
private static final Logger LOG = LoggerFactory.getLogger(PLMBommsApplication.class);
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody StringBuilder bom() throws URISyntaxException {
StringBuilder response = new StringBuilder();
String result = null;
LOG.info("Starting to send to Part Microservice==============>>>");
LOG.info("Starting call====>");
List<ServiceInstance> serviceInstance = discoveryClient.getInstances("plm-part-ms");
ServiceInstance partInstance = serviceInstance.get(0);
try {
MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
mvm.add("abc", "Hello Part");
String urlString = "http://" + partInstance.getHost() + ":" + Integer.toString(partInstance.getPort())
+ "/partPost";
LOG.info("URL String: " + urlString);
result = restTemplate.postForObject(urlString, mvm, String.class);
response.append("successPost = " + result);
response.append(System.getProperty("line.separator"));
} catch (Exception e) {
response.append(System.getProperty("line.separator"));
response.append("failPost = " + printException(e));
response.append(System.getProperty("line.separator"));
}
try {
result = restTemplate.getForObject(partInstance.getUri(), String.class);
response.append("successGet = " + result);
response.append(System.getProperty("line.separator"));
} catch (Exception e) {
response.append(System.getProperty("line.separator"));
response.append("failGet = " + printException(e));
response.append(System.getProperty("line.separator"));
}
return response;
}
public static String printException(Throwable t) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
if (t instanceof StorageException) {
if (((StorageException) t).getExtendedErrorInformation() != null) {
System.out.println(String.format("\nError: %s",
((StorageException) t).getExtendedErrorInformation().getErrorMessage()));
}
}
return (String.format("Exception details:\n%s", stringWriter.toString()));
}
@RequestMapping(value = "/bomPost", method = { RequestMethod.POST })
public String bomPost(@RequestParam(value = "abc") String param1) {
LOG.info("in plm-bom-ms post method: " + param1);
return "return response from plm-bom-ms post method";
}
@RequestMapping(value = "/bomGet", method = { RequestMethod.GET })
public String bomGet() {
LOG.info("in plm-bom-ms get method");
return "return response from plm-bom-ms get method";
}
public static void main(String[] args) {
SpringApplication.run(PLMBommsApplication.class, args);
}
}
上面的微服务正在尝试对另一个微服务进行休息调用,部分微服务:
@RestController
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PLMPartmsApplication {
private static final Logger LOG = LoggerFactory.getLogger(PLMPartmsApplication.class);
@Autowired
private DiscoveryClient discoveryClient;
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody StringBuilder part() throws URISyntaxException {
StringBuilder response = new StringBuilder();
String result = null;
LOG.info("Starting to send to BOM Microservice==============>>>");
LOG.info("Starting call====>");
List<ServiceInstance> serviceInstance = discoveryClient.getInstances("plm-bom-ms");
ServiceInstance bomInstance = serviceInstance.get(0);
try {
MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
String urlString = "http://" + bomInstance.getHost() + ":" + Integer.toString(bomInstance.getPort())
+ "/bomPost";
LOG.info("URL String: " + urlString);
mvm.add("abc", "Hello BOM");
result = restTemplate.postForObject(urlString, mvm, String.class);
response.append("successPost = " + result);
response.append(System.getProperty("line.separator"));
} catch (Exception e) {
response.append(System.getProperty("line.separator"));
response.append("failPost = " + printException(e));
response.append(System.getProperty("line.separator"));
}
try {
String urlString = "http://" + bomInstance.getHost() + ":" + Integer.toString(bomInstance.getPort())
+ "/bomGet";
LOG.info("URL String: " + urlString);
result = restTemplate.getForObject(urlString, String.class);
response.append("successGet = " + result);
response.append(System.getProperty("line.separator"));
} catch (Exception e) {
response.append(System.getProperty("line.separator"));
response.append("failGet = " + printException(e));
response.append(System.getProperty("line.separator"));
}
return response;
}
public static String printException(Throwable t) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
if (t instanceof StorageException) {
if (((StorageException) t).getExtendedErrorInformation() != null) {
System.out.println(String.format("\nError: %s",
((StorageException) t).getExtendedErrorInformation().getErrorMessage()));
}
}
return (String.format("Exception details:\n%s", stringWriter.toString()));
}
@RequestMapping(value = "/partPost", method = { RequestMethod.POST })
public String partPost(@RequestParam(value = "abc") String param1) {
LOG.info("in plm-part-ms post method: " + param1);
return "in plm-part-ms post method";
}
@RequestMapping(value = "/partGet", method = { RequestMethod.GET })
public String partGet() {
LOG.info("in plm-part-ms get method ");
return "in plm-part-ms get method";
}
public static void main(String[] args) {
SpringApplication.run(PLMPartmsApplication.class, args);
}
}
我得到以下异常:
Exception details:\njava.lang.IllegalStateException: No instances available for 100.77.236.18\r\n\tat org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:79)\r\n\tat org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:46)\r\n\tat org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:85)\r\n\tat org.springframework.cloud.netflix.metrics.MetricsClientHttpRequestInterceptor.intercept(MetricsClientHttpRequestInterceptor.java:65)\r\n\tat org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:85)\r\n\tat org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:69)\r\n\tat org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)\r\n\tat org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)\r\n\tat org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596)\r\n\tat org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)\r\n\tat org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)\r\n\tat com.jci.bommsplm.PLMBommsApplication.bom(PLMBommsApplication.java:83)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:622)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:729)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:261)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:115)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:103)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)\r\n\tat org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)\r\n\tat org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:745)\r\n\r\n\r\n
Eureka、BOM 和 Part 微服务部署在 azure 上。Eureka 注册了微服务,并且我可以使用“/service-instances/plm-part-ms”在 bom 微服务中分割微服务细节,其中“plm-part-ms”是部分微服务的 Spring 应用程序名称。任何帮助将不胜感激 !