spring cloud微服务-OpenFeign的使用

OpenFeign的使用

openFeign的作用是服务间的远程调用 ,比如通过OpenFeign可以实现调用远程服务。

已经有了LoadBalancer为什么还要用openFeign?

  • 在微服务架构中,LoadBalancer和OpenFeign虽然都提供了服务间调用的能力,但它们的设计目的和使用场景有所不同。
  • LoadBalancer主要关注于服务间的负载均衡,它可以帮助客户端在多个服务实例之间分配请求,以实现高可用性和性能优化。
  • 而OpenFeign则提供了一种声明式的Web服务客户端编程模型,它使得编写服务间调用的代码更加简洁和直观。

OpenFeign到底是什么?

官方文档地址,https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/。

OpenFeign是Spring cloud框架中集成的声明式HTTP客户端工具。

OpenFeign可以让远程调用服务达到像本地调用方法一样的体验。

OpenFeign怎么用?

OpenFeign在服务消费端使用,比如某个服务使用OpenFeign调用其它服务

1.引入依赖,在项目 的pom文件添加:

复制代码
<!-- openfeign 远程调用 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.在项目启动类上添加@EnableFeignClients注解,开启openFeign功能

复制代码
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient			
public class TlmallOrderApplication {

3.编写OpenFeign客户端,调用其它微服务

java 复制代码
@FeignClient(name = "tlmall-storage")
public interface StorageServiceFeignClient {

@PostMapping("/storage/reduce-stock")
    Result<?> reduceStock(@RequestBody StorageDTO productReduceStockDTO);

}


@FeignClient(name = "tlmall-account")
public interface AccountServiceFeignClient {

@PostMapping("/account/reduce-balance")
    Result<?> reduceBalance(@RequestBody AccountDTO accountReduceBalanceDTO);

}

4.服务发起调用 ,像调用本地方式一样调用其它服务

java 复制代码
@Autowired
    private AccountServiceFeignClient accountService;

    @Autowired
    private StorageServiceFeignClient storageService;



public Result<?> createOrder(String userId, String commodityCode, Integer count) {

// deduct storage
        StorageDTO storageDTO = new StorageDTO();
        storageDTO.setCommodityCode(commodityCode);
        storageDTO.setCount(count);
        //RestTemplate远程调用
        //String storage_url = "http://localhost:8010/storage/reduce-stock";
        //整合了Nacos+LoadBalaner,可以使用微服务名tlmall-storage代替localhost:8020
        //String storage_url = "http://tlmall-storage/storage/reduce-stock";
        //Integer storageCode = restTemplate.postForObject(storage_url,storageDTO, Result.class).getCode();
        //openFeign远程调用
        Integer storageCode = storageService.reduceStock(storageDTO).getCode();
        if (storageCode.equals(COMMON_FAILED.getCode())) {
            throw new BusinessException("stock not enough");
        }

        // deduct balance
        int price = count * 2;
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(userId);
        accountDTO.setPrice(price);
        //RestTemplate远程调用
        //String account_url = "http://localhost:8020/account/reduce-balance";
        //整合了Nacos+LoadBalaner,可以使用微服务名tlmall-account代替localhost:8020
        //String account_url = "http://tlmall-account/account/reduce-balance";
        //Integer accountCode = restTemplate.postForObject(account_url, accountDTO, Result.class).getCode();
        //openFeign远程调用
        Integer accountCode = accountService.reduceBalance(accountDTO).getCode();
        if (accountCode.equals(COMMON_FAILED.getCode())) {
            throw new BusinessException("balance not enough");
        }
}

重启服务,验证

1.到Nacos控制台查看服务是否注册成功

2.进行测试访问url

3.借助idea工具,复制一个服务,再次操作,看当有2个服务时,能否把2个都调用了

相关推荐
草莓熊Lotso14 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
wdyan2975 天前
带着小白学习-微服务-5 实战:配置中心-数据库连接
数据库·学习·微服务
胡写代码6 天前
把数据库连接搬进配置中心,服务起不来:spring.config.import 的 4 个必填项
spring cloud·微服务
利威尔xu9 天前
Go IM 源码实战(14):本地缓存快 3000 倍,失效却难 10 倍
后端·缓存·微服务·面试·golang
利威尔xu9 天前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径
redis·后端·微服务·面试·golang
骑着蜗牛撵大象32710 天前
Spring Boot 4.1 + Spring AI 2.0:RAG 供文、图谱供关系、MCP 供工具的 Java Agent 平台
java·spring boot·spring·微服务·知识图谱·rag·mcp
code_slave(码畜)10 天前
微服务架构落地:慢 SQL 治理与连接池调优 —— 守住数据库的最后一道防线
数据库·spring boot·sql·spring cloud·微服务·架构
微三云 - 廖会灵 (私域系统开发)11 天前
企业级 SaaS 系统架构设计:单体、微服务还是模块化单体?技术选型实战
微服务·架构·系统架构
shuair12 天前
Spring Cloud 微服务脚手架:Simple Cloud 系统架构设计
spring cloud·微服务·系统架构
CodeGL12 天前
微服务框架——go-zero
微服务·架构·golang