文本 Spring Cloud是一個微服務框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分佈式系統解決方案。java
Spring Cloud對微服務基礎框架Netflix的多個開源組件進行了封裝,同時又實現了和雲端平臺以及和Spring Boot開發框架的集成。web
Spring Cloud爲微服務架構開發涉及的配置管理,服務治理,熔斷機制,智能路由,微代理,控制總線等spring
Eureka是Netflix的一個核心子模塊,Eureka是基於的Rest服務,用於定位服務。服務的註冊與發現對於微服務架構來講是很是重要的,通過配置以後訪問服務只需提供服務標識便可。其功能相似於dubbo的註冊中心zookeeper。apache
分佈式系統中通常須要遵循CAP原則:CAP原則又稱CAP定理,指的是在一個分佈式系統中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區容錯性),三者不可兼得(摘自百度百科)。安全
Eureka和zookeeper不一樣點在於,zookeeper是基於CP原則構建,而Eureka是基於AP原則構建。即任什麼時候刻zookeeper的訪問請求都能獲得一致性的結果,可是其並不保證服務的可用性,zookeeper的選舉機制在節點多了以後會很是耗時。在Eureka平臺中不會出現相似於zookeeper的選舉leader的過程,若是某臺服務器宕機,Eureka會自動切換到新的Eureka節點,當服務器恢復以後,Eureka會從新將其歸入服務器集羣管理當中。bash
開發環境:
開發工具:Spring Tool Suite 4
JDK版本:1.8
SpringBoot版本:2.0.9.RELEASE
SpringCloud版本:Finchley.SR3
複製代碼
File-->New-->Maven Project,選中Create a simple project(skip archetype selection) 點擊Next,輸入相應的groupId和ArtifactId,packaging修改成pom,點擊完成,建立成功。此parent項目爲整個springcloud的父項目,主要用於版本控制和公共jar包引入。服務器
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hewl</groupId>
<artifactId>springcloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
右鍵cloud-demo-parent項目-->Maven-->New Maven Module Project,Module Name輸入cloud-demo-eureka-server,點擊Finish完成建立,PS:若是項目出現紅叉,請Maven-->update project架構
建立成功項目結構是這樣的: 根據我的習慣,我改爲了這樣,沒有resource文件夾的同窗,右鍵項目-->New-->source Folder<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hewl</groupId>
<artifactId>cloud-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>cloud-demo-eureka-server</artifactId>
<name>cloud-demo-eureka-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency> -->
</dependencies>
</project>
複製代碼
server:
port: 8761 # 你的端口
spring:
# security:
# user:
# name: admin
# password: admin
application:
name: service-registry
eureka:
instance:
hostname: localhost # 你的地址
instance-id: ${spring.application.name}:${server.port}:@project.version@
prefer-ip-address: true
client:
registerWithEureka: false # 表示是否註冊自身到eureka服務器,由於當前這個應用就是eureka服務器,不必註冊自身,因此這裏是false
fetchRegistry: false # fetchRegistry表示是否從eureka服務器獲取註冊信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
複製代碼
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hewl</groupId>
<artifactId>cloud-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>cloud-demo-eureka-client</artifactId>
<name>cloud-demo-eureka-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
複製代碼
spring:
application:
name: service-eureka-client #服務名稱
server:
port: 8800 #端口
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #服務中心地址
複製代碼
package com.hewl.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
private String port;
@GetMapping("/test")
public String testMethod(@RequestParam("name") String name) {
return "hello world!!! 端口爲:" + port + "名字爲:" + name;
}
}
複製代碼
像eureka server的這種管理頁面,若是隨便登陸是很是不安全的,那麼怎麼辦呢?咱們這裏引入這個jar包,給eureka server管理頁面增長一個登陸驗證。app
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
複製代碼
server:
port: 8761 # 你的端口
spring:
security:
user:
name: admin
password: admin
application:
name: service-registry
eureka:
instance:
hostname: localhost # 你的地址
client:
registerWithEureka: false # 表示是否註冊自身到eureka服務器,由於當前這個應用就是eureka服務器,不必註冊自身,因此這裏是false
fetchRegistry: false # fetchRegistry表示是否從eureka服務器獲取註冊信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
複製代碼
spring:
application:
name: service-eureka-client #服務名稱
server:
port: 8800 #端口
eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/ #服務中心地址
複製代碼