一、什麼是Zuul
zuul包含了對請求的路由和過濾兩個最主要的功能:
其中路由功能負責將外部請求轉發到劇透的微服務實例上,是實現外部訪問統一入口的基礎而過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎,Zuul和Eureka進行整合,將Zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的訪問微服務都是通過Zuul轉發後獲得。
提供代理+路由+過濾的功能
1.新建模塊microservicecloud-zuul-gateway-9527,並修改pom
<dependency>
<groupId>org.springframeword.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframeword.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
2.修改yml
server:
port:9527
spring:
application:
name:microservicecloud-zuul-gateway
eureka:
client:
service-url:
defaultZone:http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-Id:gateway-9527.com
prefer-ip-address:true
info:
app.name:atguigu-microcloud
company.name:www.atguigu.com
build.artifactId:$project.artifactId$
build.version:$project.version$
3.修改host
127.0.0.1 myzuul.com
4.修改主啓動類
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp{
public static void main(String[] args){
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class,args);
}
}
5.啓動服務,訪問http://myzuul.com:9527/microservicecloud-dept/dept/get/2
二、zuul路由訪問映射規則
1.修改yml
zuul:
routes:
mydept.serviceId:microservicecloud-dept
mydept.path:/mydept/**
訪問http://myzuul.com:9527/mydept/dept/get/2
此時原路徑也可以訪問新路徑也可以訪問
2.原真實服務名忽略,修改yml文件
zuul:
ignored-services:microservicecloud-dept
routes:
mydept.serviceId:microservicecloud-dept
mydept.path:/mydept/**
使用ignored-services:"*"可以禁用所有的原服務名
三、SpringCloudConfig分佈式配置中心
微服務意味着將單個應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置信息才能運行,所有一套集中式的、動態的配置管理設施必不可少。SpringCloud提供了ConfigServer來解決這個問題,我們每一個微服務自己帶着一個application.yml,上百配置文件管理極其不便。
SpringCloudConfig爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一箇中心化的外部配置。
SpringCloudConfig分爲服務端和客戶端兩部分。
服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口
客戶端則是通過制定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方法的管理和訪問配置內容
1.先用自己的Github賬號在上面新建一個名爲microservicecloud-config的新Repository
2.由上一部獲取SHH協議的git地址
3.本地硬盤目錄上新建git倉庫並clone
4.直接在clone下來的目錄裏新建一個yml文件(必須使用utf-8格式保存)
spring:
profiles:
active:
-dev
---
spring:
profiles:dev #開發環境
application:
name:microservicecloud-config-atguigu-dev
--
spring:
profiles:test #測試環境
application:
name:microservicevloud-config-atguigu-test
5.將yml推送上github
6.新建模塊microservicecloud-config-3344,修改pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
7.修改yml
server:
port:3344
spring:
application:
name:microservicecloud-config
cloud:
config:
server:
git:
url:[email protected]:xxxx/microservicecloud-config.git #GitHub上面的git倉庫名字
8.修改主啓動類
@SpringBootApplication
@EnableConfigServer
public class config_3344_StartSpringCloudApp{
public static void main(String[] args){
SpringApplication.run(Config_3344_StartSpringCloudApp.class,args);
}
}
9.修改host
127.0.0.1 config-3344.com
10.啓動config微服務
訪問http://config-3344.com:3344/application-dev.yml
訪問http://config-3344.com:3344/application-test.yml
訪問http://config-3344.com:3344/application-qas.yml(不存在的配置)
四、SpringCloudConfig配置讀取規則
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
五、SpringCloudConfig與客戶端連接
1.新建microservicecloud-config-client.yml文件(utf-8格式!!!)
spring:
profiles:
active:
-dev
---
server:
port:8201
spring:
profiles:dev
application:
name:microserviceCloud-config-client
eureka:
client:
service-url:
defaultZone:http://eureka-dev.com:7001/eureka/
---
server:
port:8202
spring:
profiles:test
application:
name:microservicecloud-confing-client
eureka:
client:
service-url:
defaultZone:http://eureka-test.com:7001/eureka/
2.文件提交github
3.新建模塊microservicecloud-config-3355,修改pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
4.新建bootstrap.yml
application.yml是用戶級的資源配置項
bootstrap.yml是系統級的,優先級更高
SpringCloud會創建一個“BootstrapContext”,作爲Spring應用的“ApplicationContext”的父上下文。初始化的時候,“BootStrapContext”負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的“Environment”。
“Bootstrap”屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。“BootStrapContext”和“ApplicationContext”有着不同的約定,所以新增一個“bootstrap.yml”文件,保證“BootstrapContext”和“ApplicationContext”配置的分離
spring:
cloud:
name:microservicecloud-config-client #需要從github上讀取的資源文件名,注意沒有yml後綴
profile:dev #本次訪問的配置項
label:master
url:http://config-3344.com:3344 #本微服務啓動後先去找3344號訪服務,通過SpringCloudConfig獲取github上的服務地址
5.新建application.yml
spring:
application:
name:microservicecloud-config-client
6.修改host
127.0.0.1 client-config.com
7.新建類
@RestController
public class ConfigClientRest{
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServce;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig(){
String str = "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port;
System.out.println(str);
return str;
}
}
8.啓動3344,3355服務,訪問3355的/config方法