Giter VIP home page Giter VIP logo

Comments (13)

aCoder2013 avatar aCoder2013 commented on June 16, 2024 1

@lzn312 可以参考下官方的文档,用@ RibbonClients 注解修饰你的配置类,这样会将这个配置类中的bean放在独立的spring上下文,而不是父context,https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html#_customizing_the_default_for_all_ribbon_clients

from blog.

Tinkerc avatar Tinkerc commented on June 16, 2024

你好,请教一下CustomHttpRequestInterceptor 怎么添加到 LoadBalancerInterceptor 之后

from blog.

aCoder2013 avatar aCoder2013 commented on June 16, 2024

@Tinkerc 因为要获取到实际的IP,因此必须保证你添加的拦截器的代码在ribbon初始化的地方之后执行。

有几种方式,比如你可以监听Spring初始化完成事件,然后注入RestTemplate,把自己自定义的拦截器添加进去。

from blog.

Tinkerc avatar Tinkerc commented on June 16, 2024

@aCoder2013 嗯,我也是要获取实际IP,可以把关键源码贴出来一下吗,重新注入RestTemplate是否还需要单独设置@LoadBalanced

from blog.

aCoder2013 avatar aCoder2013 commented on June 16, 2024

嗯,重新注入的是ribbon初始化完成之后的RestTemplate,只需要给RestTemplate再添加一个拦截器即可

    @Resource
    private RestTemplate restTemplate;

    private AtomicBoolean started = new AtomicBoolean(false);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event != null) {
            if (started.compareAndSet(false, true)) {
                /*
                    Spring初始化完成后再执行
                 */
                List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                if (interceptors == null) {
                    interceptors = new ArrayList<>();
                }
                interceptors.add(new LogClientHttpRequestInterceptor());
                restTemplate.setInterceptors(interceptors);
            }
        }
    }

from blog.

lzn312 avatar lzn312 commented on June 16, 2024

你好目前我也遇到了这个问题,在spring boot 1.5x集成ribbon和feign。**是通过自定义规则实现某些特殊请求规则定义。但是和你记录的一样当请求返回时。rule的lb选择错误导致了404,自定义的规则不能去掉。请问第二种解法能详细说一说吗?

from blog.

lzn312 avatar lzn312 commented on June 16, 2024

@aCoder2013 非常感谢你的回复,该问题已经通过配置@RibbonClients解决

from blog.

broadvieweditor avatar broadvieweditor commented on June 16, 2024

博主您好,我是电子工业出版社博文视点的编辑,看到您发表的文章,觉得内容很好,您是否有兴趣出版图书呢:)

我的微信是472954195

from blog.

Sunjb123 avatar Sunjb123 commented on June 16, 2024

你好,是要在@RibbonClients中再次配置IRule么

from blog.

AKwang100 avatar AKwang100 commented on June 16, 2024

非常感谢,终于找到一个花了心思的靠谱解释

from blog.

aCoder2013 avatar aCoder2013 commented on June 16, 2024

@AKwang100 很高兴能帮到你

from blog.

Veklip avatar Veklip commented on June 16, 2024

你好,我的微信是Tnsg_ui_lip,想请教一下你这个问题,我也遇到这个情况,但是我们没有定义IRule,我暂时加了discoverClient#getInstances打印某个服务所有的uri

from blog.

NelsonFeng avatar NelsonFeng commented on June 16, 2024

@aCoder2013 非常感谢你的回复,该问题已经通过配置@RibbonClients解决

麻烦问下如何解决的哇我用了@RibbonClients但是还是404

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.