springboot使用openfeign固定ip请求
一个单体springboot项目,请求外部接口,这里想用一下openfeign,没有注册中心,又不能直接请求外部域名(会走流量滴),想着openfeign也是可以指定url进行请求的,但是外部接口又多项目部署,还让请求的时候负载均衡一下,怎么搞,openfeign请求url的时候轮询一下?看看网上一堆重写这,重写那的,好复杂,这里记录一下最后我这边怎么做的吧。
项目版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
<spring-boot.version>2.5.4</spring-boot.version>
</properties>
<dependencies>
// 省略其他
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意:这里的版本很重要!!!这里的版本很重要!!!这里的版本很重要!!!
配置文件
就配置了一个:
feign:
httpclient:
enabled: true
config
核心其实就是自定义config
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.ServiceInstanceListSuppliers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoadBalancerConfig {
@Bean
public ServiceInstanceListSupplier serviceInstanceListSupplier() {
//这里要看自己引用的openfeign和loadbalancer版本
return ServiceInstanceListSuppliers.from("my-service",
new DefaultServiceInstance("my-service", "my-service", "127.0.0.1", 9990, false),
new DefaultServiceInstance("my-service", "my-service", "127.0.0.1", 9991, false)
);
}
}
要注意版本:不同版本的DefaultServiceInstance构造器是不一样的!
feign接口
@FeignClient(name="my-service",url="",configuration = LoadBalancerConfig.class)
public interface TestService {
//具体请求接口地址
@GetMapping("/test")
String getTest(@RequestParam("id") List<Long> ids);
}
开启配置
@EnableFeignClients(basePackages = {"你的feign接口所在包"})
public class TestApp {
public static void main( String[] args )
{
SpringApplication.run(TestApp.class, args);
System.out.println("---TestApp启动完成----");
}
}
实际调用
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestService testService;
@Override
public void test() {
String response = zhishuService.getItemPutAwayTimeCenter(itemIds);
System.out.println("哇卡卡,我真是个小天才!!");
}
}
外部接口
不贴了,反正就是轮询调用了,嘎嘎!!!