計劃:java
內容:git
spring cloud Dalston SR3
搭建配置中心spring cloud config 1.3.2.RELEASE
版本spring cloud 1.5.3.RELEASE
版本依賴介紹微服務並非本章的重點,固然若沒有了解過微服務的,能夠先了解一下,再繼續本章的內容。github
推薦幾篇文章: 解析微服務架構(一):什麼是微服務 解析微服務架構(二):融入微服務的企業集成架構 解析微服務架構(三):微服務重構應用及IBM解決方案spring
若讀者不肯意看長篇大論抑或以爲過於抽象不能很好的理解,那麼筆者簡單的例子來介紹微服務apache
去超市,咱們發現有專門賣肉的、賣零食的、賣蔬菜的、賣酒的,各部門**
各司其職
**,不像80年代賣肉的也賣菜,而每一個部門至關於一個服務,同時也發現,超市有服務檯,能夠問酒都放哪裏,在軟件中我門能夠簡單理解爲網關,但網關還有不少事情能夠作,後面咱們再介紹bootstrap
微服務優勢:tomcat
微服務缺點:微信
先看兩個圖嘗試理解理解cookie
圖一: session
圖二:
圖一咱們理解一下: 目前config server支持從三個地方獲取配置文件,分別爲:
那很是好,咱們能夠把配置提交到版本倉庫便可
圖二咱們簡單理解下:
配置中心能夠理解成超市中的倉庫,我賣酒的,得問他我能賣些什麼酒,也就是說,在軟件中,我每一個服務得問他我有些什麼配置
也同時看得出,咱們只要把配置文件提交到配置倉庫,發送事件/鉤子,那麼服務就會隨之而去配置中心獲取文件更新,下降咱們的維護成本
接下來咱們不得不說一個事情,不然玩不轉配置中心
重點:
服務是以spring.application.name
的配置屬性來決定這個服務的id,也就是告訴配置中心,我是誰
spring.profiles.active
這是要拿哪一個配置文件,那麼這樣咱們就能夠區分多個維度或者說環境,這裏能夠是多個,能夠逗號分隔
訪問策略:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
若配置爲:
spring.application.name=sso spring.profiles.active=dev
會尋找配置文件名爲:sso-dev.properties文件,固然也能夠爲sso-dev.yml或其餘
上面囉嗦了那麼多,終於要到代碼階段了...
如圖所示以最簡單的方式獲取代碼,固然也把原始代碼放置於original-files文件夾下
因爲項目工程集中化,因此解壓拷貝到工程後須要進行簡單調整
<?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.carl.auth</groupId> <artifactId>sso-config</artifactId> <version>1.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sso-config</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <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> <!--爲了加快速度,修改爲國內的代理--> <repositories> <repository> <releases> <enabled>true</enabled> </releases> <id>maven2-release</id> <url>http://uk.maven.org/maven2/</url> </repository> <repository> <snapshots> <enabled>true</enabled> <!--快照版本庫兩個小時檢查更新一遍--> <updatePolicy>interval:120</updatePolicy> </snapshots> <id>oss-snapshots</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <enabled>true</enabled> </releases> <id>maven2-release</id> <url>http://uk.maven.org/maven2/</url> </pluginRepository> </pluginRepositories> </project>
配置管理服務服務支持
/* * 版權全部.(c)2008-2017. 卡爾科技工做室 */ package com.carl.auth.ssoconfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class SsoConfigApplication { public static void main(String[] args) { SpringApplication.run(SsoConfigApplication.class, args); } }
把application.properties修改爲application.yml,由於yml文件更直觀
該配置文件爲配置中心的項目設置
#指定日誌輸出文件 logging: file: "logs/sso-config.log" info: name : "配置中心" --- server: #服務端口 port: 8888 #訪問路徑 context-path: /config spring: profiles: #本地配置文件 active: #配置文件本地化 - native application: #指定應用名稱 name: sso-config
mvn spring-boot:run
若看到以下結果,恭喜你,配置成功
2017-09-09 15:54:44.776 INFO 10696 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshEndpoint': registering with J MX server as MBean [org.springframework.cloud.endpoint:name=refreshEndpoint,type=RefreshEndpoint] 2017-09-09 15:54:45.287 INFO 10696 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 2017-09-09 15:54:45.727 INFO 10696 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http) 2017-09-09 15:54:45.743 INFO 10696 --- [ main] c.c.auth.ssoconfig.SsoConfigApplication : Started SsoConfigApplication in 17.302 seconds (JVM runnin g for 28.779)
能夠嘗試訪問:http://localhost:8888/config
出現以下別灰心,那是正常的,不出來纔是不正常的呢。。。
到此,已經完成大部分的工程,那麼接下來是咱們的重頭戲
resources/config
接下來咱們嘗試寫配置文件在config目錄就叫sso-dev.properties
吧,意味着cas的配置應該爲:
spring.application.name=sso spring.profiles.active=dev
測試一下配置文件(sso-dev.properties 內容以下):
# # 版權全部.(c)2008-2017. 卡爾科技工做室 # hello_key=value
重啓一下配置中心 試試訪問:http://localhost:8888/config/sso/dev
結果以下,恭喜你!!!
繼續走... 把sso-dev.properties文件修改爲sso-server須要的配置(把application.properties內容拷貝過來便可)
# # 版權全部.(c)2008-2017. 卡爾科技工做室 # ## # CAS Server Context Configuration # server.context-path=/cas server.port=8443 server.ssl.enabled=false server.max-http-header-size=2097152 server.use-forward-headers=true server.connection-timeout=20000 server.error.include-stacktrace=NEVER server.tomcat.max-http-post-size=2097152 server.tomcat.basedir=build/tomcat server.tomcat.accesslog.enabled=true server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms) server.tomcat.accesslog.suffix=.log server.tomcat.max-threads=10 server.tomcat.port-header=X-Forwarded-Port server.tomcat.protocol-header=X-Forwarded-Proto server.tomcat.protocol-header-https-value=https server.tomcat.remote-ip-header=X-FORWARDED-FOR server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true ## # CAS Cloud Bus Configuration # spring.cloud.bus.enabled=false endpoints.enabled=false endpoints.sensitive=true endpoints.restart.enabled=false endpoints.shutdown.enabled=false management.security.enabled=true management.security.roles=ACTUATOR,ADMIN management.security.sessions=if_required management.context-path=/status management.add-application-context-header=false security.basic.authorize-mode=role security.basic.enabled=false security.basic.path=/cas/status/** ## # CAS Web Application Session Configuration # server.session.timeout=300 server.session.cookie.http-only=true server.session.tracking-modes=COOKIE ## # CAS Thymeleaf View Configuration # spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=true spring.thymeleaf.mode=HTML ## # CAS Log4j Configuration # # logging.config=file:/etc/cas/log4j2.xml server.context-parameters.isLog4jAutoInitializationDisabled=true ## # CAS AspectJ Configuration # spring.aop.auto=true spring.aop.proxy-target-class=true ## # CAS Authentication Credentials # cas.authn.accept.users=casuser::Mellon
再次重啓sso-config 試試訪問:http://localhost:8888/config/sso/dev
結果以下,再次恭喜你!!!
接下來是重點 在sso-server工程下做業:
bootstrap.properties:
# # 版權全部.(c)2008-2017.廣州市森銳科技股份有限公司 #指定日誌文件 logging.file=logs/cas.log info.name=單點登陸系統 #定義application.name的id spring.application.name=sso #尋找配置中心爲sso-dev.properties spring.profiles.active=dev #指定配置中心地址 spring.cloud.config.uri=http://localhost:8888/config #開啓配置中心 spring.cloud.config.enabled=true #支持自動任務去配置中心刷新配置 spring.cloud.config.watch.enabled=true #30秒刷新一次 spring.cloud.config.watch.initialDelay=30000 #請求配置中心超市 spring.cloud.config.watch.delay=1000 #檢查配置健康 health.config.enabled=true
因爲手腳架創建出來的不便於看調試信息,因此把log4j2.xml也調整一下,因爲文件行數過多,就不放出來了,須要的自行下載,主要把warn改爲info和一些debug
最後,項目的目錄結構應該以下:
運行sso-server
build.cmd run
若出現以下圖,恭喜你,運行成功
最後訪問:http://localhost:8443/cas
用戶名/密碼:casuser/Mellon
若最後能登陸成功,本章的內容你已獲得必定的收穫
若是技術的交流或者疑問能夠聯繫或者提出issue。
QQ: 756884434 (請註明:SSO-CSDN)
若是項目對你有技術上的提高、工做上的幫助或者一些啓示,不妨請小編喝杯咖啡,小編更會滿懷激情的爲你們講解和輸出博文哦。
微信 支付寶