国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringCloud Ribbon負載均衡代碼實例

瀏覽:40日期:2023-09-16 14:28:47

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); //負載均衡 //商品列表啟用兩個節點時 //由客戶端來自動選擇節點,可能是8771端口,也有可能是8772端口 //參數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;@Componentpublic 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 傳遞參數 * * */ 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 傳遞參數 * @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上測試兩個接口即可

SpringCloud Ribbon負載均衡代碼實例

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久夜视频 | 在线播放免费播放av片 | 免费观看欧美一级特黄 | 99在线在线视频免费视频观看 | 99精品视频一区在线观看miya | 成人爱爱网站在线观看 | 日本高清在线中文字幕网 | 国产最爽的乱淫视频国语对 | 手机看片免费基地你懂的 | 国产精品欧美亚洲韩国日本 | 美女黄色毛片免费看 | 亚洲精品欧洲一区二区三区 | 久久ri精品高清一区二区三区 | 国产看午夜精品理论片 | 男人久久天堂 | 欧美俄罗斯一级毛片激情 | 中文字幕在线观看不卡视频 | 国产精品久久久久久久久免费 | 国产在线精品一区二区高清不卡 | 亚洲午夜久久 | 国产三级久久久精品三级 | 亚洲视频精品在线 | 欧美午夜在线观看理论片 | www操操操| 免费在线观看a级片 | 一级毛片免费播放视频 | 国产成人一区二区 | a一级毛片视频免费看 | heyzo北条麻妃久久 | 日韩在线视频中文字幕 | 久久久久欧美精品 | 久久这里只有精品视频99 | www午夜| 日韩三级黄色片 | 一级毛片真人不卡免费播 | 成人毛片免费观看视频大全 | 欧美日韩亚洲综合另类ac | 欧美另类孕交 | 久久精品三级视频 | 久草免费福利视频 | 欧美在线做爰高清视频 |