實際項目中微服務項目比較多,爲了方便就須要配置文件集中管理,接下來寫一下分佈式配置中心的實現,說明:由於比較簡單,這裏直接就講高可用的配置中心,再也不分文講解java
新建一個spring boot項目:spring
選擇Config Server 和 Eureka Server: apache
完整Pom.xml以下:bootstrap
<?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.mayi.springcloud</groupId> <artifactId>commonservice-config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>commonservice-config</name> <description>配置中心</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>Finchley.M9</spring-cloud.version> </properties> <dependencies> <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.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> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
這裏,咱們使用 本地 方式來實現配置中心segmentfault
bootstrap.yml微信
spring: application: name: service-config cloud: config: server: native: search-locations: - classpath:/shared/ profiles: active: - native server: port: 8888
解釋 :架構
要開啓一個註冊中心很是簡單,只須要修改啓動類,加入@EnableConfigServer註解便可app
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class CommonserviceConfigApplication { public static void main(String[] args) { SpringApplication.run(CommonserviceConfigApplication.class, args); } }
以spring-cloud 2.0 微服務架構從入門到精通【三】服務提供者/服務消費者(ribbon)maven
這篇文章中的service-user微服務爲例:分佈式
首先pom.xml中添加配置依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
修改service-user 的 bootstrap.yml
spring: application: name: service-user cloud: config: discovery: enabled: true service-id: service-config profile: dev label: master
解釋 :
service-id : 指向配置中心的 微服務名,這樣就實現了高可用
profile: 此配置爲了方便 開發、測試、線上環境的配置文件的切換
在service-config 配置中心項目中的shared目錄下添加service-user-dev.yml
server: port: 8801
注意:此配置文件的名稱須要遵照service-user的application.name + 中劃線 + profile 約定,不然沒法讀取
添加後目錄以下:
依次啓動 Eureka Server ---- service config ---- service-user
輸入:http://localhost:8761/
發現service-user 端口中爲8801 由上文可知,該端口的配置是從配置中心讀取的
接下來,我會依次更新文章,直至整個架構完成,若有興趣的朋友關注做者 或 加我微信 拉你進入spring cloud社區羣
微信公衆號:java架構師修行
本公衆號從2018-5.1日 - 2019.5.1日期間,將要按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習