最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud Ribbon負(fù)載均衡代碼實例

 更新時間:2020年03月13日 11:46:13   作者:玉天恒  
這篇文章主要介紹了SpringCloud Ribbon負(fù)載均衡代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1.添加依賴

<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>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

<!-- lombok -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>

2.修改啟動類

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

@SpringBootApplication
@MapperScan("cn.ytheng.order_service")
public class OrderServiceApplication {

  /**
   * @Loadbalanced負(fù)載均衡策略
   */
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {

    SpringApplication.run(OrderServiceApplication.class, args);
  }

}

3.添加Controller

import cn.theng.order_service.utils.RibbonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
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.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/order")
public class ProductOrderController {

  @RequestMapping("/test")
  public Object test(@RequestParam("product_id") int productId) {

    //方法一
//    ServiceInstance instance = loadBalancerClient.choose("product-service");
//    String url = String.format("http://%s:%s/api/v1/product/find?id=" + productId, instance.getHost(), instance.getPort());
//    RestTemplate template = new RestTemplate();
//    Map<String, Object> map2 = template.getForObject(url, Map.class);

    //負(fù)載均衡
    //商品列表啟用兩個節(jié)點時
    //由客戶端來自動選擇節(jié)點,可能是8771端口,也有可能是8772端口
    //參數(shù)id名稱需要保持一致
    //方法二(推薦)
    String uri = "http://product-service/api/v1/product/find?id={id}";
    Map<String, Object> request = new HashMap<>();
    request.put("id", productId);
    Map<String, Object> map3 = RibbonUtils.get(uri, Map.class, request);

    return "success";
  }

  @PostMapping("/test2")
  public Object test2(@RequestParam("product_id") int productId) {

    Product product = new Product();
    product.setId(productId);

    String uri = "http://product-service/api/v1/product/find2";
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("token", "theng");
    Object result = RibbonUtils.post(uri, Object.class, product, headers);

    return "success";
  }
}

4.添加Ribbon調(diào)用公共類

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

@Component
public class RibbonUtils {

  @Autowired
  private RestTemplate restTemplate;

  private static RestTemplate template;

  //@PostConstruct修飾的方法會在服務(wù)器加載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次
  @PostConstruct
  public void init() {
    template = restTemplate;
  }

  /**
   *
   * @param uri 接口地址
   * @param responseType 返回類型
   *
   * */
  public static <T> T get(String uri, Class<T> responseType) {

    return template.getForObject(uri, responseType);
  }

  /**
   *
   * @param uri 接口地址
   * @param responseType 返回類型
   * @param request 傳遞參數(shù)
   *
   * */
  public static <T> T get(String uri, Class<T> responseType, Map<String, ?> request) {

    return template.getForObject(uri, responseType, request);
  }

  /**
   *
   * @param uri 接口地址
   * @param responseType 返回類型
   * @param request 傳遞參數(shù)
   * @param headerMap 報頭信息
   *
   * */
  public static <T> T get(String uri, Class<T> responseType, Map<String, ?> request, Map<String, String> headerMap) {

    //添加報頭
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    for(Map.Entry<String, String> entry : headerMap.entrySet()){
      String mapKey = entry.getKey();
      String mapValue = entry.getValue();
      headers.add(mapKey, mapValue);
    }

    //body的類型定為String,這里使用get沒有用到body,post會使用到
    HttpEntity<String> entity = new HttpEntity<String>(null, headers);

    ResponseEntity<T> result = template.exchange(uri, HttpMethod.GET, entity, responseType, request);

    return result.getBody();
  }

  /**
   *
   * @param uri 接口地址
   * @param responseType 返回類型
   * @param body 傳遞實體
   * @param headers 報頭信息
   *
   * */
  public static <T> T post(String uri, Class<T> responseType, Object body, LinkedMultiValueMap<String, String> headers) {

    if (!headers.containsKey("Content-Type")) {
      headers.put("Content-Type", Collections.singletonList("application/json;charset=UTF-8"));
    }

    HttpEntity request = new HttpEntity(body, headers);
    Object obj = template.postForObject(uri, request, responseType);
    return (T) obj;
  }
}

