1. 程式人生 > >Linux 下 Eureka 伺服器的部署

Linux 下 Eureka 伺服器的部署

                        1. 檢視 Linux 相關資訊        版本檢視# cat /etc/issueCentOS release 6.5 (Final)        位數檢視# getconf LONG_BIT64        第 2 - 4 步是 Eureka Server war 包的構建,如果你嫌自己構建麻煩可以去 http://mvnrepository.com/artifact/com.netflix.eureka/eureka-server 下載一個,比如作者下載的是 eureka-server-1.1.134.war,然後跳過 2 - 4 步直接進入第 5 步。        2. 安裝 git# yum install git
        檢視安裝結果# git --versiongit version 1.7.1# which git/usr/bin/git        3. 安裝 gradle        安裝 Gradle 要求實現好了 JDK(關於 Linux 上 JDK 的安裝參考《如何在 64 位 linux 機器上安裝 jdk1.6?》)。Gradle 自帶 Groovy 庫,已有安裝好的 Groovy 將被 Gradle 忽視。        然後去官網下載 gradle-1.12-bin.zip(大小 41 MB)到 /root/kdf 目錄:http://www.gradle.org/downloads# unzip gradle-1.12-bin.zip# cp -r ~/kdf/gradle-1.12 /usr/local/gradle-1.12# vi /etc/profile
        在末尾新增以下幾行:
