這篇文章主要介紹“怎么把非springboot項目集成eureka”,在日常操作中,相信很多人在怎么把非springboot項目集成eureka問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么把非springboot項目集成eureka”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
10余年的江陽網站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。營銷型網站建設的優(yōu)勢是能夠根據用戶設備顯示端的尺寸不同,自動調整江陽建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“江陽網站設計”,“江陽網站推廣”以來,每個客戶項目都認真落實執(zhí)行。
隨著SOA架構的演進,越來越多的服務商需要提供一種通用的可以動態(tài)伸縮的基礎架構,
而對嘻「」來說,亦是如此。并且隨著TPS的不斷增加,more and more endpoint 需要整合這種分布式基礎架構。這里有點類似當年google的Bigtable 的論文剛發(fā)表時的案例,對不同業(yè)務,或者相同業(yè)務不同功能,在或者相同功能實現(xiàn)節(jié)點拓展,分布式處理。(要實現(xiàn)分布式處理能力的系統(tǒng)必然伴隨這分布式場景的一些問題,這里不做討論。
類似Dubbo分布式服務治理,不同的微服務需要依賴注冊中心做服務治理與管控,springCloud 也是這樣,作為微服務的生態(tài),不同的組件負責不同的功能(例如archaius 做配置and eureka 做服務PUBSUB等...)。
簡單的說.通過分析源碼發(fā)現(xiàn)DiscoveryClient對象是客戶端啟動加載的核心類,

它創(chuàng)建的時候就會去注冊,如下

在restemplate 調用的過程中需要先通過應用層通信拿到服務器對應spring.application.name 為vipaddress的InstanceInfo,instanceOf里面維護了對應的你將要調用的服務器或者服務如下:

可能會在這里拿不出來實例信息,最后報沒有對應服務實例,以上來自Applications Class,
源碼后文會詳細分析,這里不過多分析只解決問題
這里按照官網案例搭建注冊環(huán)境,可能會出現(xiàn)一切準備就緒然注冊不上去的情況。
走一遍注冊流程如下:


Archaius會默認加載classpath下的config.properties文件作為當前內存資源配置,也可以改,通過archaius.configurationSource.defaultFileName,當然既然是動態(tài)的肯定也是可以做多數(shù)據源動態(tài)處理,具體不詳細說明了
具體配置意義先不考慮,先為了解決問題考慮環(huán)境
手動注冊這一截是沒有的 顯示empty
所以我們需要把這一截加上去,
代碼如下:
int port = instanceInfo.getPort();
Map<String, String> maps = Maps.newHashMap();
maps.put("management.port", String.valueOf(port));
//activity-manager:dev-10.0.2.17:8010
applicationInfoManager.getInstance().registerAppMetadata(maps);還有一個細節(jié)問題:
Archaius1Utils.initConfig("eureka-client");
這個是在查看報錯日志的時候報的,為了更好的兼容框架需要解決,大概意思是說缺少這個配置 
在加載此類實現(xiàn)類的時候就會一起初始化該類。很明顯組件已經幫我們做了,我們只需要提供對應配置文件即可 
依賴為


這里需要注意的兩點:


繼續(xù)DEBUG會看到:

這里開始遞歸調用攔截器,也就是我們在啟動時候放進去的攔截器會在這里調用

當然這里有個細節(jié)

這里我們需要關注兩個點
1 通過服務serviceId拿到核心instanceInfo ,下面會解釋
2 通過對應服務信息選擇對應服務調用
分別對應第一二行代碼
繼續(xù)debug

這里需要通過一個serviceId加載一個LloadBalancer,代碼如下 
Spring 默認會調用以上工廠加載,所以我們點進去會看到 
這里也需要注意兩點:
1 我們這個lloadbalancer是用的時候才去加載,換句話說懶加載
2 這里有個緩存的 操作 第一個if為false就會返回緩存,換句話說只會創(chuàng)建一次
然后我們進去

這些東西。


以上的clientConfig 就是我們在代碼設置進去的EurekaClientConfigBean
現(xiàn)在還沒有上面參數(shù),不要緊因為還沒有開始初始化。
繼續(xù)加載完之后開始加載
上
圖大概意思就是先加載父類baseLoadbalancer然后加載自己
restOfInit方法就是核心加載方法

上圖標紅的是比較重要的方法

這里比較重點的如上標紅處
進去看到:

他就會走到這里,這是什么呢?

對應一下會從visualHostNameAppMap 里面通過vipAddress那到List<InstanceOf>,成功拿到instanceInfo之后回到剛開始進源碼的地方就會返回一個包含對應instanceInfo的loadBalancer,
總結一下:
負載均衡實現(xiàn)原理概述為根據配置加載負載均衡攔截器,用戶客戶端調用遍歷處理,通過servceId通過HTTP拿到對應instanceInfo(和dubbo流程差不多,第一次都需要去拿之后緩存在本地。默認90秒把重新調用一次拉取信息)多個需要依算法選取一個然后進行遠程調用。
代碼如下:
package com.kili.lipapay.nmc.common;
import com.google.common.collect.Maps;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.EurekaClient;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
@Component
class SimpleEurakeService {
private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory
.getInstance();
public void registerWithEureka() throws IOException {
ApplicationInfoManager applicationInfoManager = null;
EurakeInstanceConfig config = null;
InstanceInfo instanceInfo = null;
// Register with Eureka
if (applicationInfoManager == null) {
config = new EurakeInstanceConfig();
instanceInfo = new EurekaConfigBasedInstanceInfoProvider(config).get();
applicationInfoManager = new ApplicationInfoManager(config, instanceInfo);
}
// Archaius1Utils.initConfig("eureka-client");
Properties properties = new Properties();
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
properties.load(inputStream);
properties.setProperty("eureka.ipAddr", InetAddress.getLocalHost().getHostAddress());
String instanceId = applicationInfoManager.getInfo().getAppName() + ":dev-" + properties.getProperty("eureka.ipAddr") + ":" + properties.getProperty("eureka.port");
properties.setProperty("eureka.instanceId", instanceId);
ConfigurationManager.loadProperties(properties);
int port = instanceInfo.getPort();
Map<String, String> maps = Maps.newHashMap();
maps.put("management.port", String.valueOf(port));
//activity-manager:dev-10.0.2.17:8010
applicationInfoManager.getInstance().registerAppMetadata(maps);
applicationInfoManager.getInstance().setInstanceStatus(
InstanceInfo.InstanceStatus.UP);
EurekaClient eurekaClient = new DiscoveryClient(applicationInfoManager, new DefaultEurekaClientConfig());
String vipAddress = configInstance.getStringProperty(
"eureka.vipAddress", "unknown").get();
InstanceInfo nextServerInfo = null;
while (nextServerInfo == null) {
try {
nextServerInfo = eurekaClient
.getNextServerFromEureka(vipAddress, false);
} catch (Throwable e) {
System.out
.println("Waiting for service to register with eureka..");
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public void unRegisterWithEureka() {
// Un register from eureka.
DiscoveryManager.getInstance().shutdownComponent();
}
private void processRequest(final Socket s) {
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line = rd.readLine();
if (line != null) {
System.out.println("Received the request from the client.");
}
PrintStream out = new PrintStream(s.getOutputStream());
System.out.println("Sending the response to the client...");
out.println("Reponse at " + new Date());
} catch (Throwable e) {
System.err.println("Error processing requests");
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@PostConstruct
private void init() throws IOException {
SimpleEurakeService sampleEurekaService = new SimpleEurakeService();
sampleEurekaService.registerWithEureka();
}配置如下:
# note that for a purely client usage (e.g. only used to get information about other services, # there is no need for registration. This property applies to the singleton DiscoveryClient so # if you run a server that is both a service provider and also a service consumer, # then don't set this property to false. #eureka.shouldEnforceRegistrationAtInit=false ## configuration related to reaching the eureka serversSS eureka.client.service-url.defaultZone=http://localhost:7025/eureka eureka.serviceUrl.default=http://localhost:7025/eureka spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=config-server eureka.region=default spring.application.name=sampleservice #Name of the application to be identified by other services eureka.name=sampleservice #Virtual host name by which the clients identifies this service eureka.vipAddress=sampleservice #The port where the service will be running and serving requests eureka.port=8080 #For eureka clients running in eureka server, it needs to connect to servers in other zones eureka.preferSameZone=false #Change this if you want to use a DNS based lookup for determining other eureka servers. For example #of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.properties eureka.shouldUseDns=false eureka.us-east-1.availabilityZones=default erueka.registration.enabled=true
到此,關于“怎么把非springboot項目集成eureka”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
分享名稱:怎么把非springboot項目集成eureka
分享網址:http://www.chinadenli.net/article14/pespde.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站內鏈、網站制作、動態(tài)網站、商城網站、搜索引擎優(yōu)化、軟件開發(fā)
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)