您好,登錄后才能下訂單哦!
首先我搭建了一個Eureka 注冊中心,這里就不著重介紹了,不知道的小伙伴可以
網上查資料!
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用到的依賴是 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 啟動類
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 端
首先引入依賴
<?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>
主要有關的依賴是 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/
### 端點控制
management:
endpoints:
web:
exposure:
# 開啟指定端點
include: hystrix.stream
project:
name: hahha
啟動類上什么配置也不用添加,就可以了,重要的一點:
1:如果用了Eureka ,則需要配置 spring.cloud.config.discovery.enable: true 和 spring.cloud.config.discovery.service-id: config-demo 這兩個屬性
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;
}
}
我這里用了 兩種方式去讀取配置文件中的內容
1:使用Spring core中的Environment 類 中的getProperty 可以取到
2:使用Spring 的 @Value("${project.name}") 注解
6.接下來驗證一下:訪問 http://localhost:8091/users/config?key=project.name 結果發現獲取不到Config Server中配置的參數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。