export GRADLE_HOME=/usr/local/gradle-1.12export PATH=$PATH:$GRADLE_HOME/bin
        儲存退出,然後執行# source /etc/profile        驗證環境變數:# echo $PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.7.0_60/bin:/usr/local/gradle-1.12/bin        驗證 gradle 是否安裝成功:# gradle -v------------------------------------------------------------Gradle 1.12------------------------------------------------------------Build time:   2014-04-29 09:24:31 UTCBuild number: noneRevision:     a831fa866d46cbee94e61a09af15f9dd95987421Groovy:       1.8.6Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013Ivy:          2.2.0JVM:          1.7.0_60 (Oracle Corporation 24.60-b09)OS:           Linux 2.6.32-431.el6.x86_64 amd64
        輸出結果中顯示了我們安裝的 Gradle 的版本以及本地環境配置資訊,證明安裝成功。        4. 構建 Eureka 伺服器        從 github 獲取 Eureka 原始碼:# git clone https://github.com/Netflix/eureka.git        在存放原始碼的路徑下執行以下命令以構建 Eureka 伺服器:# cd eureka/# ./gradlew clean build        成功後在 ./eureka-server/build/libs/ 目錄下找到 war 包。        5. 安裝 tomcat        安裝 Eureka 伺服器要求事先安裝好了 Tomcat。去 http://tomcat.apache.org/ 下載你想要的版本,比如作者下載的是 apache-tomcat-6.0.41.tar.gz,大小 7 MB。# tar zxvf apache-tomcat-6.0.41.tar.gz# cp -r apache-tomcat-6.0.41 /usr/local/tomcat# /usr/local/tomcat/bin/startup.sh        其他電腦客戶端訪問 http://serverIP:8080,如果小貓沒出來,在伺服器執行# curl http://localhost:8080        有 Tomcat 管理介面 HTML 文字輸出,說明沒有開放伺服器防火牆 8080 埠,開放該埠或者直接關掉防火牆。        6. 部署 Eureka server# /usr/local/tomcat/bin/shutdown.sh# cp eureka-server-1.1.134.war /usr/local/tomcat/webapps/eureka.war# /usr/local/tomcat/bin/startup.sh        Tomcat 啟動以後,其他電腦客戶端訪問 http://serverIP:8080/eureka,404。檢視 /usr/local/tomcat/logs/catalina.out,發現有警告資訊:2014-07-03 09:47:00,614 WARN  com.netflix.config.util.ConfigurationUtils:165 [main] [loadFromPropertiesFile] Unable to load properties fileorg.apache.commons.configuration.ConfigurationException: Unable to load the configuration from the URL file:/usr/local/tomcat/webapps/eureka/WEB-INF/classes/eureka-${environment}-${region}.propertiesat org.apache.commons.configuration.DefaultFileSystem.getInputStream(DefaultFileSystem.java:84)at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:323)at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:234)at org.apache.commons.configuration.AbstractFileConfiguration.<init>(AbstractFileConfiguration.java:197)at org.apache.commons.configuration.PropertiesConfiguration.<init>(PropertiesConfiguration.java:285)at com.netflix.config.util.OverridingPropertiesConfiguration.<init>(ConfigurationUtils.java:267)at com.netflix.config.util.ConfigurationUtils.loadFromPropertiesFile(ConfigurationUtils.java:160)at com.netflix.config.util.ConfigurationUtils.getConfigFromPropertiesFile(ConfigurationUtils.java:196)at com.netflix.config.util.ConfigurationUtils.getPropertiesFromFile(ConfigurationUtils.java:214)at com.netflix.config.ConfigurationManager.getPropertiesFromFile(ConfigurationManager.java:431)at com.netflix.config.ConfigurationManager.loadCascadedProperties(ConfigurationManager.java:288)at com.netflix.config.ConfigurationManager.loadCascadedPropertiesFromResources(ConfigurationManager.java:268)at com.netflix.eureka.DefaultEurekaServerConfig.init(DefaultEurekaServerConfig.java:93)at com.netflix.eureka.DefaultEurekaServerConfig.<init>(DefaultEurekaServerConfig.java:75)at com.netflix.eureka.EurekaBootStrap.initEurekaEnvironment(EurekaBootStrap.java:130)at com.netflix.eureka.EurekaBootStrap.contextInitialized(EurekaBootStrap.java:88)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583)at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)at org.apache.catalina.core.StandardService.start(StandardService.java:525)at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)at org.apache.catalina.startup.Catalina.start(Catalina.java:595)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.io.FileNotFoundException: /usr/local/tomcat/webapps/eureka/WEB-INF/classes/eureka-${environment}-${region}.properties (No such file or directory)at java.io.FileInputStream.open(Native Method)at java.io.FileInputStream.<init>(FileInputStream.java:146)at java.io.FileInputStream.<init>(FileInputStream.java:101)at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)at java.net.URL.openStream(URL.java:1037)at org.apache.commons.configuration.DefaultFileSystem.getInputStream(DefaultFileSystem.java:80)... 39 more        這是找不到 eureka-${environment}-${region}.properties 配置檔案# /usr/local/tomcat/bin/shutdown.sh# vi /usr/local/tomcat/webapps/eureka/WEB-INF/classes/eureka-server.properties        將最後一行 @next=eureka-${environment}-${region}.properties        去掉。        此外,/usr/local/tomcat/logs/catalina.out 在上述警告後邊還有以下錯誤:2014-07-03 09:47:01,920 ERROR com.netflix.discovery.DiscoveryClient:1015 [main] [makeRemoteCall] Can't get a response from http://localhost/eureka/v2/apps/Can't contact any eureka nodes - possibly a security group issue?com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refusedat com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:184)at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:120)at com.sun.jersey.api.client.Client.handle(Client.java:648)at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)at com.netflix.discovery.DiscoveryClient.getUrl(DiscoveryClient.java:1389)at com.netflix.discovery.DiscoveryClient.makeRemoteCall(DiscoveryClient.java:960)at com.netflix.discovery.DiscoveryClient.makeRemoteCall(DiscoveryClient.java:909)at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:757)at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:653)at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:237)at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:169)at com.netflix.discovery.DiscoveryManager.initComponent(DiscoveryManager.java:84)at com.netflix.eureka.EurekaBootStrap.initEurekaEnvironment(EurekaBootStrap.java:159)at com.netflix.eureka.EurekaBootStrap.contextInitialized(EurekaBootStrap.java:88)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583)at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)at org.apache.catalina.core.StandardService.start(StandardService.java:525)at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)at org.apache.catalina.startup.Catalina.start(Catalina.java:595)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refusedat org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:151)at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827)at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:170)... 39 moreCaused by: java.net.ConnectException: Connection refusedat java.net.PlainSocketImpl.socketConnect(Native Method)at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)at java.net.Socket.connect(Socket.java:579)at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)... 46 more        這是 Tomcat 預設埠是 8080,而 Eureka 預設為 80。本文示例暫時用 8080,所以# vi /usr/local/tomcat/webapps/eureka/WEB-INF/classes/eureka-client.properties        將 eureka.port=80 改為 eureka.port=8080。# vi /usr/local/tomcat/webapps/eureka/WEB-INF/classes/eureka-client-test.properties        將eureka.serviceUrl.defaultZone=http://localhost/eureka/v2/eureka.serviceUrl.default.defaultZone=http://localhost/eureka/v2/        改為eureka.serviceUrl.defaultZone=http://localhost:8080/eureka/v2/eureka.serviceUrl.default.defaultZone=http://localhost:8080/eureka/v2/        重啟 Tomcat# /usr/local/tomcat/bin/startup.sh# tail -f /usr/local/tomcat/logs/catalina.out        或者直接檢視 catalina.out,發現有以下錯誤:2014-07-03 14:33:05,484 ERROR com.netflix.discovery.DiscoveryClient:1015 [main] [makeRemoteCall] Can't get a response from http://localhost:8080/eureka/v2/apps/Can't contact any eureka nodes - possibly a security group issue?com.sun.jersey.api.client.ClientHandlerException: java.net.SocketTimeoutException: Read timed outat com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:184)at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:120)at com.sun.jersey.api.client.Client.handle(Client.java:648)at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)at com.netflix.discovery.DiscoveryClient.getUrl(DiscoveryClient.java:1389)at com.netflix.discovery.DiscoveryClient.makeRemoteCall(DiscoveryClient.java:960)at com.netflix.discovery.DiscoveryClient.makeRemoteCall(DiscoveryClient.java:909)at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:757)at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:653)at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:237)at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:169)at com.netflix.discovery.DiscoveryManager.initComponent(DiscoveryManager.java:84)at com.netflix.eureka.EurekaBootStrap.initEurekaEnvironment(EurekaBootStrap.java:159)at com.netflix.eureka.EurekaBootStrap.contextInitialized(EurekaBootStrap.java:88)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)at org.apache.catalina.core.StandardService.start(StandardService.java:525)at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)at org.apache.catalina.startup.Catalina.start(Catalina.java:595)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.net.SocketTimeoutException: Read timed outat java.net.SocketInputStream.socketRead0(Native Method)at java.net.SocketInputStream.read(SocketInputStream.java:152)at java.net.SocketInputStream.read(SocketInputStream.java:122)at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827)at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:170)... 39 more        這個正常的,啟動的時候會去找其他的 server,找不到,過一會就自己啟動了(這時候我們去訪問 http://serverIP:8080 應該看不到那隻小貓)。        等待五分鐘左右,訪問 http://serverIP:8080,可以看到小貓頁面。訪問 http://serverIP:8080/eureka/,看到以下介面:Eureka部署成功介面        說明 Eureka Server 部署成功。        參考資料