【Spring Cloud】小型專案的搭建日記:Spring Cloud Config的搭建
阿新 • • 發佈:2018-12-10
閒敘一下
本來我自己有打算寫一個Spring Cloud相關知識的部落格(本人技術很渣,小吹一下,嗯…),然後看到網上已經有很多人都在發相關的知識了,我就不再贅述了,首先不浪費自己的時間,也不浪費大家的時間,看著的都是“千篇一律”的文章,心裡也不舒服啊。
我現在的工作多是運維相關,碼程式碼的機會也少了,加上最近在看python,Java就基本忘得差不多了,然後在一個朋友(做前端開發的)的邀請下,準備合作一個小的專案(純是練手的,大牛勿噴!!!),所以今天就動手搭了一下Spring Cloud的配置中心(Config)
步入正題
首先,我是使用IDEA來建立專案的(用過的都清楚怎麼來建立一個Spring Boot專案),我就不在這裡多說了,配置中心我放在了GitHub上,現在開始:
config server
application.yml的配置
server:
port: 埠號
spring:
application:
name: 應用的名稱(對於eureka而言的serviceId)
cloud:
config:
server:
git:
search-paths: 分支下存放配置檔案的資料夾名
uri: 你的配置中心所在的路徑(https://github.com/使用者名稱/專案名)
label: master(分支名稱)
#eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/(預設地址)
#management(配置中心的自動重新整理)
management:
endpoints:
web:
exposure:
include: bus-refresh
如果上面的專案的是private(私有)的,那麼需要加上 spring.cloud.config.server.git.username spring.cloud.config.server.git.password
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
然後在啟動類加上@EnableEurekaClient、@EnableConfigServer 這兩個註解,這樣配置中心的服務端就搭好了。
config client
bootstrap.yml(這裡注意下,是bootstrap,不是application)
server:
port: 8652
spring:
application:
name: config-client
cloud:
bus:
trace:
enabled: true
config:
uri: http://localhost:8651/
label: master
profile: dev
name: config-client
discovery:
enabled: true
serviceId: config-server
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#management.security.enabled=false
management:
endpoints:
web:
exposure:
include: bus-refresh
這裡我用的是rabbitmq 來實現配置的同步。
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然後在啟動類上加上@RefreshScope、@EnableEurekaClient 這兩個註解,配置中心的客戶端也搭好了。
這個我最早是在方誌朋的部落格裡發現的。(我也來一張圖)
最後,希望我能堅持下去,把這個練手的專案跟完,也非常感謝能看到這裡的人。