概述java
1.1mysql
能幹嗎git
路由、過濾github
路由基本配置web
POMspring
<dependencies> <!-- zuul路由網關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- actuator監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix容錯 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 平常標配 --> <dependency> <groupId>com.wby.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
YMLsql
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 zuul: #ignored-services: microservicecloud-dept prefix: /wby ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** info: app.name: wby-microcloud company.name: www.wby.com build.artifactId: $project.artifactId$ build.version: $project.version$
HOST修改apache
C:\Windows\System32\drivers\etcbootstrap
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com 127.0.0.1 user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com
主啓動類windows
@SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } }
啓動
測試
不用路由
啓用路由
路由訪問映射規則
代理名稱
使用代理名稱的地址,可正確獲取到數據
此時的問題:使用原名稱依然能夠獲取數據,違背單入口單出口的原理,不合理。
原真實服務名忽略
忽略單個寫具體名稱,多可能夠用"*"
設置統一公共前綴:prefix
zuul: #ignored-services: microservicecloud-dept prefix: /wby ignored-services: "*" #忽略原真實單個微服務名字寫微服務名字便可,忽略多個微服務名字寫「*」 routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
最後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 zuul: #ignored-services: microservicecloud-dept prefix: /wby ignored-services: "*" #忽略原真實單個微服務名字寫微服務名字便可,忽略多個微服務名字寫「*」 routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** info: app.name: wby-microcloud company.name: www.wby.com build.artifactId: $project.artifactId$ build.version: $project.version$
概述
分佈式系統面臨的問題
是什麼
能幹嗎
與github整合配置
SpringCloud Config服務端配置
在github上新建名爲microservicecloud-config的新的Repository
由上一步得到SSH協議的git地址
本地硬盤目錄上新建git倉庫並clone
在本地microservicecloud-config項目裏面新建一個application.yml,切記!!!以UTF-8保存!!
spring: profiles: active: - dev --- spring: profiles: dev #開發環境 application: name: microservicecloud-config-wby-dev --- spring: profiles: test #測試環境 application: name: microservicecloud-config-wby-test #切記保存爲UTF-8格式
將上一步的YML文件推送到github上
Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ pwd ###顯示當前文件夾 /e/gitRepository/microservicecloud-config Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git status ###git倉庫狀態 On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) application.yml ###紅色,表示發生了彼岸花 nothing added to commit but untracked files present (use "git add" to track) Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git add . ###提交修改和提交新文件是同樣的兩步,第一步是git add Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git commit -m "init file" ###提交修改和提交新文件是同樣的兩步,第二步是git commit,""中的是提交的註釋 [master (root-commit) 681a2a1] init file 1 file changed, 15 insertions(+) create mode 100644 application.yml Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git push origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 377 bytes | 377.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/justtimo/microservicecloud-config.git * [new branch] master -> master Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $
新建Module模塊microservicecloud-config-3344,他就是Cloud的配置中心模塊
POM
<dependencies> <!-- springCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.10.0.201712302008-r</version> </dependency> <!-- 圖形化監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔斷 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
YML
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: git@github.com:justtimo/microservicecloud-config.git #GitHub上面的git倉庫名字
主啓動類config_3344_startSpringCloudApp
package com.wby.springcoud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class Config_3344_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Config_3344_StartSpringCloudApp.class, args); } }
windows下修改hosts文件,增長映射
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com 127.0.0.1 user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com config-3344.com
測試經過config微服務是否能夠從github上獲取配置內容
配置讀取規則
官網
第二種已經試過了
第一種:
第三種:
成功實現了用springcloud config經過github獲取配置信息
SpringCloud Config客戶端配置與測試
在本地E:\gitRepository\microservicecloud-config下新建文件microservicecloud-config-client.yml
microservicecloud-config-client.yml內容
將上一步提交到github上
Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git status On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) microservicecloud-config-client.yml nothing added to commit but untracked files present (use "git add" to track) Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git add Nothing specified, nothing added. Maybe you wanted to say 'git add .'? Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git add . Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git commit -m "test config" [master eaaf5b6] test config 1 file changed, 27 insertions(+) create mode 100644 microservicecloud-config-client.yml Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $ git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 533 bytes | 533.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/justtimo/microservicecloud-config.git 681a2a1..eaaf5b6 master -> master Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master) $
新建microservicecloud-config-client-3355
POM
<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> <parent> <groupId>com.wby.springcloud</groupId> <artifactId>microservicecloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-config-client-3355</artifactId> <dependencies> <!-- SpringCloud Config客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
bootstrap.yml
spring: cloud: config: name: microservicecloud-config-client #須要從github上讀取的資源名稱,注意沒有yml後綴名 profile: test #本次訪問的配置項 label: master uri: http://config-3344.com:3344 #本微服務啓動後先去找3344號服務,經過3344號服務的SpringCloudConfig獲取GitHub的服務地址
application.yml
spring: application: name: microservicecloud-config-client
windows下修改hosts文件,增長映射
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com 127.0.0.1 user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com config-3344.com client-config.com
新建rest類,驗證是否能從github上讀取配置
package com.wby.springcloud.rest; @RestController public class ConfigClientRest { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @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: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; } }
主啓動類ConfigClient_3355_StartSpringCloudApp
package com.wby.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient_3355_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args); } }
測試
啓動3344並自測:http://config-3344.com:3344/application-dev.yml
profile的值:
成功實現了客戶端3355訪問SpringCloud Config3344經過GitHub獲取配置信息
SpringCloud Config配置實戰
目前狀況
Git配置文件本地配置
microservicecloud-config-dept-client.yml
spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB03 username: root password: wby6225104 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.my.springcloud.entites mapper-locations: - classpath:mybatis/mapper/**/*.xml eureka: client: #客戶端註冊進eureka服務列表內 service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: dept-8001.com prefer-ip-address: true info: app.name: my-microservicecloud-springcloudconfig03-java1129 company.name: www.my.com build.artifactId: $project.artifactId$ build.version: $project.version$ --- server: port: 8001 spring: profiles: test application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB02 username: root password: wby6225104 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.my.springcloud.entites mapper-locations: - classpath:mybatis/mapper/**/*.xml eureka: client: #客戶端註冊進eureka服務列表內 service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: dept-8001.com prefer-ip-address: true info: app.name: my-microservicecloud-springcloudconfig02 company.name: www.my.com build.artifactId: $project.artifactId$ build.version: $project.version$
microservicecloud-config-eureka-client.yml
spring: profiles: active: - dev --- server: port: 7001 #註冊中心佔用7001端口,冒號後面必需要有空格 spring: profiles: dev application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001.com #冒號後面必需要有空格 client: register-with-eureka: false #當前的eureka-server本身不註冊進服務列表中 fetch-registry: false #不經過eureka獲取註冊信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/ --- server: port: 7001 #註冊中心佔用7001端口,冒號後面必需要有空格 spring: profiles: test application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001.com #冒號後面必需要有空格 client: register-with-eureka: false #當前的eureka-server本身不註冊進服務列表中 fetch-registry: false #不經過eureka獲取註冊信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/
Config版的eureka服務端
pom
<dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
bootstrap.yml
spring: cloud: config: name: microservicecloud-config-eureka-client #須要從github上讀取的資源名稱,注意沒有yml後綴名 profile: dev label: master uri: http://config-3344.com:3344 #SpringCloudConfig獲取的服務地址
application.yml
spring: application: name: microservicecloud-config-eureka-client
Config_Git_EurekaServerApplication.java
package com.wby.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // EurekaServer服務器端啓動類,接受其它微服務註冊進來 public class Config_Git_EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(Config_Git_EurekaServerApplication.class, args); } }
測試
Config版的dept微服務
Config版的dept微服務
配置說明
啓動3344和700一、8001
啓動後
test鏈接的是2號庫
測試經過。
正式測試經過github方式:
啓動334四、700一、8001
總結