Spring Cloud 2020.0.0 Spring Boot 2.4.1 config server 和 config client 配置
技術標籤:技術spring cloudspring bootconfigclient serverjava
文章目錄
- 前言
- 2021年1月27日編輯。
- Spring Cloud Config Server 配置檔案
- Spring Cloud Config Client 配置檔案
- 版本 Spring Boot:2.4.1
- 版本 Spring Cloud:2020.0.0
- 二、正文開:正文開:正文開:正文開:正文開:
- 三、Controller 獲取配置檔案 使用值
- 一、我就直接就 C + V 別人的了
- 總結
- 感謝大家的一鍵三連 ======》 謝謝。
- 完 2021.01.27 18:38
前言
2021年1月27日編輯。
Spring Cloud Config Server 配置檔案
Spring Cloud Config Client 配置檔案
版本 Spring Boot:2.4.1
版本 Spring Cloud:2020.0.0
眨眼間這都2021年了,連Spring Cloud 都升級到 2020.0.0版本了
然鵝,C+V 戰士,戰鬥的速度得跟上。但是這版本更新才不到一個月,並麼有幾個地方可以C+V 。
so,英語渣的我只能看著英文文件和特意下載的翻譯工具硬上了,現倒騰出來一點,拿出來分享一下。
有錯誤的地方希望大佬們多加指正,反正我看到了就會改。
更希望技術大大們可以努力搞出更多的可以讓 === 》那啥啥《=== 們可以借鑑的程式碼。
正文開:
二、正文開:正文開:正文開:正文開:正文開:
一、先整 Spring Cloud Config Server
1、pom.xml
直接上程式碼,希望只C+V依賴包,其他無視就好
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ioenn</artifactId>
<groupId>com.ioenn</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.ioenn</groupId>
<artifactId>ioenn-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ioenn-config-server</name>
<description>Ioenn-Config-Server project for Spring Boot</description>
<dependencies>
<!-- spring cloud config 服務端包 -->
<!--spring cloud配置中心依賴[預設GIT]-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka client 端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2、application.yml
直接上程式碼,C+V後,看註釋,要改成自己的
server:
port: 8762
eureka:
client:
serviceUrl:
register-with-eureka: true
fetch-registry: true
defaultZone: http://localhost:8761/eureka/
instance:
preferIpAddress: true
spring:
application:
name: ioenn-config-server
# git 倉庫
cloud:
config:
server:
git:
uri: https://github.com/-------/--------- #配置檔案所在倉庫
basedir: D:\IOENN\-------\-------
default-label: master #配置檔案分支
search-paths: config #配置檔案所在根目錄
3、啟動類 ******Application.java
新增幾個註解
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class IoennConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(IoennConfigServerApplication.class, args);
}
}
好了,Spring Cloud Config Server 整完了 , 抬走,下一個。
二、Spring Cloud Config Client 配置檔案
一開始我沒注意版本,然後Config 客戶端一直取不到Config 服務端獲取到的git檔案裡配置檔案中的值,然後發現版本問題後又找不到現成的教程,只能去看官方文件。
官方文件地址 : https://spring.io/projects/spring-cloud-config
讓我下定決心去看官方文件的地址: https://blog.csdn.net/f641385712/article/details/111595426
(我內心是拒絕的,不想開荒,只想伸手。)
配置檔案程式碼:
1、pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ioenn</artifactId>
<groupId>com.ioenn</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.ioenn</groupId>
<artifactId>ioenn-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ioenn-config-client</name>
<description>Ioenn-Config-Client project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka服務發現客戶端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--官網教的,要新增這個依賴,不然找不到值-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project>
2、bootstrap.yml
這個玩楞,就是把 application.****** 改個名,但是必須得是這個名。
url 示例:
http://localhost:8762/configclient/prod
server:
port: 8763
spring:
application:
name: ioenn-config-client
cloud:
config:
name: configclient # 這裡要改成自己的
profile: prod # 這裡要改成自己的
discovery:
service-id: ioenn-config-server
enabled: true
uri: http://localhost:8762
bootstrap:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Spring Cloud Config Client 完事,抬走,下一個。
三、Controller 獲取配置檔案 使用值
別人的示例 都是 直接 Controller 裡整 。
一、我就直接就 C + V 別人的了
1.1、Controller url 示例 :
http://localhost:8763/demo/url
鼓勵原創 :Controller C + V 地址 : https://www.cnblogs.com/zuowj/p/10432445.html
他下邊有 github地址,github上是:
版本 Spring Boot: 2.1.3.RELEASE
版本 Spring Cloud: Greenwich.RELEASE
的 配置中心 ,可以直接借鑑的。
6、Controller.java
package com.ioenn.ioennconfigclient.controller;
import com.ioenn.ioennconfigclient.model.RemoteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/demo")
public class IoennController extends RemoteConfig {
@Autowired
private Environment environment;
@Value("${demo-config-profile-env}")
public String profileEnv;
@Value("${zuowenjun.site}")
public String zwjSite;
@Value("${zuowenjun.skills}")
public String zwjSkills;
@Value("${zuowenjun.motto}")
public String zwjMotto;
@RequestMapping("/url")
public String url(){
System.out.println("url+ profileEnv : "+profileEnv);
System.out.println("url+ zwjSite : "+zwjSite);
System.out.println("url+ zwjSkills : "+zwjSkills);
System.out.println("url+ zwjMotto : "+zwjMotto);
return zwjSite;
}
@RequestMapping("/map")
public Object getRemoteConfigByValue(){
Map<String,Object> model=new HashMap<>();
model.put("getMode","@Value");
Map<String,String> remoteCgfMap=new HashMap<>();
remoteCgfMap.put("profileEnv", this.profileEnv);
remoteCgfMap.put("zwjSite", this.zwjSite);
remoteCgfMap.put("zwjSkills",this.zwjSkills);
remoteCgfMap.put("zwjMotto", this.zwjMotto);
System.out.println("map+ profileEnv : "+this.profileEnv);
System.out.println("map+ zwjSite : "+this.zwjSite);
System.out.println("map+ zwjSkills : "+this.zwjSkills);
System.out.println("map+ zwjMotto : "+this.zwjMotto);
model.put("remoteConfig",remoteCgfMap);
return model;
}
@RequestMapping("/obge")
public Object getRemoteConfigByEnv(){
Map<String,Object> model=new HashMap<>();
model.put("getMode","Environment");
Map<String,String> remoteCgfMap=new HashMap<>();
remoteCgfMap.put("profileEnv", environment.getProperty("demo-config-profile-env"));
remoteCgfMap.put("zwjSite", environment.getProperty("zuowenjun.site"));
remoteCgfMap.put("zwjSkills", environment.getProperty("zuowenjun.skills"));
remoteCgfMap.put("zwjMotto", environment.getProperty("zuowenjun.motto"));
System.out.println("obge+ profileEnv : "+ environment.getProperty("demo-config-profile-env"));
System.out.println("obge+ zwjSite : "+ environment.getProperty("zuowenjun.site"));
System.out.println("obge+ zwjSkills : "+ environment.getProperty("zuowenjun.skills"));
System.out.println("obge+ zwjMotto : "+ environment.getProperty("zuowenjun.motto"));
model.put("remoteConfig",remoteCgfMap);
return model;
}
}
2、莫得了
3、model.RemoteConfig.java
package com.ioenn.ioennconfigclient.model;
import org.springframework.beans.factory.annotation.Value;
public abstract class RemoteConfig {
@Value("${demo-config-profile-env}")
public String profileEnv;
@Value("${zuowenjun.site}")
public String zwjSite;
@Value("${zuowenjun.skills}")
public String zwjSkills;
@Value("${zuowenjun.motto}")
public String zwjMotto;
}
總結
這回真莫得了,少沒少啥我也不知道, C+V 戰士能有什麼壞心思呢。