相信有很多團隊在老Web項目(zookeeper,dubbo,tomcat)想要過渡到新的Eureka註冊管理的Spring Boot都會遇到這樣一個問題,新項目想調用老項目提供的服務,或者不想採用Spring Boot 而是直接想使用Eureka 替換掉原有的zookeeper和dubbo,那怎麼辦0.0 能不能將老項目註冊到Eureka Server?
1.項目jar依賴:
gradle:
compile "com.netflix.eureka:eureka-client:1.4.12"
maven:
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
<version>1.4.12</version>
</dependency>
此處主要依賴的是Netflix的eureka-client jar包(spring boot也是對它的一個簡單封裝)要注意這裏的jar包版本要與Spring Boot項目依賴的eureka-client jar包版本一致 不然可能不兼容
下面是該jar包所有的依賴(有jar包衝突的要處理解決一下~)
2.添加Listener用於將節點註冊到Eureka Server
package com.kowalski.web.eureka;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryManager;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Created by Kowalski on 2017/5/18
* Updated by Kowalski on 2017/5/18
*/
@Slf4j
public class EurekaInitAndRegisterListener implements ServletContextListener {
private static final DynamicPropertyFactory configInstance = DynamicPropertyFactory
.getInstance();
/**
* * Notification that the web application initialization
* * process is starting.
* * All ServletContextListeners are notified of context
* * initialization before any filter or servlet in the web
* * application is initialized.
*
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
/**設置被讀取配置文件名稱 默認config.properties*/
Properties properties = new Properties();
properties.setProperty("archaius.configurationSource.defaultFileName", "config.properties");
System.setProperties(properties);
/**註冊*/
registerWithEureka();
}
public void registerWithEureka() {
/**加載本地配置文件 根據配置初始化這臺 Eureka Application Service 並且註冊到 Eureka Server*/
DiscoveryManager.getInstance().initComponent(
new MyInstanceConfig(),
new DefaultEurekaClientConfig());
/**本臺 Application Service 已啓動,準備好侍服網絡請求*/
ApplicationInfoManager.getInstance().setInstanceStatus(
InstanceInfo.InstanceStatus.UP);
log.info("o2o eureka Application Service initing and registering");
/**Application Service 的 Eureka Server 初始化以及註冊是異步的,需要一段時間 此處等待初始化及註冊成功 可去除*/
// String vipAddress = configInstance.getStringProperty(
// "eureka.vipAddress", "o2o").get();
// InstanceInfo nextServerInfo = null;
// while (nextServerInfo == null) {
// try {
// nextServerInfo = DiscoveryManager.getInstance()
// .getDiscoveryClient()
// .getNextServerFromEureka(vipAddress, false);
// } catch (Throwable e) {
// log.info("Waiting for service to register with eureka..");
// try {
// Thread.sleep(10000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
//
//
// }
// }
// log.info("Service started and ready to process requests..");
}
/**
* * Notification that the servlet context is about to be shut down.
* * All servlets and filters have been destroy()ed before any
* * ServletContextListeners are notified of context
* * destruction.
*
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
DiscoveryManager.getInstance().shutdownComponent();
}
}
在web.xml中添加listener,項目在啓動時進行註冊到eureka Server(異步)
3.Eureka配置
在配置目錄下添加config.properties文件(默認讀取的文件名)添加配置:
###Eureka Client configuration for Sample Eureka Service
#Properties based configuration for eureka client. The properties specified here is mostly what the users
#need to change. All of these can be specified as a java system property with -D option (eg)-Deureka.region=us-east-1
#For additional tuning options refer <url to go here>
#部署應用程序的區域
# - 對於AWS指定一個AWS區域
# - 對於其他數據中心,指定一個指示該區域的任意字符串。
# 這裏主要指定美國東部D
eureka.region=default
#服務指定應用名,這裏指的是eureka服務本身(相當於boot中的app.name)
eureka.name=o2oProject
#客戶識別此服務的虛擬主機名,這裏指的是eureka服務本身(相當於boot中的serviceId)
eureka.vipAddress=o2o
#服務將被識別並將提供請求的端口(web服務部署的tomcat端口)
eureka.port=8810
#設置爲false,因爲該配置適用於eureka服務器本身的eureka客戶端。
#在eureka服務器中運行的eureka客戶端需要連接到其他區域中的服務器。
#對於其他應用程序,不應設置(默認爲true),以實現更好的基於區域的負載平衡。
eureka.preferSameZone=true
#如果要使用基於DNS的查找來確定其他eureka服務器(請參見下面的示例),請更改此選項
eureka.shouldUseDns=false
eureka.us-east-1.availabilityZones=default
#由於shouldUseDns爲false,因此我們使用以下屬性來明確指定到eureka服務器的路由(eureka Server地址)
eureka.serviceUrl.default=http://localhost:7003/eureka/
以上是一些基本配置,想了解更多配置可以去看com.netflix.appinfo.EurekaInstanceConfig
這裏有時候會有個問題,項目會報找不到eureka Server的hostName的錯,在boot中有個preferIpAdress的配置,配置後會默認使用ip訪問而不是主機名hostName,在原生的eureka 中寫preferIpAdress配置不會被解析讀取,因此我們仿照boot的該配置做法:
package com.kowalski.web.eureka;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by kowalski on 2017/5/25
*/
public class MyInstanceConfig extends MyDataCenterInstanceConfig {
@Override
public String getHostName(boolean refresh) {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return super.getHostName(refresh);
}
}
}
到這裏,在tomcat啓動的時候,該節點就會註冊到Eureka Server上並供其他項目正常調度,同樣的,也不影響原來的zk,dubbo,調用走的都是Http請求,我們目前使用Spring web client中的Restemplate,便於對所有Http請求的統一管理(使用HttpClient連接池,統一的中文亂碼處理等等),使用feignClient也可以。
4.簡易客戶端demo
好久沒更新了,這個還是以前用的了,很多細節也記不起來了,奉上剛寫的demo供大家學習之用~
https://pan.baidu.com/s/1clbu0pkdoNjgxKWDDogXFw
Email:[email protected]