欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Cloud?OpenFeign模版化客戶(hù)端搭建過(guò)程

 更新時(shí)間:2022年06月24日 14:12:59   作者:、楽.  
OpenFeign是一個(gè)顯示聲明式的WebService客戶(hù)端。使用OpenFeign能讓編寫(xiě)Web Service客戶(hù)端更加簡(jiǎn)單,這篇文章主要介紹了Spring?Cloud?OpenFeign模版化客戶(hù)端,需要的朋友可以參考下

OpenFeign是什么?

OpenFeign是一個(gè)顯示聲明式的WebService客戶(hù)端。使用OpenFeign能讓編寫(xiě)Web Service客戶(hù)端更加簡(jiǎn)單。使用時(shí)只需定義服務(wù)接口,然后在上面添加注解。OpenFeign也支持可拔插式的編碼和解碼器。spring cloud對(duì)feign進(jìn)行了封裝,使其支持MVC注解和HttpMessageConverts。和eureka(服務(wù)注冊(cè)中心)和ribbon組合可以實(shí)現(xiàn)負(fù)載均衡。在Spring Cloud中使用OpenFeign,可以做到使用HTTP請(qǐng)求訪問(wèn)遠(yuǎn)程服務(wù),就像調(diào)用本地方法一樣的,開(kāi)發(fā)者完全感知不到這是在調(diào)用遠(yuǎn)程方法,更感知不到在訪問(wèn)HTTP請(qǐng)求,非常的方便。

OpenFeign能干啥?

  • OpenFeign的設(shè)計(jì)宗旨式簡(jiǎn)化Java Http客戶(hù)端的開(kāi)發(fā)。Feign在restTemplate的基礎(chǔ)上做了進(jìn)一步的封裝,由其來(lái)幫助我們定義和實(shí)現(xiàn)依賴(lài)服務(wù)接口的定義。在OpenFeign的協(xié)助下,我們只需創(chuàng)建一個(gè)接口并使用注解的方式進(jìn)行配置(類(lèi)似于Dao接口上面的Mapper注解)即可完成對(duì)服務(wù)提供方的接口綁定,大大簡(jiǎn)化了Spring cloud Ribbon的開(kāi)發(fā),自動(dòng)封裝服務(wù)調(diào)用客戶(hù)端的開(kāi)發(fā)量。
  • OpenFeign集成了Ribbon,利用ribbon維護(hù)了服務(wù)列表,并且通過(guò)ribbon實(shí)現(xiàn)了客戶(hù)端的負(fù)載均衡。與ribbon不同的是,通過(guò)OpenFeign只需要定義服務(wù)綁定接口且以申明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用。

OpenFeign使用

使用OpenFeign之前,我們首先將之前的工程還原,為了操作簡(jiǎn)單,我們只采用單節(jié)點(diǎn)的eureka。

API服務(wù)模塊搭建

由于我們使用openfeign之后,需要暴露相關(guān)接口給外部服務(wù),所以我們需要寫(xiě)一個(gè)api服務(wù)。

我這里為了方便所以直接在外部創(chuàng)建了一個(gè)ms-service-api服務(wù),實(shí)際開(kāi)發(fā)過(guò)程中我們基本都是將其寫(xiě)在對(duì)應(yīng)的模塊中。

整體模塊創(chuàng)建完成后,我們便可以定義相關(guān)接口供外部調(diào)用了,每個(gè)服務(wù)一一對(duì)應(yīng)即可。

這里只演示一個(gè)goods-service-api項(xiàng)目的搭建,項(xiàng)目代碼在文末會(huì)給出。

引入依賴(lài)

引入 open feign的相關(guān)依賴(lài)。

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

創(chuàng)建接口

這里對(duì)應(yīng)的是goods-service項(xiàng)目中的GoodsService類(lèi),這里定義了其中的方法。

public interface IGoodsService {
    @GetMapping("/goods")
    String getGoodsById();
}

