基于OpenFeign实现服务调用

我们之前使用ribbon完成负载均衡有什么缺点:  restTemplate---url地址。

①代码可读性比较差

②编码风格和我们习惯的不同,习惯的编码风格service 调用dao  service中注入dao,dao对象调用相应的方法

1、什么是OpenFeign

OpenFeignSpring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样简单, 只需要创建一个接口并添加一个注解即可。

Nacos很好的兼容了Feign Feign负载均衡默认集成了 Ribbon, 所以在Nacos下使用Fegin默认就实现了负载均衡的效果。

2、OpenFeign的使用

2.1 在order微服务中加入Feign的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.2 在主启动类上加开启注解

@SpringBootApplication
@EnableFeignClients //开启OpenFeign注解
public class OrderApp {
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class,args);
    }

    @Bean
    @LoadBalanced  //负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

2.3 创建Feign的接口

@FeignClient用于创建声明是API接口,该接口是RESTful风格的。Feign被设计成插拔式的,可注入其他组件和Feign一起使用。最典型的是如果Ribbon可用,Feign会和Ribbon相结合进行负载均衡。value()和name()一样,是被调用的服务的ServiceId

这里接口的方法参数要和product的controller接口的方法参数一致

@FeignClient(value = "springcloud-product")
public interface ProductFeign {
    @GetMapping("product/getOne/{id}")
    CommonResult getOne(@PathVariable Integer id);
}

2.4 修改controller层

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductFeign productFeign;

    @GetMapping("buy/{pid}/{num}")
    public CommonResult buy(@PathVariable Integer pid,@PathVariable Integer num){
        
        CommonResult result = productFeign.getOne(pid);
        Product product = JSON.parseObject(JSON.toJSONString(result.getData()), Product.class);
        Order order = new Order(2, "饺子皮", product.getPid(), product.getPname(), product.getPprice(), num);
        Integer addOrder = orderService.addOrder(order);
        if (addOrder>0){
            return new CommonResult(2000,"购买成功",addOrder);
        }
        return new CommonResult(5000,"购买失败",null);
    }
}

重启order微服务,查看效果,可以成功购买就说明配置成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在微服务架构中,服务之间需要进行调用,而OpenFeign是一个基于Netflix Feign实现的轻量级HTTP客户端,可以用来简化服务之间的调用。下面是通过OpenFeign实现服务调用的步骤: 1.引入OpenFeign依赖:在pom.xml文件中加入以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2.创建Feign客户端接口:定义一个Java接口,使用注解@FeignClient指定需要调用服务名,以及该服务的具体接口地址。 ```java @FeignClient(name = "service-provider") public interface UserFeignClient { @GetMapping("/user/{id}") User getUserById(@PathVariable("id") Long id); @PostMapping("/user") User addUser(@RequestBody User user); @PutMapping("/user/{id}") User updateUser(@PathVariable("id") Long id, @RequestBody User user); @DeleteMapping("/user/{id}") void deleteUser(@PathVariable("id") Long id); } ``` 3.在应用程序中使用Feign客户端接口:在需要调用服务的地方,注入Feign客户端接口,然后调用接口中定义的方法即可。 ```java @RestController public class UserController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/user/{id}") public User getUserById(@PathVariable Long id) { return userFeignClient.getUserById(id); } @PostMapping("/user") public User addUser(@RequestBody User user) { return userFeignClient.addUser(user); } @PutMapping("/user/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { return userFeignClient.updateUser(id, user); } @DeleteMapping("/user/{id}") public void deleteUser(@PathVariable Long id) { userFeignClient.deleteUser(id); } } ``` 这样,我们就可以通过OpenFeign实现服务调用了。OpenFeign会根据Feign客户端接口中定义的方法,自动构造出HTTP请求,发送给指定的服务,并将响应转换成Java对象返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值