springcloud分散式配置中心
阿新 • • 發佈:2019-05-25
一共三個部分
一、註冊中心
配置檔案:application.yml
###服務啟動埠號 server: port: 8100 ###服務名稱(服務註冊到eureka名稱) spring: application: name: server ###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka ###因為該應用為註冊中心,不會註冊自己 register-with-eureka: true ###是否需要從eureka上獲取註冊資訊 fetch-registry: true
啟動類
@EnableEurekaServer
@SpringBootApplication
public class springcloudEureka {
public static void main(String[] args) {
SpringApplication.run(springcloudEureka.class,args);
}
}
pom檔案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--SpringCloud eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
二、configServer
一、配置檔案 :application.yml
###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka spring: application: ####註冊中心應用名稱 name: config cloud: config: server: git: . ###git環境地址 uri: https://gitee.com/XXXX/testspringcloud.git ####svn專案下屬搜尋目錄 search-paths: - testspringcloud ###賬號密碼 username: XXXXX password: XXXXX ####讀取分支 label: master ####埠號 server: port: 9999
二、啟動類
@EnableConfigServer//開啟配置中心
@SpringBootApplication
@EnableEurekaClient
public class Star {
public static void main(String[] args) {
SpringApplication.run(Star.class, args);
}
}
pom檔案
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
測試有沒有配置成功瀏覽器輸出:
三、configclient
一、配置檔案 bootstrap.yml
spring:
application:
####註冊中心應用名稱
name: config
cloud:
config:
####讀取字尾
profile: sit
####讀取config-server註冊地址
discovery:
serviceId: config
enabled: true
label: master
# uri: http://localhost:9999/
##### eureka服務註冊地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
server:
port: 8882
二、啟動類
@SpringBootApplication
@EnableEurekaClient
public class Star {
public static void main(String[] args) {
SpringApplication.run(Star.class, args);
}
}
pom檔案
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web元件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>