zoukankan      html  css  js  c++  java
  • springcloud ribbon 客户端负载均衡用法

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


    }

  • 相关阅读:
    shell脚本中判断上一个命令是否执行成功
    nginx 414 Request-URI Too Large
    nginx 重写URL尾部斜杠
    Linux shell 日期,时间相关的命令
    shell脚本中自定义日志记录到文件
    scanf后面跟一个getchar
    1.Tarball软件make与makefile详解(还需要补充)
    <>和“”的区别
    malloc,calloc,realloc,alloc
    toString()方法细节
  • 原文地址:https://www.cnblogs.com/liuys635/p/10546131.html
Copyright © 2011-2022 走看看