重写feign客户端实现本地与开发/测试环境联调

公司网络调整后本地与Rancher上的开发/测试容器不能互通,重写替换路径为gateway路径实现联调

/*
 * Copyright 2013-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.openfeign.loadbalancer;

import feign.Client;
import feign.Request;
import feign.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.*;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.BackOffPolicy;
import org.springframework.retry.backoff.NoBackOffPolicy;
import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.net.URI;
import java.util.*;

import static org.springframework.cloud.openfeign.loadbalancer.LoadBalancerUtils.buildRequestData;

/**
 * A {@link Client} implementation that provides Spring Retry support for requests
 * load-balanced with Spring Cloud LoadBalancer.
 *
 * @author Olga Maciaszek-Sharma
 * @author changjin wei(魏昌进)
 * @author Wonsik Cheung
 * @author Andriy Pikozh
 * @since 2.2.6
 */
@SuppressWarnings({"rawtypes", "unchecked"})
class RetryableFeignBlockingLoadBalancerClient implements Client {

    private static final String BASE_URL = "172.16.172.130:23740";
    private static final Log LOG = LogFactory.getLog(RetryableFeignBlockingLoadBalancerClient.class);

    private final Client delegate;

    private final LoadBalancerClient loadBalancerClient;

    private final LoadBalancedRetryFactory loadBalancedRetryFactory;

    private final LoadBalancerClientFactory loadBalancerClientFactory;

    private final List<LoadBalancerFeignRequestTransformer> transformers;

    /**
     * @deprecated in favour of
     * {@link RetryableFeignBlockingLoadBalancerClient#RetryableFeignBlockingLoadBalancerClient(Client, LoadBalan
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign客户端是一个轻量级的HTTP客户端,它使用注解方式定义API接口,并且能够与Spring Cloud等微服务框架无缝集成。下面是实现Feign客户端的步骤: 1. 添加依赖 在Maven或Gradle中添加Feign客户端的依赖。 Maven: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' ``` 2. 定义API接口 使用Feign注解定义调用的API接口,例如: ```java @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/example") String getExample(); @PostMapping("/example") void postExample(@RequestBody Example example); } ``` 3. 注入Feign客户端 在需要调用API的代码中注入Feign客户端,例如: ```java @Service public class ExampleService { private final ExampleServiceClient exampleServiceClient; public ExampleService(ExampleServiceClient exampleServiceClient) { this.exampleServiceClient = exampleServiceClient; } public String getExample() { return exampleServiceClient.getExample(); } public void postExample(Example example) { exampleServiceClient.postExample(example); } } ``` 4. 配置Feign客户端 可以在配置文件中配置Feign客户端的一些属性,例如: ```yaml example-service: url: http://example-service connectTimeout: 5000 readTimeout: 5000 ``` 以上就是实现Feign客户端的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值