org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://DEPT-8001/dept/get/1": DEPT-8001; nested exception is java.net.UnknownHostException: DEPT-8001
不识别主机名称、则可能是主机名称没在系统文件hosts中配置,但检查发现,已经配置,排除。
于是查了一下Ribbon的用法,发现使用Ribbon的时候,必须要在RestTemplate bean配置中添加负载均衡注解@LoadBalanced
于是加完如下:
package com.zemel.consumer.config;
import java.nio.charset.Charset;import java.util.Base64;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpHeaders;import org.springframework.web.client.RestTemplate;@Configurationpublic class RestConfig { @Bean public HttpHeaders getHeaders(){ // 进行一个Http头信息配置 HttpHeaders headers = new HttpHeaders(); String auth = "wendy:wendy"; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII"))); // 加密字符串要有空格 String authHeader = "Basic " + new String(encodedAuth); headers.set("Authorization", authHeader); return headers; } @Bean@LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); }}