5.在PostMan上測試兩個接口即可

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?IOC?xml方式進(jìn)行工廠Bean操作詳解

    Spring?IOC?xml方式進(jìn)行工廠Bean操作詳解

    這篇文章主要介紹了Spring?IOC?xml方式進(jìn)行工廠Bean操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • Java通過Fork/Join優(yōu)化并行計算

    Java通過Fork/Join優(yōu)化并行計算

    這篇文章主要為大家詳細(xì)介紹了Java通過Fork、Join來優(yōu)化并行計算,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SpringBoot集成Kafka 配置工具類的詳細(xì)代碼

    SpringBoot集成Kafka 配置工具類的詳細(xì)代碼

    spring-kafka 是基于 java版的 kafka client與spring的集成,提供了 KafkaTemplate,封裝了各種方法,方便操作,它封裝了apache的kafka-client,不需要再導(dǎo)入client依賴,這篇文章主要介紹了SpringBoot集成Kafka 配置工具類,需要的朋友可以參考下
    2022-09-09
  • SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

    SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

    這篇文章主要介紹了SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • java打印國際象棋棋盤的方法

    java打印國際象棋棋盤的方法

    這篇文章主要為大家詳細(xì)介紹了java打印出國際象棋棋盤的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 詳解Maven profile配置管理及激活profile的幾種方式

    詳解Maven profile配置管理及激活profile的幾種方式

    這篇文章主要介紹了詳解Maven profile配置管理及激活profile的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 使用IDEA畫UML圖的詳細(xì)步驟

    使用IDEA畫UML圖的詳細(xì)步驟

    UML是面向?qū)ο笤O(shè)計的建模工具,獨立于任何具體程序設(shè)計語言,是一種為面向?qū)ο笙到y(tǒng)的產(chǎn)品進(jìn)行說明、可視化和編制文檔的一種標(biāo)準(zhǔn)語言,本文重點給大家介紹使用IDEA畫UML圖的詳細(xì)步驟,需要的朋友參考下吧
    2021-06-06
  • 手把手教你在eclipse創(chuàng)建第一個java?web項目并運行

    手把手教你在eclipse創(chuàng)建第一個java?web項目并運行

    Eclipse是用來做開發(fā)的自由集成開發(fā)環(huán)境,這也是很多java程序員會使用的開發(fā)環(huán)境,所以可以使用eclipse創(chuàng)建javaweb項目,下面這篇文章主要給大家介紹了關(guān)于如何在eclipse創(chuàng)建第一個java?web項目并運行的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • Intellij IDEA中啟動多個微服務(wù)(開啟Run Dashboard管理)

    Intellij IDEA中啟動多個微服務(wù)(開啟Run Dashboard管理)

    這篇文章主要介紹了Intellij IDEA中啟動多個微服務(wù)(開啟Run Dashboard管理),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Mybatis中的延遲加載,以及原理分析

    Mybatis中的延遲加載,以及原理分析

    這篇文章主要介紹了Mybatis中的延遲加載以及原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04

最新評論

河南省| 汾阳市| 大邑县| 莒南县| 读书| 靖边县| 新营市| 达拉特旗| 绩溪县| 抚松县| 泗洪县| 昭通市| 龙南县| 白银市| 抚宁县| 滕州市| 本溪市| 昌吉市| 连州市| 山西省| 贵港市| 台中市| 霞浦县| 定西市| 芷江| 隆回县| 田阳县| 白玉县| 元谋县| 承德市| 墨江| 正蓝旗| 讷河市| 阿拉尔市| 塘沽区| 靖宇县| 承德县| 噶尔县| 桓台县| 二连浩特市| 百色市|