首页 > 编程语言 > 详细

springcloud ribbon 客户端负载均衡用法

时间:2019-03-17 12:09:55      阅读:176      评论:0      收藏:0      [点我收藏+]

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;

@Configuration
public 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();
}


}

springcloud ribbon 客户端负载均衡用法

原文:https://www.cnblogs.com/liuys635/p/10546131.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!