server: port: 8762 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://test:111@localhost:8761/eureka server: enableSelfPreservation: false instance: prefer-ip-address: true #安全認證的配置 security: basic: enabled: true user: name: test password: 111
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://test:111@localhost:8762/eureka server: enableSelfPreservation: false instance: prefer-ip-address: true #安全認證的配置 security: basic: enabled: true user: name: test password: 111
eureka.client.serviceUrl.defaultZone
的url已經把憑證嵌入到它裏面,那麼HTTP基本的身份驗證將會被自動添加到你的eureka客戶端(curl風格,如 http://user:password@localhost:8761/eureka)。 pom.xml
中引入須要的如下依賴內容:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- eureka support --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
經過@EnableEurekaServer
註解啓動一個服務註冊中心提供給其餘應用進行對話。這一步很是的簡單,只須要在一個普通的Spring Boot應用中添加這個註解就能開啓此功能,好比下面的例子:java
@EnableEurekaServer //啓動一個服務註冊中心提供給其餘應用進行對話 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
在默認設置下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲,只須要在application.properties
中問增長以下配置:git
server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
pom.xml
中,加入以下配置:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
而後在啓動類上加@EnableEurekaClient註解,該註解能激活Eureka中的DiscoveryClient
實現。github
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
以後在對應的application.properties文件配置spring
spring.application.name=test server.port=8180 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/
經過spring.application.name
屬性,咱們能夠指定微服務的名稱後續在調用的時候只須要使用該名稱就能夠進行服務的訪問瀏覽器
eureka.client.serviceUrl.defaultZone
屬性對應服務註冊中心的配置內容,指定服務註冊中心的位置,告訴咱們該服務註冊到哪一個eureka上,如果多個eureka的話,中間用逗號隔開。緩存
而後再次在瀏覽器中輸入對應的eureka的地址,就能看到對應的test項目註冊在eureka上去了。安全
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #關鍵在於下面2個配置 instance: preferIpAddress: true instance-id: ${spring.cloud.client.ipAddress}:${server.port}
eureka註冊服務慢的問題如何解決?服務器
eureka.instance.leaseRenewalIntervalInSeconds 參考文檔: http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service
原文: Why is it so Slow to Register a Service? Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period. 翻譯: 做爲實例還涉及到與註冊中心的週期性心跳,默認持續時間爲30秒(經過serviceUrl)。在實例、服務器、客戶端都在本地緩存中具備相同的元數據以前,服務不可用於客戶端發現(因此可能須要3次心跳)。你可使用eureka.instance.leaseRenewalIntervalInSeconds 配置,這將加快客戶端鏈接到其餘服務的過程。在生產中,最好堅持使用默認值,由於在服務器內部有一些計算,他們對續約作出假設。
如何解決eureka Server不踢出已關停的節點的問題?架構
server端: eureka.server.enable-self-preservation (設爲false,關閉自我保護主要) eureka.server.eviction-interval-timer-in-ms 清理間隔(單位毫秒,默認是60*1000) client端: eureka.client.healthcheck.enabled = true 開啓健康檢查(須要spring-boot-starter-actuator依賴) eureka.instance.lease-renewal-interval-in-seconds =10 租期更新時間間隔(默認30秒) eureka.instance.lease-expiration-duration-in-seconds =30 租期到期時間(默認90秒) 示例: 服務器端配置: eureka: server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000 客戶端配置: eureka: instance: leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30 注意: 更改Eureka更新頻率將打破服務器的自我保護功能 參考文檔 https://github.com/spring-cloud/spring-cloud-netflix/issues/373
Eureka Environment的配置app
eureka.environment: 字符串 參考文檔: https://github.com/Netflix/eureka/wiki/Configuring-Eureka
Eureka DataCenter的配置
eureka.datacenter: cloud https://github.com/Netflix/eureka/wiki/Configuring-Eureka 這邊說:配置-Deureka.datacenter=cloud,這樣eureka將會知道是在AWS雲上