這個(gè)時(shí)候還需要將goods-service項(xiàng)目的類(lèi)繼承該接口:

首先需要將我們創(chuàng)建的api服務(wù)依賴(lài)添加到項(xiàng)目中:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>goods-service-api</artifactId>
</dependency>

然后開(kāi)始修改:

@Slf4j
@RestController
public class GoodsService implements IGoodsService  {

    @Value("${server.port}")
    private String port;

    /**
     * 根據(jù)ID查詢(xún)商品信息
     *
     * @return
     */
    @GetMapping("/goods")
    public String getGoodsById() {
        log.info("收到請(qǐng)求,端口為:{}", port);
        return "返回商品信息";
    }
}

這里為了區(qū)分職責(zé),類(lèi)似我們寫(xiě)mapper和service這種,又額外寫(xiě)了一個(gè)對(duì)外暴露的接口。

FeignClient中的name不能隨意寫(xiě),它對(duì)應(yīng)各個(gè)服務(wù)在eureka中注冊(cè)的名字。如果不寫(xiě)該接口的話,可以將該注解加在IGoodsService上。

@FeignClient(name = "goods-service")
public interface IGoodsServiceFeignClient extends IGoodsService {
}

以上便是一個(gè)api服務(wù)的搭建過(guò)程。

外部調(diào)用

當(dāng)我們的API服務(wù)調(diào)用完成之后,如何在聚合服務(wù)中調(diào)用呢?

首先我們需要在聚合服務(wù)中引入openfeign以及各api服務(wù)的依賴(lài):

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

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>goods-service-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>marking-service-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>order-service-api</artifactId>
        </dependency>

引入相關(guān)依賴(lài)后,我們就可以不需要之前的代碼了,全面采用面向接口的形式來(lái)開(kāi)發(fā)。

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

    // 代理對(duì)象
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IPromotionService promotionService;

    @Autowired
    private IOrderService orderService;

    @GetMapping
    public String order() {
        log.info("begin do order");
//        使用openfiegn
        String goods = goodsService.getGoodsById();
        String promotion = promotionService.getPromotionById();
        String result = orderService.createOrder(goods, promotion);
        return result;
    }
}

這個(gè)時(shí)候還沒(méi)有完全結(jié)束,我們還需要在啟動(dòng)類(lèi)上配置掃碼相關(guān)的feign類(lèi)。

@EnableFeignClients(basePackages = "com.example.feignclient")
@SpringBootApplication
public class MallProtalApplication {

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

以上便是我們集成openfeign的全部步驟。

項(xiàng)目代碼

cloud-demo

OpenFeign相關(guān)特性

  • Gzip壓縮
  • Looger
  • 底層通信框架:(sun.net.www.protocol.http.HttpURLConnection)
  • 也可以自定義替換為OKHttp

Logger 日志使用

使用OpenFeign的日志功能,我們需要進(jìn)行如下幾個(gè)操作:

創(chuàng)建配置類(lèi)

@Configuration
public class FeignClientLogConfiguration {

    /**
     * NONE
     * BASIC
     * HEAD
     * FULL
     * @return
     */
    @Bean
    Logger.Level feignLogger(){
        return Logger.Level.FULL;
    }
}

指定配置類(lèi)

@FeignClient(name = "goods-service",configuration = FeignClientLogConfiguration.class)
public interface IGoodsServiceFeignClient extends IGoodsService {

}

最后還需要在聚合服務(wù)出添加相關(guān)日志配置

feignclient所在的路徑

# openfeign 日志
logging.level.[com.example.feignclient]=DEBUG

使用OKHttp通信

引入依賴(lài)

<!--        ophttp-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>

修改配置

# openfeign替換底層通信框架
feign.httpclient.enabled=false
feign.okhttp.enabled=true

到此這篇關(guān)于Spring Cloud OpenFeign模版化客戶(hù)端的文章就介紹到這了,更多相關(guān)Spring Cloud OpenFeign客戶(hù)端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論