基于OpenFeign实现服务调用

1 什么是OpenFeign   

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

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

2 feign的使用

1.加入feign依赖

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

2 在主启动类上加入开启feign的注解

package com.cyy.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud-parent
 * @description: 主启动类
 * @author: 崔艺耀
 * @create: 2021-07-06 20:25
 **/
@SpringBootApplication
@EnableDiscoveryClient//开启nacos注解
@EnableFeignClients//开启feign注解
@MapperScan(basePackages = "com.cyy.order.mapper")
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(com.cyy.order.OrderApplication.class,args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

3 创建feign的接口

package com.cyy.order.feign;

import com.cyy.coment.entry.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "product")
public interface ProductFeign {
    @GetMapping("/product/FindById/{pid}")
    public Product selectById(@PathVariable Integer pid);

}

4 修改OrderController的代码

package com.cyy.order.controller;

import com.cyy.coment.entry.Order;
import com.cyy.coment.entry.Product;
import com.cyy.order.feign.ProductFeign;
import com.cyy.order.sevice.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: springcloud-parent
 * @description: 订单的控制层
 * @author: 崔艺耀
 * @create: 2021-07-06 19:50
 **/
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductFeign productFeign;

    @GetMapping("saveOrder")
    public String saveOrder(@RequestParam Integer pid,Integer num){
        Order order=new Order();
        order.setNumber(num);
        order.setUid(2);
        order.setUsername("张三");
        order.setPid(pid);

        Product product = productFeign.selectById(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        orderService.saveOrder(order);
        return "下单成功";
    }
}

5 重启order微服务,查看效果

在微服务架构中,服务之间需要进行调用,而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、付费专栏及课程。

余额充值