搭建SpringcloudConfig中的configClient端,從configServer端獲取數據

configServer端在上一篇博客已經搭好,搭建configServer

創建一個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.RELEASE</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>
  

創建一個bootstrap.yml 配置文件

spring:application:name需要填git中配置文件的名稱,環境名稱也是一樣

spring:
  application:
    ####註冊中心應用名稱
    name:  test-configClient
  cloud:
    config:
    ####環境名稱(後綴)
      profile: sit
      ####讀取config-server註冊地址
      discovery:
        service-id: config-server
        ###開啓讀取
        enabled: true
#####    eureka服務註冊地址    
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka    
server:
  port: 8882

創建一個測試類

package com.vhukze.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	@Value("${name}")
	private String name;

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

@Value中填git中配置文件中屬性的名稱。

創建啓動類,開啓Eureka     @EnableEurekaClient

啓動Eureka,啓動ConfigServer,啓動ConfigClient

訪問localhost:8882/getInfo

發佈了68 篇原創文章 · 獲贊 36 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章