栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

脱离spring cloud使用feign

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

脱离spring cloud使用feign

优雅的http接口调用-feign。
spring-cloud-feign是spring cloud微服务之间调用封装的功能,由于feign的封装和解耦做的比较好,因此脱离spring cloud也能使用。

Spring boot项目使用feign

项目背景:spring boot 2.2.5.RELEASE

引入Maven

            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.5.RELEASE
        

启动注解

和spring cloud项目一样,同样需要在启动类上添加注解

@EnableFeignClients

Feign接口

接下来直接写feign的接口就行了,和产品spring cloud项目相比,这里的服务地址不再是服务名,而换成了具体的url

@FeignClient(url = "${ca.url}", name = "caClient")
public interface CaClient {

    @PostMapping(value = "ca", headers = "TOKEN=${ca.token}")
    Result ca(@RequestBody CaDto dto);
    
}

测试验证
@Slf4j
@SpringBootTest(classes = CaApplication.class)
@RunWith(SpringRunner.class)
public class CaClientTest {
    @Autowired
    private CaClient  caClient;

    @Test
    public void testOpenApiPartner() {
        Result result = caClient.ca(CaDto
                .builder()
                .partnerId("123")
                .type("platform")
                .build());
        log.info(JsonUtils.beanToString(result));
        Assert.isTrue(result != null, "返回空");
        Assert.isTrue(result.isSuccess(), "返回false");
        Assert.isTrue(result.getCode() == 200, "返回错误");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/319160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号