OpenFeign(1)--->使用OpenFeign

1、在前面我们讲解了Ribbon,我们使用ribbon的方式主要是自己是同RestTemplate 来进行远程请求的调用,代码如下:

    @GetMapping("userGetOrderById")
    public OrderDto userGetOrderById(@RequestParam("orderId") String orderId){

        //使用具备负载均衡功能的RestTemplate进行http服务调用
        ResponseEntity<OrderDto> forEntity = restTemplate.getForEntity("http://order-service/getOrderById?orderId=" + orderId, OrderDto.class);
        return forEntity.getBody();
    }

     这种方式在小项目中我们这样写确实也没啥问题,但是如果我们的接口特别多,这种方式就比较鸡肋了,因此spring cloud 为我们提供了OpenFeign来解决这个问题,接下来我们来看看如何使用openFeign?

 

2、使用OpenFeign

       2.1、项目结构:

                 

 

        2.2、order-api 项目信息:

                  pom.xml文件:

    <parent>
        <artifactId>spring-cloud-openfeign</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-api</artifactId>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

                定义的FeignClient : 

      @FeignClient("order-service")
      @RequestMapping("order")
      public interface OrderFeignClient {

          @PostMapping("save")
          boolean saveOrder(@RequestBody OrderDto orderDto);

          @GetMapping("findAll")
          Collection <OrderDto> findAll();
       }

    

         2.3、order-service 项目信息:

               pom.xml文件:

    <parent>
        <artifactId>spring-cloud-openfeign</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.wzy</groupId>
            <artifactId>order-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

         OrderController提供服务:

@RestController
@RequestMapping("order")
public class OrderController {

    public static  final Map<String,OrderDto> datas = new ConcurrentHashMap<>();

    @PostMapping("save")
    public boolean saveOrder(@RequestBody OrderDto orderDto){
        datas.put(orderDto.getId(), orderDto);
        return true;
    }

    @GetMapping("findAll")
    public Collection<OrderDto> findAll(){
        try {
            Thread.sleep(800L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return datas.values();
    }
}

      启动类:    

@SpringBootApplication
@EnableDiscoveryClient
public class OrderService {

    public static void main(String[] args) {
        SpringApplication.run(OrderService.class, args);
    }

}

 

       配置文件application.properties:

server.port=7070
spring.application.name=order-service

eureka.client.service-url.defaultZone=http://localhost:9090/eureka
eureka.client.registry-fetch-interval-seconds=10

     2.4、user-service 项目信息:

              pom.xml文件:

   <parent>
        <artifactId>spring-cloud-openfeign</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.wzy</groupId>
            <artifactId>order-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

           调用order-service 的方式:

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @PostMapping("saverOrder")
    public boolean saveOrder(@RequestBody OrderDto orderDto){
        return orderFeignClient.saveOrder(orderDto);
    }

    @GetMapping("getAllOrder")
    public Collection <OrderDto> getAllOrder(){
        return orderFeignClient.findAll();
    }
}

           启动类:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(clients = {OrderFeignClient.class})
public class UserService {

    public static void main(String[] args) {
        SpringApplication.run(UserService.class, args);
    }
}

 

       配置文件application.properties:

spring.application.name=user-service
server.port=8080
eureka.client.service-url.defaultZone=http://localhost:9090/eureka
eureka.client.instance-info-replication-interval-seconds=10

#设置feign的请求超时时间为1秒
feign.client.config.default.read-timeout=1000
ribbon.ReadTimeout=1000

 

以上就是使用OpenFeign的代码,使用还是很简单的,接下来的文章我们将剖析其原理。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值