赶知识网

SpringCloud(四)-手把手教你使用OpenFeign

php/mysql/apache 2022-06-06发布 694次点击

1. OpenFeign简介

OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。


2. 配置OpenFeign

2.1 配置pom.xml

找到服务消费者8200项目,修改pom.xml添加依赖


  <!--openfeign -->

      <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

2.2 主启动类加上注解@EnableFeignClients注解

@EnableFeignClients申明该项目是Feign客户端,扫描对应的feign client。


package com.elio.springcloud;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,

        HibernateJpaAutoConfiguration.class})

@EnableDiscoveryClient

@EnableFeignClients

public class ProductConsumer8200 {


    public static void main(String[] args){

        SpringApplication.run(ProductConsumer8200.class, args);

    }

}


2.3 新增提供者API接口

我们需要集中化管理API,就可以通过接口统一管理,需要新增商品服务的接口类ProductService,并添加@FeignClient(name="springcloud-product-provider")注解,其中name就是我们要访问的微服务的名称。比如getServiceInfo方法中@GetMapping("product/provider/get/info")和服务提供者8100的接口路径是一样的。



package com.elio.springcloud.service;


import com.elio.springcloud.dto.Result;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;


@Component

@FeignClient(name="springcloud-product-provider")

public interface ProductService {


    /**

     * 查询

     * @return

     */

    @GetMapping("product/provider/get/info")

    public Result getServiceInfo();


    /**

     * 查询

     * @param id

     * @return

     */

    @GetMapping("product/provider/get/{id}")

    public Result selectById(@PathVariable("id") Long id);

}


2.4 修改消费者Controller

现在接口已经封装在了ProductService里面了,因此在消费者Controller,我们不用再通过微服务名加地址去访问了,修改后的Controller如下


package com.elio.springcloud.controller;


import com.elio.springcloud.dto.Result;

import com.elio.springcloud.service.ProductService;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;


import javax.annotation.Resource;


@RestController

public class ProductConsumerController {


    @Resource

    RestTemplate restTemplate;


    @Resource

    ProductService productService;


    //public static String url = "http://localhost:8100/";

    public static String url = "http://springcloud-product-provider/";


    /**

     * 查询

     * @return

     */

    @GetMapping("product/consumer/get/info")

    public Result getServiceInfo(){

        return productService.getServiceInfo();

        /*return new Result(200, "查询成功",

                restTemplate.getForObject(url+"product/provider/get/info", Result.class));*/

    }


    /**

     * 查询

     * @param id

     * @return

     */

    @GetMapping("product/consumer/get/{id}")

    public Result selectById(@PathVariable("id") Long id){

        return productService.selectById(id);

       /* return new Result(200, "查询成功",

                restTemplate.getForObject(url+"product/provider/get/"+id, Result.class));*/

    }


}


2.5 测试

现在我们已经配置好了消费端通过OpenFeign调用远程接口,接下来就是重启消费端服务8200,重启后,浏览器访问消费者API


访问的8100服务

访问的8101服务

3. 总结

本文简单介绍了如何通过OpenFeign访问提供者的服务,不仅有效的管理了API,还集成了Ribbon实现了负载均衡的功能。现在的项目貌似已经很完美了, 既可以是实现服务注册也可以实现服务发现,还可以实现负载均衡功能,但是假如服务提供者返回一个错误的话,那么消费者将直接在浏览器展示错误页面,这是不合理的,这个问题我们将在下一篇文章中解决。


作者:Renaissance_

链接:https://www.jianshu.com/p/f083660c65bf

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。1. OpenFeign简介

OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。


2. 配置OpenFeign

2.1 配置pom.xml

找到服务消费者8200项目,修改pom.xml添加依赖


  <!--openfeign -->

      <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

2.2 主启动类加上注解@EnableFeignClients注解

@EnableFeignClients申明该项目是Feign客户端,扫描对应的feign client。


package com.elio.springcloud;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,

        HibernateJpaAutoConfiguration.class})

@EnableDiscoveryClient

@EnableFeignClients

public class ProductConsumer8200 {


    public static void main(String[] args){

        SpringApplication.run(ProductConsumer8200.class, args);

    }

}


2.3 新增提供者API接口

我们需要集中化管理API,就可以通过接口统一管理,需要新增商品服务的接口类ProductService,并添加@FeignClient(name="springcloud-product-provider")注解,其中name就是我们要访问的微服务的名称。比如getServiceInfo方法中@GetMapping("product/provider/get/info")和服务提供者8100的接口路径是一样的。



package com.elio.springcloud.service;


import com.elio.springcloud.dto.Result;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;


@Component

@FeignClient(name="springcloud-product-provider")

public interface ProductService {


    /**

     * 查询

     * @return

     */

    @GetMapping("product/provider/get/info")

    public Result getServiceInfo();


    /**

     * 查询

     * @param id

     * @return

     */

    @GetMapping("product/provider/get/{id}")

    public Result selectById(@PathVariable("id") Long id);

}


2.4 修改消费者Controller

现在接口已经封装在了ProductService里面了,因此在消费者Controller,我们不用再通过微服务名加地址去访问了,修改后的Controller如下


package com.elio.springcloud.controller;


import com.elio.springcloud.dto.Result;

import com.elio.springcloud.service.ProductService;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;


import javax.annotation.Resource;


@RestController

public class ProductConsumerController {


    @Resource

    RestTemplate restTemplate;


    @Resource

    ProductService productService;


    //public static String url = "http://localhost:8100/";

    public static String url = "http://springcloud-product-provider/";


    /**

     * 查询

     * @return

     */

    @GetMapping("product/consumer/get/info")

    public Result getServiceInfo(){

        return productService.getServiceInfo();

        /*return new Result(200, "查询成功",

                restTemplate.getForObject(url+"product/provider/get/info", Result.class));*/

    }


    /**

     * 查询

     * @param id

     * @return

     */

    @GetMapping("product/consumer/get/{id}")

    public Result selectById(@PathVariable("id") Long id){

        return productService.selectById(id);

       /* return new Result(200, "查询成功",

                restTemplate.getForObject(url+"product/provider/get/"+id, Result.class));*/

    }


}


2.5 测试

现在我们已经配置好了消费端通过OpenFeign调用远程接口,接下来就是重启消费端服务8200,重启后,浏览器访问消费者API


访问的8100服务

访问的8101服务

3. 总结

本文简单介绍了如何通过OpenFeign访问提供者的服务,不仅有效的管理了API,还集成了Ribbon实现了负载均衡的功能。现在的项目貌似已经很完美了, 既可以是实现服务注册也可以实现服务发现,还可以实现负载均衡功能,但是假如服务提供者返回一个错误的话,那么消费者将直接在浏览器展示错误页面,这是不合理的,这个问题我们将在下一篇文章中解决。


作者:Renaissance_

链接:https://www.jianshu.com/p/f083660c65bf

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


Top10

沪ICP备09053415号 © 赶知识网