1.Spring-eureka(註冊中心) Eureka基礎架構包括 註冊中心,服務提供者,服務消費者 1.創建服務註冊中心 import org.springframework.boot.SpringApplication; import org.springframework.boot.auto ...
1.Spring-eureka(註冊中心)
Eureka基礎架構包括
註冊中心,服務提供者,服務消費者
1.創建服務註冊中心
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EureKaApp {
public static void main(String[] args) {
SpringApplication.run(EureKaApp.class, args);
}
}
2.在pom文件中繼承spring-boot-star核心 增加 spring-boot-web客戶端 增加spring-cloud-eureka-server 增加spring-cloud核心文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<!-- dependencyManagement dependencies區別 如果在dependencyManagement 設置了版本信息
那在dependencies就不需要設置版本啦 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<!-- <finalName>eurekaservice</finalName> -->
</build>
3.在application.properties文件中增加服務埠以前 應用名稱等
#埠名稱
server.port=6611
#服務名稱
spring.application.name=server-service
#設置為false代表不註冊自己服務
eureka.client.register-with-eureka=false
#由於註冊中心的只測就是維護服務示例,他並不需要去檢索服務,所以設置為false
eureka.client.fetch-registry=false
#地址 本地ip
eureka.instance.hostname=127.0.0.1
#訪問地址 預設為自己的服務就是註冊中心
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
## 心跳間隔 服務續約任務的調度時間 多少秒調度一次 心跳
eureka.instance.lease-renewal-interval-in-seconds= 7
## 服務失效時間: 如果多久沒有收到請求,則可以刪除服務
eureka.instance.lease-expiration-duration-in-seconds = 7
4.開啟入口,在瀏覽器中輸入127.0.0.1:埠名 即可打開註冊中心
5.高可用的註冊中心也就是相當於創建兩個服務註冊中心 將服務分別註冊到兩個註冊中心,防止註冊中心宕機後影響業務。
2.Spring-Cloud創建客戶端並註冊到註冊中心
1.創建啟動類(這裡使用EnableEurekaClient註解註入到註冊中心的 也可以使用@EnableDiscoveryClient因為EnableEurekaClient內部實現了@EnableDiscoveryClient註解
或者使用@SpringCloudApplication因為他內部實現了@EnableDiscoveryClien,和@EnableCircuitBreaker(斷路器)註解)
@SpringBootApplication
@EnableEurekaClient
public class RequestApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(RequestApp.class, args);
}
}
2.在pom中增加 spring-boot核心,spring-cloud核心和,spring-cloud-start-eureka,spring-boot-start-web客戶端
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<dependencies>
<!-- 添加對Web的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Test -->
<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>
<!--依賴管理,用於管理spring-cloud的依賴,其中Camden.SR3是版本號 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.application.properties文件
#服務名稱 在註冊中心中application的名稱
spring.application.name=server-request
#項目埠 註意該埠不可和註冊中心埠一直
server.port=6614
#設置伺服器地址 設置註冊中心的地址也就是上面所配置的eureka.client.serviceUrl.defaultZone 如果是多個的話逗號隔開
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6611/eureka/
#命令執行超時時間,預設1000ms 這個是在使用hystrix容錯機制時配置的用於設置請求時間(服務A調用服務B所需的時間單位毫秒)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000
# 執行是否啟用超時,預設啟用true
hystrix.command.default.execution.timeout.enabled=true