springcloud-config分佈式配置中心

1、首先需要搭建eureka集羣。在此不多說。

github申請一個倉庫。

https://github.com/yunwenlong/springcloud-config.git

 

 

2、搭建springcloud-config需要搭建服務器(server)與客戶端(client)

服務器maven配置信息

<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>
		</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>

application.ywl配置文件

eureka:
  client:
    service-url:
           defaultZone: http://localhost:8800/eureka
spring:
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yunwenlong/springcloud-config.git
          search-paths:
            - conf
      label: master
server:
  port: 8888

uri地址爲GitHub地址

name爲配置文件前綴。

啓動類

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class AppServer {

	public static void main(String[] args) {
		SpringApplication.run(AppServer.class, args);
	}

}

到此,服務端已經完成。

下面開始搭建客戶端

maven依賴

<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>
		
		<!-- actuator監控中心、手動刷新 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</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>

application.ywl

spring:
  application:
    ####註冊中心應用名稱
    name: springcloud-config
  cloud:
    config:
    ####讀取後綴
      profile: prod
      ####讀取config-server註冊地址
      discovery:
        service-id: springcloud-config
        enabled: true
#####    eureka服務註冊地址    
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8800/eureka    
server:
  port: 8882

management:
  endpoints:
    web:
      exposure:
        include: "*"
  

 

獲取配置文件的代碼

@RestController
@RefreshScope  // 手動刷新時會重新加載配置文件
public class TestController {
	
	@Value("${configKey}")
	private String namme;

	@RequestMapping("name")
	public String name(){
		return namme;
	}
	
}

啓動類

@SpringBootApplication
@EnableEurekaClient
public class AppClient {

	public static void main(String[] args) {
		SpringApplication.run(AppClient.class, args);
	}

}

每次修改過之後,重新獲取配置文件不可以。

需要重新請求一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章