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


    }

  • 相关阅读:
    Go复习--为何不允许重载overload?
    Go疑问-1
    Go复习--for循环陷井
    Go复习--slice协程不安全
    Go复习之久违的goto语句
    Go复习---编译错误:undefined:
    Linux 环境拷贝文件发生的错误
    【转载】java数据库操作
    VBA文件处理
    【转】关于C#使用Excel的数据透视表的例子
  • 原文地址:https://www.cnblogs.com/liuys635/p/10546131.html
Copyright © 2011-2022 走看看