Springcloud 集成 Nacos配置中心

LINUX NACOS 快速启动

Springcloud 集成 Nacos注册中心_Ocean@上源码的博客-CSDN博客

1)nacos添加配置(Data ID 与 application:name 对应)

Springcloud 集成 Nacos配置中心_第1张图片

2)pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.lhy
    cloud-api
    0.0.1-SNAPSHOT
    cloud-api
    cloud-api
    
        1.8
        2.2.2.RELEASE
        2.2.1.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            ${spring-nacos.version}
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            ${spring-nacos.version}
        
        
        
            org.springframework.cloud
            spring-cloud-commons
            ${spring-cloud.version}
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3)bootstrap.yml

server:
  port: 12315

spring:
  application:
    name: cloud-api
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml

核心配置

application:name:cloud-api              Data ID 名称
server-addr: 127.0.0.1:8848               配置中心地址
file-extension: yml                               配置文件格式        

4)启动类

package com.lhy.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudApiApplication {

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

}

5)测试类

package com.lhy.cloud.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Class description.
 *
 * @author ocean liu
 * @createTime 2021/11/29 - 10:57
 */
@RequestMapping("/user")
@RestController
@RefreshScope // 支持动态更新
public class UserController {

    @Value("${user.headPortrait:123.png}")
    private String headPortrait;

    @GetMapping("/headPortrait")
    public String headPortrait(){
        return headPortrait;
    }

}

@RefreshScope                                             支持动态更新
@Value("${user.headPortrait:123.png}")        默认123.png

6)验证

Springcloud 集成 Nacos配置中心_第2张图片

你可能感兴趣的:(框架集成,springcloud,Java,java,spring,cloud)