SpringCloud统一配置之使用配置
- 配置的读取方式在上一篇文章中有提到。
- 取到值之后赋值给静态类里的静态变量。
- 因为SpringCloud项目启动的时候会把所有的api都执行一遍(相当蛋疼),所以这里直接就可以写一个方法进行赋值。
代码如下:
入口类:
package com.shinho; import java.util.Map; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import com.alibaba.fastjson.JSON;
import com.shinho.log.ShinhoLog; @Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.shinho"})
@EnableWebMvc
@EnableEurekaClient
@RestController
public class KylintestApplication { @Value("${foo}")
String foo; private static final Logger logger = LoggerFactory.getLogger(KylintestApplication.class); public static void main(String[] args) {
SpringApplication.run(KylintestApplication.class, args);
} @RequestMapping("/log")
@Bean
public String Log() {
logger.info("init:log");
ShinhoLog.foo = this.foo;
String json = "log";
return json;
} @RequestMapping("/hi")
@Bean
public String home() {
String json = "123:"+ShinhoLog.info();
return json;
}
}
静态类:
package com.shinho.log; import java.io.File; import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component; @Component
public class ShinhoLog { public static String foo; public static String info(){
return foo;
} }
启动之后立即访问 http://localhost:XXXX/hi 这是会返回123:foo version 2, 完美!
SpringCloud统一配置之使用配置的更多相关文章
- 带你入门SpringCloud统一配置 | SpringCloud Config
前言 在微服务中众多服务的配置必然会出现相同的配置,如果配置发生变化需要修改,一个个去修改然后重启项目的方案是绝对不可取的.而 SpringCloud Config 就是一个可以帮助你实现统一配置选择 ...
- springcloud(十二)-springcloud-config统一管理微服务配置
1.为什么要统一管理微服务配置 对于传统的单体应用,常使用配置文件管理所有配置.例如一个SpringBoot开发的单体应用,可将配置内容放在application.yml文件中.如果需要切换环境,可设 ...
- SpringCloud(6)分布式配置中心Spring Cloud Config
1.Spring Cloud Config 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组 ...
- springcloud 入门 8 (config配置中心)
Spring Cloud Config: 配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config. 在分布式系统中,由于服务数量巨多, ...
- SpringCloud Zuul 路由映射规则配置
阅读目录 前言 快速入门 路由详解 Cookie与头信息 本地跳转 Hystrix和Ribbon支持 过滤器解释 动态加载 后记 回到目录 前言 本文起笔于2018-06-26周二,接了一个这周要完成 ...
- SpringCloud学习之Config分布式配置中心(八)
统一配置中心概述 如果微服务架构中没有使用统一配置中心时,所存在的问题: 配置文件分散在各个项目里,不方便维护 配置内容安全与权限,实际开发中,开发人员是不知道线上环境的配置的 更新配置后,项目需要重 ...
- Springcloud及Git线上配置详解
SpringCloud 这个阶段该如何学? 三层架构 + MVC 框架: Spring IOC AOP SpringBoot,新一代的JavaEE开发标准,自动装配 模块化~ all in one,代 ...
- 【SpringCloud】Ribbon如何自定义客户端配置和全局配置
起因 事情的起因是这样的,公司内部要实现基于Zuul网关的灰度路由,在上线时进行灰度测试,故需要配置业务微服务向Eureka注册的metadata元数据,和自定义Ribbon的负载规则达到只访问灰度服 ...
- 微服务SpringCloud之Spring Cloud Config配置中心SVN
在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...
- SpringCloud使用Consul作为分布式配置中心
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_36027670/article/de ...
随机推荐
- Android如何打印std::cout/printf(重定向stdout)
Android应用调试时没有stdout和stderr的输出,网上看到的解释都是下面这个样子: ################################################# An ...
- Python 数据类型--集合(set)
一.集合(set) 集合也是一种数据类型,一个类似列表的,无序的,不重复的.它主要有两大作用 1.把一个列表变为集合,就自动去重了,不需要写额外的代码 2.关系测试,测试两组数据之间的交际.差集.并集 ...
- SQL 查询重复的行
select * from tbsold where orderid in (select orderid from tbsold group by orderid having count(orde ...
- 在 Ubuntu 14.04 Chrome中安装Flash Player(转)
在 Ubuntu 14.04 中安装 Pepper Flash Player For Chromium 一个 Pepper Flash Player For Chromium 的安装器已经被 Ubu ...
- 手把手教你搭建一个Elasticsearch集群
一.为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? (1)高可用性 Elasticsearch 作为一个 ...
- 使用MyEclipse将HTML5移动项目迁移到PhoneGap(三)
MyEclipse开年钜惠 在线购买低至75折!立即开抢>> [MyEclipse最新版下载] 三.将图像从HTML5项目复制到PhoneGap项目 1. 在PhoneGap项目的www ...
- php 特殊字符
今天碰到一个处理文件特殊字符的事情,再次注意到这个问题,在php中: * 以单引号为定界符的php字符串,支持两个转义\'和\\ * 以双引号为定界符的php字符串,支持下列转义: \n 换行 ...
- 步步入佳境---UI入门(1)--项目建立与实现
一,本文讲解建立一个空项目,怎么一步一步的创建程序,总体的感觉一下程序流程 1,首先建立一个项目,如下:single view project,我们首先删除CHAppDelegate文件和Main. ...
- 【其他】msb-lsb-intel-motorola大小端问题
MSB(Most Significant Bit) 最高有效位: LSB(Least Significant Bit) 最低有效位 intel格式:低字节在前 Motorola格式:高字节在前 参考1 ...
- TX2-start 6 CPU kernel-开启高功耗模式
1.TX2简介 Jetson TX2是由一个GPU和一个CPU集群组成.CPU集群由双核denver2处理器和四核ARM Cortex-A57组成,通过高性能互连架构连接.拥有6个CPU核心和一个GP ...