千家信息网

spring cloud中FeignClientBuilder的用法

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要讲解了"spring cloud中FeignClientBuilder的用法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"spring c
千家信息网最后更新 2025年12月02日spring cloud中FeignClientBuilder的用法

这篇文章主要讲解了"spring cloud中FeignClientBuilder的用法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"spring cloud中FeignClientBuilder的用法"吧!

本文主要研究一下spring cloud的FeignClientBuilder

FeignClientBuilder

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/FeignClientBuilder.java

public class FeignClientBuilder {        private final ApplicationContext applicationContext;        public FeignClientBuilder(final ApplicationContext applicationContext) {                this.applicationContext = applicationContext;        }        public  Builder forType(final Class type, final String name) {                return new Builder<>(this.applicationContext, type, name);        }        /**         * Builder of feign targets.         *         * @param  type of target         */        public static final class Builder {                private FeignClientFactoryBean feignClientFactoryBean;                private Builder(final ApplicationContext applicationContext, final Class type,                                final String name) {                        this.feignClientFactoryBean = new FeignClientFactoryBean();                        this.feignClientFactoryBean.setApplicationContext(applicationContext);                        this.feignClientFactoryBean.setType(type);                        this.feignClientFactoryBean.setName(FeignClientsRegistrar.getName(name));                        this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name));                        // preset default values - these values resemble the default values on the                        // FeignClient annotation                        this.url("").path("").decode404(false).fallback(void.class)                                        .fallbackFactory(void.class);                }                public Builder url(final String url) {                        this.feignClientFactoryBean.setUrl(FeignClientsRegistrar.getUrl(url));                        return this;                }                public Builder contextId(final String contextId) {                        this.feignClientFactoryBean.setContextId(contextId);                        return this;                }                public Builder path(final String path) {                        this.feignClientFactoryBean.setPath(FeignClientsRegistrar.getPath(path));                        return this;                }                public Builder decode404(final boolean decode404) {                        this.feignClientFactoryBean.setDecode404(decode404);                        return this;                }                public Builder fallback(final Class fallback) {                        FeignClientsRegistrar.validateFallback(fallback);                        this.feignClientFactoryBean.setFallback(fallback);                        return this;                }                public Builder fallbackFactory(final Class fallbackFactory) {                        FeignClientsRegistrar.validateFallbackFactory(fallbackFactory);                        this.feignClientFactoryBean.setFallbackFactory(fallbackFactory);                        return this;                }                /**                 * @param  the target type of the Feign client to be created                 * @return the created Feign client                 */                public  T build() {                        return this.feignClientFactoryBean.getTarget();                }        }}
  • FeignClientBuilder提供了forType静态方法用于创建Builder;Builder的构造器创建了FeignClientFactoryBean,其build方法使用FeignClientFactoryBean的getTarget()来创建目标feign client

实例

public class FeignClientBuilderTests {        @Rule        public ExpectedException thrown = ExpectedException.none();        private FeignClientBuilder feignClientBuilder;        private ApplicationContext applicationContext;        private static Object getDefaultValueFromFeignClientAnnotation(                        final String methodName) {                final Method method = ReflectionUtils.findMethod(FeignClient.class, methodName);                return method.getDefaultValue();        }        private static void assertFactoryBeanField(final FeignClientBuilder.Builder builder,                        final String fieldName, final Object expectedValue) {                final Field factoryBeanField = ReflectionUtils                                .findField(FeignClientBuilder.Builder.class, "feignClientFactoryBean");                ReflectionUtils.makeAccessible(factoryBeanField);                final FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) ReflectionUtils                                .getField(factoryBeanField, builder);                final Field field = ReflectionUtils.findField(FeignClientFactoryBean.class,                                fieldName);                ReflectionUtils.makeAccessible(field);                final Object value = ReflectionUtils.getField(field, factoryBean);                assertThat(value).as("Expected value for the field '" + fieldName + "':")                                .isEqualTo(expectedValue);        }        @Before        public void setUp() {                this.applicationContext = Mockito.mock(ApplicationContext.class);                this.feignClientBuilder = new FeignClientBuilder(this.applicationContext);        }        @Test        public void safetyCheckForNewFieldsOnTheFeignClientAnnotation() {                final List methodNames = new ArrayList();                for (final Method method : FeignClient.class.getMethods()) {                        methodNames.add(method.getName());                }                methodNames.removeAll(                                Arrays.asList("annotationType", "value", "serviceId", "qualifier",                                                "configuration", "primary", "equals", "hashCode", "toString"));                Collections.sort(methodNames);                // If this safety check fails the Builder has to be updated.                // (1) Either a field was removed from the FeignClient annotation and so it has to                // be removed                // on this builder class.                // (2) Or a new field was added and the builder class has to be extended with this                // new field.                assertThat(methodNames).containsExactly("contextId", "decode404", "fallback",                                "fallbackFactory", "name", "path", "url");        }        @Test        public void forType_preinitializedBuilder() {                // when:                final FeignClientBuilder.Builder builder = this.feignClientBuilder                                .forType(FeignClientBuilderTests.class, "TestClient");                // then:                assertFactoryBeanField(builder, "applicationContext", this.applicationContext);                assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class);                assertFactoryBeanField(builder, "name", "TestClient");                assertFactoryBeanField(builder, "contextId", "TestClient");                // and:                assertFactoryBeanField(builder, "url",                                getDefaultValueFromFeignClientAnnotation("url"));                assertFactoryBeanField(builder, "path",                                getDefaultValueFromFeignClientAnnotation("path"));                assertFactoryBeanField(builder, "decode404",                                getDefaultValueFromFeignClientAnnotation("decode404"));                assertFactoryBeanField(builder, "fallback",                                getDefaultValueFromFeignClientAnnotation("fallback"));                assertFactoryBeanField(builder, "fallbackFactory",                                getDefaultValueFromFeignClientAnnotation("fallbackFactory"));        }        @Test        public void forType_allFieldsSetOnBuilder() {                // when:                final FeignClientBuilder.Builder builder = this.feignClientBuilder                                .forType(FeignClientBuilderTests.class, "TestClient").decode404(true)                                .fallback(Object.class).fallbackFactory(Object.class).path("Path/")                                .url("Url/");                // then:                assertFactoryBeanField(builder, "applicationContext", this.applicationContext);                assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class);                assertFactoryBeanField(builder, "name", "TestClient");                // and:                assertFactoryBeanField(builder, "url", "http://Url/");                assertFactoryBeanField(builder, "path", "/Path");                assertFactoryBeanField(builder, "decode404", true);                assertFactoryBeanField(builder, "fallback", Object.class);                assertFactoryBeanField(builder, "fallbackFactory", Object.class);        }        @Test        public void forType_build() {                // given:                Mockito.when(this.applicationContext.getBean(FeignContext.class))                                .thenThrow(new ClosedFileSystemException()); // throw an unusual exception                                                                                                                                // in the                                                                                                                                // FeignClientFactoryBean                final FeignClientBuilder.Builder builder = this.feignClientBuilder                                .forType(TestClient.class, "TestClient");                // expect: 'the build will fail right after calling build() with the mocked                // unusual exception'                this.thrown.expect(Matchers.isA(ClosedFileSystemException.class));                builder.build();        }}
  • FeignClientBuilderTests验证了safetyCheckForNewFieldsOnTheFeignClientAnnotation、forType_preinitializedBuilder、forType_allFieldsSetOnBuilder、forType_build

小结

FeignClientBuilder提供了forType静态方法用于创建Builder;Builder的构造器创建了FeignClientFactoryBean,其build方法使用FeignClientFactoryBean的getTarget()来创建目标feign client

感谢各位的阅读,以上就是"spring cloud中FeignClientBuilder的用法"的内容了,经过本文的学习后,相信大家对spring cloud中FeignClientBuilder的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

方法 学习 内容 目标 静态 构造器 研究 验证 实例 小结 就是 思路 情况 文章 更多 知识 知识点 篇文章 跟着 问题 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 java服务器相关问题 ARM和喷码机软件开发 优网服务器 电力信息网络安全招标技术要求 试卷保密室智能存储服务器 超市系统的数据库服务器 安全dns服务器作用 现场总线主从站还是服务器客户端 学校国家网络安全周活动总结 软件开发人员如何自我介绍 爱普森15168邮件服务器设置 后台有数据库支持的关系信息系统 网站数据库安全的问题 嵩明正规软件开发网上价格 h3c怎么做到网络安全 舟山新区软件开发定制 龙芯服务器管理员教程 需要数据库软件有哪些内容 服务器管理zaina 甘井子区佰诚精锐软件开发 甲骨云服务器添加root密码 襄阳市委网络安全 网络安全相关的记录片 软件开发需要系统仿真吗 本溪政务软件开发价格 万点网络技术有限公司 纯生存服务器死亡不掉落手机版 福建专业服务器租用云服务器 服务器514 软件开发涉及到的技术汇总
0