首页 > 其他分享 >解决消费者从eureka注册中心获取的不是外网ip的问题

解决消费者从eureka注册中心获取的不是外网ip的问题

时间:2022-12-14 01:11:29浏览次数:54  
标签:ip boot springframework eureka 外网 org import public

因为生产者注册到注册中的不是ip,没有指定生产者所在服务的外网ip地址

在配置文件中进行如下配置

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0.0.1

  

 

 问题到此解决,下面列出问题排除的过程

我是在使用feign远程调用的时候,发现远程调用老是出现调用超时的问题,但是本地环境调用没有问题,这里我重写了feign的一些方法

package com.java.config;

import feign.Client;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/13 11:45
 */
@Component
public class RestConfig {

    public CachingSpringLoadBalancerFactory cachingLBClientFactory(
            SpringClientFactory factory) {
        return new CachingSpringLoadBalancerFactory(factory);
    }

    @Bean
    public Client feignClient(SpringClientFactory clientFactory) {
        CachingSpringLoadBalancerFactory bean = cachingLBClientFactory(clientFactory);
        return new LoadBalancerFeignClient(new InFeignClient(null, null), bean, clientFactory);
    }

}

  

package com.java.config;

import com.netflix.client.config.IClientConfig;
import feign.Client;
import feign.Request;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.URI;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/13 11:44
 */
@Slf4j
public class InFeignClient extends Client.Default {
    public InFeignClient(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
        super(sslContextFactory, hostnameVerifier);
    }

    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        try {

            return super.execute(request, options);
        } catch (IOException e) {
            log.warn(" 请求 {} 异常 ======> {}", request.url(), e.getMessage());
            throw e;
        }
    }
}

  

package com.java.service.impl;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 原来:http://lagou-service-resume/resume/openstate/ + userId;
 * @FeignClient表明当前类是一个Feign客户端,value指定该客户端要请求的服务名称(登记到注册中心上的服务提供者的服务名称)
 */
@FeignClient(value = "ip-service",fallback = ResumeFallback.class,path = "/queryIp")
public interface ResumeServiceFeignClient {


    /**
     * Feign要做的事情就是,拼装url发起请求,我们调用该方法就是调用本地接口方法,那么实际上做的是远程请求
     * @param ip
     * @return
     */
    @RequestMapping("/queryIpCity/{ip}")
    @ResponseBody
    public String queryIpCity(@PathVariable("ip") String ip);

}

测试类

package com.java.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.java.service.impl.ResumeServiceFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;



@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AutodeliverControllerTest {

    @Autowired
    private ResumeServiceFeignClient queryIpClient;

    @Test
    public void test() {
        String ip="43.254.105.235";
        log.info("【Feign方式调用】【查询ip地址详细信息】入参:{}", ip);
        String forObject = null;
        try {
            forObject = queryIpClient.queryIpCity(ip);
            log.info("【Feign方式调用】【查询ip地址详细信息】返回参数:{}", forObject);
            JSONObject jsonObject = JSON.parseObject(forObject);
            String city = jsonObject.getString("address");
            log.info("【Feign方式调用】【查询ip地址详细信息】返回参数:{}", city);
        } catch (Exception e) {
            log.error("【Feign方式调用】【调用获取ip信息接口异常】:{}",e);
        }

    }

}

  日志中打印的是内网ip地址,通过网上找资料,了解到了eureka.instance.ip-address这个key值

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.java</groupId>
    <artifactId>feign-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--spring boot 父启动器依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>


    <dependencyManagement>
        <dependencies>
            <!--spring cloud依赖管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--日志依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--eureka client 客户端依赖引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--Feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>qiuxie</finalName>
    </build>


</project>

  熔断类

package com.java.service.impl;


import org.springframework.stereotype.Component;

/**
 * 降级回退逻辑需要定义一个类,实现FeignClient接口,实现接口中的方法
 *
 *
 */
@Component  // 别忘了这个注解,还应该被扫描到
public class ResumeFallback implements ResumeServiceFeignClient {

    @Override
    public String queryIpCity(String ip) {
        return "去火星了....";
    }
}

  启动类

package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/13 10:07
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients  // 开启Feign 客户端功能
public class FeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class,args);
    }

}

  

 

 

 

 

 

 

 

 

 一般自动设置的是内网ip地址,如果你的多台服务器之间设置了对等连接,同时可以相互访问,使用内网ip地址没有问题,如果没有设置,需要手动设置你的服务所在服务器的外网ip地址

标签:ip,boot,springframework,eureka,外网,org,import,public
From: https://www.cnblogs.com/q202105271618/p/16979525.html

相关文章

  • Mac 下 VMware Fusion Linux 虚拟机设置静态 IP
    首先打开Mac终端查看网关cat/Library/Preferences/VMware\Fusion/vmnet8/nat.conf#NATgatewayaddressip=172.16.57.2 #网关(GATEWAY)netmask=25......
  • Jenkins实践指南-05-Jenkins pipeline 语法01
    3.Jenkinspipeline语法3.1pipeline组成  [作者:Surpassme]Jenkinspipeline是基于Groovy语言实现的一种DSL(领域特定语言),用于描述整条流水线是如何进行的。流水......
  • linux系统过滤ip地址总结
    Perl模块用法安装Perl模块#官网地址https://metacpan.org/pod/Regexp::Common#下载地址https://cpan.metacpan.org/authors/id/A/AB/ABIGAIL/Regexp-Common-2017060......
  • 故事终章,NOIP2022
    挥之不去的梦魇没有前情提要Day0上午到机房,监督高一做题,因为没有高二和初中。午饭前后打了一会儿CS,被薄纱。午饭尝试了酸菜鱼面,差点吐了,直接倒掉。快下午才睡午觉......
  • TypeScript:带属性关联的泛型对象解构问题研究
    TypeScript:带属性关联的泛型对象解构问题研究2020-08-24 1236简介: ##背景###利用泛型进行属性关联大家在业务中一定很熟悉这样的场景,针对某个action,传递一个指定......
  • MFC的固高环形倒立摆GRIP2002实验平台
     固高环形倒立摆GRIP2002是基于GT-400-SV-PCI运动控制卡的一个二级环形倒立摆(摆杆和连杆两根杆的摆),固高公司提供了一个DOS环境下的Demo和MATLAB7.0的simulink的Demo,但DO......
  • 你不知道的javascript 1.2理解作用域 (一)
    程序进行处理的参与者1引擎从头到尾负责整个JavaScript程序的编译及执行过程2编译器引擎的处理过程中的帮手之一,负责语法分析及代码生成(解析(parse)->transform(......
  • Missing boundary in multipart/form-data
    [13-Dec-202217:56:46Asia/Shanghai]PHPWarning:Missingboundaryinmultipart/form-dataPOSTdatainUnknownonline0最近在服务器中发现大量的Missingboun......
  • swiper常用属性
    <view> <!-- 指示点:indicator-dots 自动轮播:autoplay 轮播间隔:interval 使用衔接动画:circular 动画的时长:duration --> <swiperclass="swiper"indi......
  • antd 配置了@craco/craco后react-scripts报错问题
    react安装了antd配置了@craco/craco后运行yarnstart后直接报错:Cannotfindmodule'react-scripts\package.json'Requirestack:/*package.json*/"scripts":{-......