黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SpringCloud項目實戰:Feign負載均衡

SpringCloud項目實戰:Feign負載均衡

更新時間:2021-09-29 08:44:28 來源:動力節點 瀏覽1650次

Feign可以做什么

Feign 旨在使編寫 Java Http 客戶端更容易。在使用Ribbon+RestTemplate時,我們使用RestTemplate對http請求進行封裝,形成一組模板化的調用方法。但是在實際開發中,由于依賴服務的調用可能不止一個,而且一個接口往往會在多個地方被調用,所以通常每個微服務都會封裝一些客戶端類來封裝這些依賴的服務調用。所以Feign在這個基礎上做了進一步的封裝,他會幫我們定義和實現依賴服務接口的定義。在Feign的實現下,我們只需要創建一個接口,并使用注解進行配置即可(之前Dao接口用Mapper注解標注,現在是微服務接口,標注了Feign注解),和服務提供者的接口綁定,簡化了使用Spring cloud Ribbon時自動封裝服務調用客戶端的開發量。Feign集成了Ribbon,使用Ribbon維護MicroServiceCloud-Dept的服務列表信息,通過輪詢實現客戶端負載均衡。與 Ribbon 不同的是,feign 只需要定義服務綁定接口,并使用聲明式方法來優雅簡單地實現服務調用。

Feign 步驟

1.參考80個微服務搭建microservicecloud-consumer-dept-feign

(1) 與80相比,pom.xml文件增加了feign的依賴

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

(2)application.yml復制80

(3)新建一個主啟動類DeptConsumer80_Feign_App.java,并添加如下注解

package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptConsumer80_Feign_App.class, args);
  }
}

2.修改微服務microservicecloud-api

(1)在pom.xml文件中添加feign依賴

<依賴>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</依賴>

(2)新增接口DeptClientService.java

package com.atguigu.springcloud.service;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.atguigu.springcloud.entities.Dept;
@FeignClient(value = "MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
    public Dept get(@PathVariable("id") long id);     
    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
    public List<Dept> list();     
    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
    public boolean add(Dept dept);
}

(3)執行clean install構建jar包

3.微服務microservicecloud-consumer-dept-feign新建一個DeptController_Feign.java

package com.atguigu.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptClientService;
@RestController
public class DeptController_Feign
{
  @Autowired
  private DeptClientService service = null;
  @RequestMapping(value = "/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id)
  {
   return this.service.get(id);
  }
  @RequestMapping(value = "/consumer/dept/list")
  public List<Dept> list()
  {
   return this.service.list();
  }
  @RequestMapping(value = "/consumer/dept/add")
  public Object add(Dept dept)
  {
   return this.service.add(dept);
  }
}

4.啟動eureka,3個微服務實例,啟動Feign,輸入http://localhost/consumer/dept/list,測試成功

Feign通過接口方法調用Rest服務(以前是Ribbon+RestTemplate),將請求發送到Eureka服務器(http://MICROSERVICECLOUD-DEPT/dept/list),通過Feign直接找到服務接口,因為當服務被稱為 Incorporating Ribbon 技術時,它也支持負載均衡。

大家想了解更多相關知識,可以關注一下動力節點的SpringCloud教程,里面的內容更加詳細,希望對大家能夠有所幫。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 2020国产精品亚洲综合网 | 成年人在线免费观看网站 | 久久综合免费 | 免费人成网站在线高清 | 日本天堂网在线观看 | 日韩中文字幕第一页 | 色狠狠色综合久久8狠狠色 色狠狠成人综合网 | 免费毛片看 | 日韩欧美在线观看视频 | 一级特黄性色生活片一区二区 | 露脸超嫩97后在线播放 | 久草亚洲视频 | 999久久久免费精品国产牛牛 | www.爱色.com| 青青草国产免费久久久91 | 最近免费中文字幕视频高清在线看 | 国产成人精品日本亚洲11 | 久久精品成人国产午夜 | 色片在线看 | 成人亚洲国产综合精品91 | 香港三级日本三级三级人妇 | 天天插天天摸 | 人人干夜夜操 | 午夜欧美性欧美 | 亚洲丶国产丶欧美一区二区三区 | 国产黄色在线免费观看 | 亚洲专区第一页 | 一级免费黄色 | 国产 日韩 欧美 亚洲 | 永久免费看毛片 | 日韩在线二区 | 国产成人精品视频频 | 黄色毛片视频网站 | 2015日韩永久免费视频播放 | 在线免费日本 | 日日干天天干 | 日本伊人精品一区二区三区 | 欧美黑人三级 | 日韩伦理亚洲欧美在线一区 | 国产农村精品一级毛片视频 | 三级福利视频 |