SpringCloud入門
1. 概述
SpringCloud是基于SpringBoot的一個(gè)微服務(wù)架構(gòu)
2. 單體項(xiàng)目
單體應(yīng)用:一個(gè)獨(dú)立的應(yīng)用,所有的模塊和資源都在一個(gè)項(xiàng)目中,最終把項(xiàng)目打包成一個(gè)文件,部署在一臺(tái)服務(wù)器上!
單體項(xiàng)目的優(yōu)點(diǎn):
①:開發(fā)部署簡單
②:成本相對較低
③:小型規(guī)模項(xiàng)目,性能更優(yōu)
單體項(xiàng)目的缺點(diǎn):
①:擴(kuò)展能力受限
②:模塊之間的耦合度較高
③:不便于維護(hù)升級
④:技術(shù)選型單一(只能使用同一種語言或者數(shù)據(jù)庫)
⑤:單一模塊故障會(huì)牽連其他模塊
3. 微服務(wù)
微服務(wù):將一個(gè)大型應(yīng)用拆分成多個(gè)小型應(yīng)用,這些小型項(xiàng)目部署在不同的容器中,有自己獨(dú)立的進(jìn)程,互相之間使用http協(xié)議restful來達(dá)到數(shù)據(jù)交互!這些小型項(xiàng)目同時(shí)運(yùn)行來完成大型項(xiàng)目的所有業(yè)務(wù)!
微服務(wù)與分布式:
微服務(wù)一定是分布式,它是對分布式更細(xì)粒度的劃分,分布式更是一種廣泛的應(yīng)用。
例如:將一個(gè)應(yīng)用部署在不同的計(jì)算機(jī)上,即可稱之為分布式,但不一定是微服務(wù)架構(gòu)!
微服務(wù)的優(yōu)點(diǎn):
①:便于局部擴(kuò)展
②:技術(shù)選型多樣化(可以使用不同的語言,不同的數(shù)據(jù)庫)
③:單個(gè)微服務(wù)便于開發(fā)維護(hù)
④:單個(gè)微服務(wù)復(fù)雜性降低
⑤:服務(wù)與服務(wù)之間耦合度降低
⑥:當(dāng)項(xiàng)目規(guī)模較大的時(shí)候,性能更優(yōu)
⑦:單個(gè)微服務(wù)故障,不會(huì)牽連到其他服務(wù)
微服務(wù)的缺點(diǎn):
①:成本相對較高
②:微服務(wù)數(shù)據(jù)交互受通訊影響
③:不便于部署
4. SpringCloud的組件
服務(wù)注冊發(fā)現(xiàn)——Netflix Eureka : 幫我們服務(wù)的通信地址的
客服端負(fù)載均衡——Netflix Ribbon\Feign : 解決網(wǎng)絡(luò)通信的
斷路器——Netflix Hystrix :解決微服務(wù)故障的
服務(wù)網(wǎng)關(guān)——Netflix Zuul :微服務(wù)的大門(安保部門)
分布式配置——Spring Cloud Config :統(tǒng)一管理微服務(wù)的配置
5. SpringCloud與Dubbo
SpringCloud屬于Spring系列,Dubbo屬于阿里巴巴
SpringCloud:是一個(gè)全方位的解決方案,基于http協(xié)議,具備搭建微服務(wù)的相關(guān)組件!
Dubbo:是一個(gè)rpc框架,使用原生的tcp請求,需要整合其他的組件開發(fā)!
從通信上來說:Dubbo更優(yōu),從開發(fā)的復(fù)雜性來說,SpringCloud更優(yōu),從功能完整度來說,SpringCloud更優(yōu)!
6. Eureka注冊中心
注冊中心:用于管理微服務(wù)的通訊地址,所有的微服務(wù)在啟動(dòng)時(shí),都會(huì)向注冊中心提交自己的通訊地址(IP,端口號),注冊中心會(huì)形成一個(gè)服務(wù)地址清單,所有的微服務(wù)會(huì)從注冊中心獲取地址清單,當(dāng)某個(gè)服務(wù)需要向另一個(gè)服務(wù)發(fā)起請求時(shí),該服務(wù)會(huì)從地址清單中獲取目標(biāo)服務(wù)的通訊地址,使用http發(fā)起調(diào)用!
注冊中心可以實(shí)現(xiàn)服務(wù)的自動(dòng)上下線,每個(gè)微服務(wù)都會(huì)使用心跳檢測(周期性地向注冊中心發(fā)送請求)向注冊中心續(xù)租,當(dāng)某個(gè)微服務(wù)故障的時(shí)候,不會(huì)再發(fā)送心跳檢測,那么注冊中心就會(huì)把該服務(wù)標(biāo)記為下線,同時(shí)從地址清單中移除下線的服務(wù)的地址,其他的微服務(wù)會(huì)同步新的通信清單!
7. Maven多模塊的方式搭建微服務(wù)
簡單分為三個(gè)模塊:Eureka注冊中心服務(wù)(端口號:3000),用戶服務(wù)(端口號:1000)、支付服務(wù)(端口號:2000)
用戶服務(wù)需要拉取支付服務(wù) !
1.創(chuàng)建父項(xiàng)目
pom.xml中的依賴
<!--springboot的父工程-->
<parent>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<!--springcloud的所需依賴-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在父項(xiàng)目中引用SpringBoot的父工程,同時(shí)管理SpringCloud所有的jar包,SpringCloud的版本必須與SpringBoot的版本對應(yīng)
2.在父項(xiàng)目中創(chuàng)建一個(gè)模塊作為Eureka服務(wù)
①:pom.xml中需要導(dǎo)入的依賴
<dependencies>
<!--eureka服務(wù)包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
spring-cloud-starter-netflix-eureka-server包中既包括eureka的服務(wù)端和客戶端,也包括web,所以不需要再導(dǎo)入web依賴
②:在application.yml中配置
server:
port: 3000
eureka:
instance:
hostname: 127.0.0.1
appname: eureka-center
client:
registerWithEureka: false
fetchRegistry: false #禁止獲取服務(wù)的通訊地址清單
serviceUrl:
#defaultZone: http://localhost:3000/eureka
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在此yml中需要說明:
registerWithEureka: false 禁止在注冊中心注冊自身,因?yàn)镋ureka既可以作為服務(wù)端也可作為客戶端,作為服務(wù)端的時(shí)候,不需要去注冊!
fetchRegistry: false 禁止獲取服務(wù)的通訊地址清單!
defaultZone: http://{server.port}/eureka/ 注冊中心的地址,使用$取值可以動(dòng)態(tài)修改地址!
③:創(chuàng)建主配置類
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer
{
public static void main( String[] args )
{
SpringApplication.run(EurekaServer.class);
}
}
@EnableEurekaServer 注解開啟Eureka服務(wù)!
訪問http://localhost:3000/即可查看Eureka的信息!
3.在父項(xiàng)目創(chuàng)建一個(gè)模塊作為用戶模塊服務(wù)
①:pom.xml中的依賴
<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>
spring-cloud-starter-netflix-eureka-client包中沒有web相關(guān),需要導(dǎo)入web依賴
②:在application.yml中配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
instance:
prefer-ip-address: true
instance-id: pay-server:2000
server:
port: 2000
spring:
application:
name: pay-server
defaultZone: http://localhost:3000/eureka/ 注冊中心服務(wù)端的注冊地址
prefer-ip-address: true 使用ip進(jìn)行注冊
instance-id: pay-server:2000 服務(wù)注冊到注冊中心的id名
③:創(chuàng)建主配置類
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class UserServerApplication
{
public static void main( String[] args )
{
SpringApplication.run(UserServerApplication.class);
}
}
@EnableDiscoveryClient 開啟服務(wù)發(fā)現(xiàn),開啟注冊中心客戶端功能
@EnableEurekaClient 開啟EurekaClient客戶端功能,只在Eureka下可用
這兩個(gè)注解可以不加,也是可以的
@SpringBootApplication
public class UserServerApplication
{
public static void main( String[] args )
{
SpringApplication.run(UserServerApplication.class);
}
}
4.在父項(xiàng)目創(chuàng)建一個(gè)模塊作為支付服務(wù)
配置同用戶服務(wù)一樣!
8. Eureka的集群
為了達(dá)到高可用,需要對Eureka做集群,這里在一臺(tái)電腦上模擬兩個(gè)Eureka注冊中心!
Eureka-A會(huì)在Eureka-B中注冊,同時(shí)Eureka-B也會(huì)在Eureka-A中注冊
兩個(gè)Eureka注冊中心的地址都是127.0.0.1
- 修改host文件
C:\Windows\System32\drivers\etc\hosts 右擊屬性--安全--編輯--添加讀寫權(quán)限!
127.0.0.1 peer1
127.0.0.1 peer2
- 修改eureka注冊中心的配置application.yml,這里使用多環(huán)境的方式配置,在啟動(dòng)完第一個(gè)環(huán)境,切換到第二個(gè)環(huán)境,再啟動(dòng)!
#使用SpringBoot多環(huán)境配置的方式來配置 2個(gè) 注冊中心
#主配置
spring:
profiles:
active: peer1
---
#第一個(gè)EurekaServer的配置
spring:
profiles: peer1
application:
name: eureka-server
eureka:
instance:
hostname: peer1
prefer-ip-address: true
instance-id: eureka-server:3000
client:
serviceUrl:
defaultZone: http://peer2:3001/eureka/
server:
port: 3000
---
#第二個(gè)EurekaServer的配置
spring:
profiles: peer2
application:
name: eureka-server
eureka:
instance:
hostname: peer2
prefer-ip-address: true
instance-id: eureka-server:3001
client:
serviceUrl:
defaultZone: http://peer1:3000/eureka/
server:
port: 3001
兩個(gè)eureka互相注冊,peer1的注冊中心地址應(yīng)該是peer2,peer2的注冊中心地址是peer1!
設(shè)置運(yùn)行方式為并行,啟動(dòng)主配置類,然后切換環(huán)境為peer2,再運(yùn)行,啟動(dòng)用戶服務(wù)和支付服務(wù),訪問 http://localhost:3000/,在application中可以看到已經(jīng)注冊的服務(wù)!