springboot使用openfeign固定ip请求进行负载均衡

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("哇卡卡,我真是个小天才!!");
    }

}

外部接口

不贴了,反正就是轮询调用了,嘎嘎!!!

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值