因为生产者注册到注册中的不是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