springcloud之config组件在本地配置中心的使用

1、配置中心service-config的pom核心依赖

                <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>


2、Application主应用的配置

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigApplication.class, args);
	}
}


3、application.yml文件的配置

server:
  port: 8005


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:properties/ # 搜索src/main/resource 下的properties文件夹下的文件
  application:
    name: service-config
  profiles:
    active: native  # 配置使用本地储存


注意:这里properties下的文件命名格式为 客户端的spring.application.name 加上profile后缀

URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties


4、service-config-client-base中的配置

from=default-test123


5、启动后可以通过url按照上面的映射方式访问到对应的properties 



6、把配置信息应用到客户端中,这里创建了一个application name为service-config-client的项目,核心依赖

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>


7、application中的代码

@RestController
@EnableDiscoveryClient  // 在注册中心发现服务
@SpringBootApplication
public class SpringCloudConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigClientApplication.class, args);
	}

	@Value("${from}")  // 从对应的配置中心找到文件并把属性注入到value值中
	private String value;

	@RequestMapping("/hello")
	public String hello() {
		return "hello" + value;
	}
}

8、这里使用bootstrap.yml文件,该文件会先于application.yml被加载,spring-cloud相关的属性必须配置在bootstrap.properties中,因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。例如上面的defaultZone如果不配置,则找不到service-id,会导致启动失败。

server:
  port: 8006
spring:
  application:
    name: service-config-client
  cloud:
    config:
      discovery:
        enabled: true # 通过服务发现的方式去找配置中心
        service-id: service-config # 配置中心的名字,直接配置名称可以在配置中心集群的时候实现负载均衡
      profile: base # 对应配置中心文件的${profile}部分

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
  instance:
    prefer-ip-address: true  # 使用ip地址注册到eureka server
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 在eureka server中看到的status显示为具体的ip地址和port



9、启动好服务,然后在注册中心看服务是否都已经成功注册到eureka server中,访问浏览器



代码:github

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值