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


    }

  • 相关阅读:
    521.最长特殊序列 I
    520.检查大写字母
    459.重复的子字符串
    Java 读取 .properties 文件的几种方式
    Idea 使用教程
    db2 with用法
    DB2 alter 新增/删除/修改列
    Bootstrap treegrid 实现树形表格结构
    Mysql 递归查询
    navicat for mysql 下载安装教程
  • 原文地址:https://www.cnblogs.com/liuys635/p/10546131.html
Copyright © 2011-2022 走看看