Spring Cloud 2020 config 配置 Spring Boot 2.4.1


前言

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 战士能有什么坏心思呢。


感谢大家的一键三连 ======》 谢谢。


完 2021.01.27 18:38

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值