nacos配置中心

首先配置maven,添加spring-cloud-starter-alibaba-nacos-config


	
		org.springframework.boot
		spring-boot-starter-web
	
    
        com.alibaba.cloud
	    spring-cloud-starter-alibaba-nacos-config
		    
    



	
		
	        com.alibaba.cloud
	        spring-cloud-alibaba-dependencies
	        2023.0.3.2
	        pom
	        import
	    
    

配置yaml文件

server:
  port: 66
spring:
  application:
    name: order-service  # 服务名
  cloud:
    nacos:
      server-addr: localhost:8848  # 配置中心地址
      discovery:
        server-addr: localhost:8848  # 注册中心地址
  config:
    import:
    - nacos:order-service-dev  # 配置中心的配置项的dataId

不需要OrderServiceApplication里写任何代码

最后在controller里加入@RefreshScope注解

package com.xy.order;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.api.config.annotation.NacosValue;

@RefreshScope
@RestController
public class HelloController {
	
	@Value(value = "${aa:nothing}")
	private String orderNumber;

	
	@GetMapping("/order/hello")
	public String hello(@RequestParam String name) {
		System.out.println("..........." + orderNumber);

		String res = "....." + name + "......." + orderNumber;
		return res;
	}
}

这样就可以读取到配置信息了。不给如果SpringBoot的配置信息是写在bootstrap.yml里的,可以实现配置信息的实时更新,如果是写在application.yml里的却不能实时更新。这个还得再研究研究

你可能感兴趣的:(java,spring,boot,spring,cloud,CloudAlibaba,nacos,微服务)