5

我有两个不同的微服务,将 eureka 作为服务注册表,现在我试图从另一个微服务调用一个微服务,使用功能区解析端点以实现客户端负载平衡。

服务一:

该服务公开了一个端点,http://localhost:15000/api/user/{userId}application.yml 如下,

# Spring properties
spring:
  application:
    name: user_microservice
  cloud:
    config:
      discovery:
        enabled: false

# HTTP Server
server:
  port: 15000  # HTTP (Tomcat) port



# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    instanceId: ${spring.application.name}:${random.int}
    metadataMap:
      instanceId: ${spring.application.name}:${random.int}

logging:
  level:
    com.netflix.discovery: 'OFF'
    org.springframework.cloud: 'DEBUG'

调用服务 A 的服务 B 具有以下应用程序类:

@EnableDiscoveryClient
@SpringBootApplication
public class UserSummaryApplication {

    public static void main(String[] args) {

        SpringApplication.run(UserSummaryApplication.class, args);
    }

    @Bean(name = "restTemplate")
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean(name = "loadBalancedRestTemplate")
    @LoadBalanced
    RestTemplate loadBalancedRestTemplate() {
        return new RestTemplate();
    }
}

控制器

@RestController
@RequestMapping("/api/usersummary")
public class UserSummaryController {

    @Autowired
    private UserSummaryService userSummaryService;

    @RequestMapping(value="/{userId}", method= RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public UserSummary getUserSummary(@PathVariable String userId){
        return userSummaryService.getUserSummary(userId);
    }
}

服务

@Service
public class UserSummaryService {

    // Uses Ribbon to load balance requests
    private RestTemplate loadBalancedRestTemplate;

    public UserSummary getUserSummary(String UserId){

        String url = String.format("http://%s%s","user_microservice","/api/user/{userId}");
        logger.info(url);
        try {
            return this.loadBalancedRestTemplate.getForObject(url,
                    User.class, UserId).toString();

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Autowired
    public void setLoadBalancedRestTemplate(RestTemplate loadBalancedRestTemplate) {
        this.loadBalancedRestTemplate = loadBalancedRestTemplate;
    }

}

但是当我尝试访问该服务时出现异常:

java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://user_microservice/api/user/599f877c8e365c0001cec8d1
usersummary_microservice_1  |   at org.springframework.util.Assert.state(Assert.java:70)
usersummary_microservice_1  |   at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:54)
usersummary_microservice_1  |   at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:86)
usersummary_microservice_1  |   at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:70)
usersummary_microservice_1  |   at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
usersummary_microservice_1  |   at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
usersummary_microservice_1  |   at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652)
usersummary_microservice_1  |   at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
usersummary_microservice_1  |   at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
usersummary_microservice_1  |   at com.haulmatic.usersummary.service.userSummaryService.getuserSummary(UserSummaryService.java:33)
usersummary_microservice_1  |   at com.haulmatic.usersummary.controller.userSummaryController.getuserSummary(UserSummaryController.java:29)
usersummary_microservice_1  |   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
usersummary_microservice_1  |   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
usersummary_microservice_1  |   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
usersummary_microservice_1  |   at java.lang.reflect.Method.invoke(Method.java:498)
usersummary_microservice_1  |   at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
usersummary_microservice_1  |   at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
usersummary_microservice_1  |   at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
usersummary_microservice_1  |   at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
usersummary_microservice_1  |   at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
usersummary_microservice_1  |   at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
usersummary_microservice_1  |   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
usersummary_microservice_1  |   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
usersummary_microservice_1  |   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
usersummary_microservice_1  |   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
usersummary_microservice_1  |   at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
usersummary_microservice_1  |   at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
usersummary_microservice_1  |   at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
usersummary_microservice_1  |   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
usersummary_microservice_1  |   at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
usersummary_microservice_1  |   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
usersummary_microservice_1  |   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
usersummary_microservice_1  |   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
usersummary_microservice_1  |   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
usersummary_microservice_1  |   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
usersummary_microservice_1  |   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
usersummary_microservice_1  |   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
usersummary_microservice_1  |   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
usersummary_microservice_1  |   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
usersummary_microservice_1  |   at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
usersummary_microservice_1  |   at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
usersummary_microservice_1  |   at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
usersummary_microservice_1  |   at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
usersummary_microservice_1  |   at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
usersummary_microservice_1  |   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
usersummary_microservice_1  |   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
usersummary_microservice_1  |   at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
usersummary_microservice_1  |   at java.lang.Thread.run(Thread.java:748)

对此有任何指示吗?

4

1 回答 1

11

主机名中的下划线 ( _) 无效。用破折号 ( -) 命名您的服务。

new URI("http://user_microservice/stuff").getHost() // null
new URI("http://user-microservice/stuff").getHost() // user-microservice

所以在客户端改spring.application.nameuser-microserviceURL。

于 2017-08-30T05:31:31.010 回答