SpringCloud git資源倉庫用法和Zuul路由
阿新 • • 發佈:2018-12-11
網址:https://github.com/
配置中心
原理:
github上的配置,名為School-dev.yml
spring:
application:
name: userClient
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.0.250/school
username: root
password: ps123456
server:
port: 80
eureka:
instance:
hostname: localhost
#註冊到註冊中心的微服務都是IP地址
preferIpAddress: true
client:
#是否註冊到註冊中心
registerWithEureka: true
#是否抓取註冊中心的註冊資訊
fetchRegistry: true
serviceUrl:
#微服務和客戶端用來註冊和發現的地址,此處為linux虛擬機器執行地址
defaultZone: http://192.168.174.129:8761/eureka/
application.yml 配置中心服務引用github倉庫(自己建立的)
spring:
cloud:
config:
server:
git:
uri: https://github.com/qq1073674233/ConfigCenter #github路徑
server:
port: 8888 #不指定埠,預設為8080
###配置中心依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server< /artifactId>
</dependency>
讀取資源的兩種型別均可:yml,properties
URL:http://localhost:8888/School-dev.yml
URL:http://localhost:8888/School-dev.properties
application.name錯誤
spring:
application:
name: Schoo
github上名字為:School-dev.yml
應為 School
2018-12-04 00:11:23.183 WARN 7804 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource
[org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource' defined in class path resource
錯誤:服務啟動時間早於註冊中心
Cannot execute request on any known server
2018-12-04 08:11:31.965 WARN 5796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient:
DiscoveryClient_USERSERVICE/OuYang:userService:8881 - registration failed Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
預設訪問方式:http://localhost:8888/School/dev,讀取的是properties格式
微服務啟動前配置bootstrap.yml
#註冊中心的位置在IP和埠
spring:
application:
name: School #github已定義的專案名
profiles:
active: dev #github已定義的名字,推薦:開發環境dev,測試環境test,生產環境,gene
cloud:
config:
uri: http://localhost:8888
#該檔案等同於http://localhost:8888/School-dev.yml
#此檔案需新增spring-cloud-starter-config依賴才會讀取
讀取bootstrap.yml檔案的依賴
<dependency> <!--此依賴才可讀取配置資源bootstrap.yml-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
main類添加註解 @EnableConfigServer
異常:loc header execption 一定是jar包損壞
zuul
路由配置application.yml , 均衡配置客戶端,程式訪問的入口
spring:
application:
name: zuul
server:
port: 80
eureka:
instance:
hostname: localhost
#註冊到註冊中心的微服務都是IP地址
preferIpAddress: true
client:
#是否註冊到註冊中心
registerWithEureka: false
#是否抓取註冊中心的註冊資訊
fetchRegistry: true
serviceUrl:
#微服務和客戶端用來註冊和發現的地址
defaultZone: http://192.168.174.129:8761/eureka/