亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringCloud Ribbon負載均衡代碼實例

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

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負載均衡策略
   */
  @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);

    //負載均衡
    //商品列表啟用兩個節(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調用公共類

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修飾的方法會在服務器加載Servlet的時候運行,并且只會被服務器調用一次
  @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上測試兩個接口即可

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 淺談redis key值內存消耗以及性能影響

    淺談redis key值內存消耗以及性能影響

    這篇文章主要介紹了淺談redis key值內存消耗以及性能影響,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中的HttpServletRequest接口詳細解讀

    Java中的HttpServletRequest接口詳細解讀

    這篇文章主要介紹了Java中的HttpServletRequest接口詳細解讀,是一個接口,全限定名稱為Jakarta.Serclet.http.HttpServletRequest
    HttpServletRequest接口是Servlet規(guī)范的一員,需要的朋友可以參考下
    2023-11-11
  • 解決IDEA創(chuàng)建第一個spring boot項目提示cannot resolve xxx等錯誤

    解決IDEA創(chuàng)建第一個spring boot項目提示cannot resolve xxx等

    這篇文章主要介紹了解決IDEA創(chuàng)建第一個spring boot項目提示cannot resolve xxx等錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 淺談Java BitSet使用場景和代碼示例

    淺談Java BitSet使用場景和代碼示例

    這篇文章主要介紹了淺談Java BitSet使用場景和代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 基于Java實現(xiàn)雙向鏈表

    基于Java實現(xiàn)雙向鏈表

    這篇文章主要為大家詳細介紹了基于Java實現(xiàn)雙向鏈表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java?詳細講解線程的狀態(tài)及部分常用方法

    Java?詳細講解線程的狀態(tài)及部分常用方法

    在Java程序中,一個線程對象只能調用一次start()方法啟動新線程,并在新線程中執(zhí)行run()方法。一旦run()方法執(zhí)行完畢,線程就結束了,本篇來講解Java線程的狀態(tài)以及部分常用方法
    2022-04-04
  • Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼

    Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼

    這篇文章主要介紹了Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • SpringBoot快速集成Logback日志組件

    SpringBoot快速集成Logback日志組件

    本文主要介紹了SpringBoot快速集成Logback日志組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • java 多態(tài)性詳解及簡單實例

    java 多態(tài)性詳解及簡單實例

    這篇文章主要介紹了java 多態(tài)性詳解及簡單實例的相關資料,需要的朋友可以參考下
    2017-02-02
  • GraalVM?native-image編譯后quarkus的超音速啟動

    GraalVM?native-image編譯后quarkus的超音速啟動

    這篇文章主要介紹了經(jīng)過GraalVM?native-image編譯后的quarkus,來帶大家驗證一下號稱超音速亞原子的quarkus是否名副其實,有需要的朋友可以借鑒參考下,希望能夠有所包幫助
    2022-02-02

最新評論