您好,登錄后才能下訂單哦!
首先我搭建了一個(gè)Eureka 注冊(cè)中心,這里就不著重介紹了,不知道的小伙伴可以
網(wǎng)上查資料!
1.搭建Config 配置中心
POM 文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sg.config</groupId>
<artifactId>config-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</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-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</project>
這里面Config用到的依賴(lài)是 spring-cloud-config-server ,其他自己看著引入即可
2.application.yml 的配置
server:
port: 8095
spring:
application:
name: config-demo
cloud:
config:
server:
git:
uri: https://github.com/****/spring-cloud-demo.git
username:
password:
search-paths: spring-cloud-demo-config
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8090/eureka/
register-with-eureka: true
fetch-registry: true
Config Server 的配置主要是spring.cloud.config.server.git 下面的東西,其他按需配置即可。
3.Config Server 啟動(dòng)類(lèi)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigDemoApplication.class, args);
}
}
至此 Config Server 端就配置完成了。
4.配置Config Client 端
首先引入依賴(lài)
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sg.eureka.client</groupId>
<artifactId>eureka-client-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sg.common</groupId>
<artifactId>spring-cloud-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</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>
</project>
主要有關(guān)的依賴(lài)是 spring-cloud-starter-config ,其他的按需引入即可
5.application.yml 配置文件
server:
port: 8091
spring:
application:
name: eureka-client-demo
cloud:
config:
profile: test
uri: http://127.0.0.1:8095/
label: master
discovery:
enabled: true
service-id: config-demo
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8090/eureka/
### 端點(diǎn)控制
management:
endpoints:
web:
exposure:
# 開(kāi)啟指定端點(diǎn)
include: hystrix.stream
project:
name: hahha
啟動(dòng)類(lèi)上什么配置也不用添加,就可以了,重要的一點(diǎn):
1:如果用了Eureka ,則需要配置 spring.cloud.config.discovery.enable: true 和 spring.cloud.config.discovery.service-id: config-demo 這兩個(gè)屬性
6.讀取Server中配置的屬性
package com.sg.eureka.client.controller;
import com.sg.common.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
/**
* @author liuhailong
* @date 2019-08-08
*/
@RestController
@RequestMapping(value = "users")
@Configuration
public class UserController {
@Autowired
private Environment environment;
@Value("${project.name}")
private String name;
@GetMapping(value = "/config")
public String getConfig(@RequestParam("key")String key){
return environment.getProperty(key);
}
@GetMapping(value = "/projectName")
public String projectName(){
return name;
}
}
我這里用了 兩種方式去讀取配置文件中的內(nèi)容
1:使用Spring core中的Environment 類(lèi) 中的getProperty 可以取到
2:使用Spring 的 @Value("${project.name}") 注解
6.接下來(lái)驗(yàn)證一下:訪問(wèn) http://localhost:8091/users/config?key=project.name 結(jié)果發(fā)現(xiàn)獲取不到Config Server中配置的參數(shù)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。