Probe into SpringCloud Alibaba-Nacos

Written at the beginning, this part of the blog is to record the learning process of the self-study project, not a detailed technical point research.

Reference documents

关于Nacos
Nacos Config Example
Nacos Discovery Example

SpringCloud Alibaba - Nacos

Registry (service discovery, registration)

  • Introduce nacos-discoverydependencies

    <dependency>
    	<groupId>com.alibaba.cloud</groupId>
    	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    
  • Download nacos-server and start it after decompression:startup.cmd
    Insert picture description here

  • Add @EnableDiscoveryClientannotations to the service startup class application

    @MapperScan(value = "com.bcxtm.gulimall.product.dao")
    @EnableDiscoveryClient
    @SpringBootApplication
    public class GulimallProductApplication {
          
          
    
        public static void main(String[] args) {
          
          
            SpringApplication.run(GulimallProductApplication.class, args);
        }
    }
    
  • Configuration file configuration service registration server and service name

    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
      application:
        name: gulimall-product
    
  • Start the service, visit the browser 127.0.0.1:8848, log in to nacos, and view the service registration information. (User name and password default: nacos)
    Insert picture description here
    Configuration Center (Dynamic Configuration Management)

  • Introduce nacos-configdependencies

    <dependency>
    	<groupId>com.alibaba.cloud</groupId>
    	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    
  • Create a /resource/bootstrap.propertiesconfiguration file and configure Nacos Config metadata

    spring.application.name=gulimall-coupon
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    
  • Add configuration through nacos configuration center

    It should be noted that after the dependency is introduced, the default configuration rules will be printed on the console when the service starts

    c.a.c.n.c.NacosPropertySourceBuilder     : 
    Ignore the empty nacos configuration and get it based on dataId[gulimall-coupon.properties] & group[DEFAULT_GROUP]
    

    You can find: the default rule is: gulimall-coupon.properties; the group is: DEFAULT_GROUP

    In nacos configuration center -> configuration management -> configuration list, add configuration:
    Insert picture description here

  • In resource/application.propertiesthe Add userNameand userAgedata.

    userName=BCXTM
    userAge=24
    
  • After completing the above two steps, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of the Spring Environment. Here we use @Valueannotations to inject the corresponding configuration of the Controller userNameand userAgefield, and add @RefreshScopeopen dynamic refresh function.

    @RestController
    @RefreshScope
    @RequestMapping("coupon/coupon")
    public class CouponController {
          
          
    
        @Value("${userName}")
        private String userName;
    
        @Value("${userAge}")
        private String userAge;
    
        @RequestMapping(value = "/test")
        public R test() {
          
          
            return new R().put("userName", userName).put("userAge", userAge);
        }
    }
    
  • Start the project, use the postmansend request to view the returned result

    {
          
          
        "msg": "success",
        "code": 0,
        "userName": "Bcxtm",
        "userAge": "24"
    }
    
  • nacosModify the configuration information in the configuration center and click confirm to publish
    Insert picture description hereInsert picture description here

  • Request the interface again and view the result

    {
          
          
        "msg": "success",
        "code": 0,
        "userName": "bcxtm666",
        "userAge": "24666"
    }
    
  • As you can see on the service console , the two data in the configuration have been refreshed

    o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [userName, userAge]
    

Guess you like

Origin blog.csdn.net/Nerver_77/article/details/107453634