如何從eureka獲取服務(wù)的ip和端口號進(jìn)行Http的調(diào)用
eureka獲取服務(wù)ip和端口號進(jìn)行Http調(diào)用
我告訴你們?yōu)樯段乙葟膃ureka首先獲取 goods的服務(wù)ip, 在用ip的方式使用http調(diào)用goods的服務(wù).
因為公司的規(guī)定, 不讓我們用Feigin. 我TMD的都震驚了, 我都不知道為啥. 我也不想寫同事的ip地址, 做配置, 因為都去eureka里面注冊了, 所以就這樣調(diào)用了, 真是蛋疼. 這種微服務(wù), 這種奇葩的方式..
package com.util;?
import com.alibaba.fastjson.JSON;
import com.curefun.attendance.appclient.vo.ApplyListVO;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
?
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
?
/**
?* eureka的工具類
?*
?* @author zhangke
?* @time 2019年12月5日18:11:12
?*/
@Slf4j
public class EurekaUtil {
?
? ? /**
? ? ?* 解析eureka的返回數(shù)據(jù)
? ? ?*/
? ? private static Pattern PATTERN_URL = Pattern.compile("<homePageUrl>(.+?)</homePageUrl>");
?
? ? /**
? ? ?* IP的緩存
? ? ?*/
? ? private static ConcurrentHashMap<String, List<String>> IP_CACHE = new ConcurrentHashMap<>();
?
? ? /**
? ? ?* 緩存的名字
? ? ?*/
? ? private final static String IP_NAME = "goodsIp";?
?
? ? /**
? ? ?* 獲取服務(wù)的所有地址(注冊在 eureka server 上的服務(wù))
? ? ?*
? ? ?* @param eurekaIp
? ? ?* @return
? ? ?*/
? ? public static List<String> getAllServiceAddr(String eurekaIp) {
? ? ? ? //先查詢緩存
? ? ? ? List<String> list = IP_CACHE.get(IP_NAME);
? ? ? ? if (list != null && list.size() > 0) {
? ? ? ? ? ? return list;
? ? ? ? }
? ? ? ? String serviceName = "GOODS";
? ? ? ? String url = eurekaIp + "apps/" + serviceName;
? ? ? ? OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(2, TimeUnit.SECONDS).build();
? ? ? ? Request request = new Request.Builder()
? ? ? ? ? ? ? ? //請求接口 如果需要傳參拼接到接口后面
? ? ? ? ? ? ? ? .url(url)
? ? ? ? ? ? ? ? .addHeader("Content-Type", "application/json")
? ? ? ? ? ? ? ? .addHeader("Accept", "application/xml")
? ? ? ? ? ? ? ? .get()
? ? ? ? ? ? ? ? .build();
? ? ? ? Response response = null;
? ? ? ? List<String> result = new ArrayList<>();
? ? ? ? try {
? ? ? ? ? ? response = okHttpClient.newCall(request).execute();
? ? ? ? ? ? if (response.isSuccessful()) {
? ? ? ? ? ? ? ? String responseContent = response.body().string();
? ? ? ? ? ? ? ? Matcher matcher = PATTERN_URL.matcher(responseContent);
? ? ? ? ? ? ? ? while (matcher.find()) {
? ? ? ? ? ? ? ? ? ? String homepage = matcher.group(1).trim();
? ? ? ? ? ? ? ? ? ? result.add(homepage);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? log.error("從eureka中查詢GOODS的服務(wù)實例出錯了.原因是 {}", e.getMessage());
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ? IP_CACHE.put(IP_NAME, result);
? ? ? ? return result;
? ? }?
?
? ? /**
? ? ?* GET請求獲取列表數(shù)據(jù), 同步請求
? ? ?*
? ? ?* @param ip
? ? ?* @param userId
? ? ?* @param state
? ? ?* @param pageNo
? ? ?* @param pageSize
? ? ?* @param key
? ? ?* @return
? ? ?*/
? ? public static List<ApplyListVO> getProductList(String ip, String userId, Integer state, Integer pageNo, Integer pageSize, String key) {
? ? ? ? if (StringUtils.isBlank(ip)) {
? ? ? ? ? ? return Collections.emptyList();
? ? ? ? }
? ? ? ? StringBuilder sb = new StringBuilder(ip);
? ? ? ? sb.append("goods/apply/getStuGoodsApplyList?user_id=").append(userId);
? ? ? ? sb.append("&state=").append(state).append("&pageNo=").append(pageNo);
? ? ? ? sb.append("&pageSize=").append(pageSize);
? ? ? ? if (StringUtils.isNotBlank(key)) {
? ? ? ? ? ? sb.append("&key=").append(key);
? ? ? ? }
? ? ? ? long millis = System.currentTimeMillis();
? ? ? ? Request request = new Request.Builder().url(sb.toString()).get().build();
? ? ? ? OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(2, TimeUnit.SECONDS).build();
? ? ? ? try {
? ? ? ? ? ? Response response = okHttpClient.newCall(request).execute();
? ? ? ? ? ? if (response.isSuccessful()) {
? ? ? ? ? ? ? ? String string = response.body().string();
? ? ? ? ? ? ? ? //json的轉(zhuǎn)換.換成我需要的實體對象,為空的不返回null, 一個小技巧
? ? ? ? ? ? ? ? List<ApplyListVO> applyListVOS = JSON.parseArray(string, ApplyListVO.class);
? ? ? ? ? ? ? ? if (applyListVOS == null) {
? ? ? ? ? ? ? ? ? ? applyListVOS = Collections.emptyList();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? long millis2 = System.currentTimeMillis();
? ? ? ? ? ? ? ? log.info("從周X那里查詢到物資的列表,請求的url是:{},返回結(jié)果是:{}", sb.toString(), applyListVOS);
? ? ? ? ? ? ? ? log.info("查詢的耗時是(微秒):{}", (millis2 - millis));
? ? ? ? ? ? ? ? return applyListVOS;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? return Collections.emptyList();
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? log.error("從周X的接口中查詢List信息出錯了.原因是 {}", e.getMessage());
? ? ? ? ? ? return Collections.emptyList();
? ? ? ? }
? ? }?
}這樣就可以很方便的調(diào)用了,
做微服務(wù)的一定要協(xié)調(diào)好各個組件的關(guān)系, 不然很容易兩個人要對不同的接口, 很麻煩.
對了, 現(xiàn)在我們的eureka 配置有個問題, 就是一個服務(wù)掛了之后, 需要很長的時間才能去掉. 然后請求到達(dá)網(wǎng)關(guān)之后還是去了已經(jīng)關(guān)閉的服務(wù), 就很蛋疼了. 需要配置一個參數(shù).
eureka: ? server: ? ?#配置屬性,但由于 Eureka 自我保護(hù)模式以及心跳周期長的原因, ? ? ? ? ? ? ?#經(jīng)常會遇到 Eureka Server 不剔除已關(guān)停的節(jié)點的問題 ? ? enable-self-preservation: false # 設(shè)為false,關(guān)閉自我保護(hù) ? ? eviction-interval-timer-in-ms: 5000 # 清理間隔(單位毫秒,默認(rèn)是60*1000)啟用主動失效, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #并且每次主動失效檢測間隔為3s
先要關(guān)閉自我保護(hù) enable-self-preservation: false
eviction-interval-timer-in-ms 啟用主動失效,并且每次主動失效檢測間隔為5s
Eureka Server會定時(間隔值是eureka.server.eviction-interval-timer-in-ms,默認(rèn)值為0,默認(rèn)情況不刪除實例)進(jìn)行檢查,如果發(fā)現(xiàn)實例在在一定時間(此值由客戶端設(shè)置的eureka.instance.lease-expiration-duration-in-seconds定義,默認(rèn)值為90s)內(nèi)沒有收到心跳,則會注銷此實例。
現(xiàn)在的eureka的配置 已經(jīng)改過來了。
eureka頁面中顯示ip+端口
eureka:
? instance:
? ? prefer-ip-address: true
? ? instance-id: ${spring.cloud.client.ip-address}:${server.port}
# spring.cloud.client.ip-address 為 2.x 版本,1.x 版本為 spring.cloud.client.ipAddress
# ${spring.cloud.client.ip-address} 這個值從 org.springframework.cloud.client.HostInfoEnvironmentPostProcessor 中獲取@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
?? ??? ?SpringApplication application) {
?? ?InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
?? ?LinkedHashMap<String, Object> map = new LinkedHashMap<>();
?? ?map.put("spring.cloud.client.hostname", hostInfo.getHostname());
?? ?map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
?? ?MapPropertySource propertySource = new MapPropertySource(
?? ??? ??? ?"springCloudClientHostInfo", map);
?? ?environment.getPropertySources().addLast(propertySource);
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 2021.1 首個 Beta 版本發(fā)布
這篇文章主要介紹了IntelliJ IDEA 2021.1 首個 Beta 版本發(fā)布,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Java設(shè)計模式之狀態(tài)模式(State模式)介紹
這篇文章主要介紹了Java設(shè)計模式之狀態(tài)模式(State模式)介紹,本文講解了何時使用狀態(tài)模式、如何使用狀態(tài)模式等內(nèi)容,需要的朋友可以參考下2015-03-03
使用java實現(xiàn)BBS論壇發(fā)送郵件過程詳解
這篇文章主要介紹了使用java發(fā)送郵件過程詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

