Springcloud Alibaba Nacos Registration Center and Configuration Center

  Nacos: A dynamic service discovery, configuration management and service management platform that makes it easier to build cloud-native applications.

Registry

  I wrote about the service registration and discovery component Eureka component of SpringCloud before, but Eureka is now discontinued, so Ali Dad has provided us with a better-to-use, more complete function, and easier configuration component—
  
  Nacos Nacos is open sourced by Alibaba A dynamic service discovery, configuration management, and service management platform that is easier to build cloud-native applications. Simply put, it is both a service registration discovery center and configuration management. It combines SpringCloud's Eureka and Config into one, making it easier to use Simple and simple; the official document describes it in detail. Today I will focus on using Alibaba's Nacos component for service registration and discovery in SpringCloud
  
1. Install Nacos
  Nacos supports Linux, Mac, and Windows. I mainly use Linux. And my services are installed in docker, so I installed it in one step with docker

# Nacos的默认端口是8848,启动单机模式,至于集群,以后再说吧
docker run -itd -p 8848:8848 --env MODE=standalone --restart=always --name nacos nacos/nacos-server

  After the container is started, access the Nacos console page through "ip:8848/nacos", the default user name and password are nacos
Insert picture description here
2. Import
  the parent project or public module that depends on your microservice or the microservice that needs to be registered to the registry Import Alibaba's management module and Nacos dependency in the module. I like to use the latest version. If there is a bug, it will be downgraded. Don't persuade;

<dependencies>
    <!-- Nacos -->
       <dependency>
           <groupId>com.alibaba.cloud</groupId>
           <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       </dependency>
</dependencies>
   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>com.alibaba.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>2.2.3.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

3. Configure Nacos configuration

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.109:8848 # 服务注册地址
  application:
    name: gulimall-coupon	# 服务名

4. Open the service registration discovery function in the startup class

@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
public class GulimallCouponApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(GulimallCouponApplication.class, args);
    }
}

5. Start the test to
  start the microservice, and you can see our registered service on the Nacos console page
Insert picture description here

Configuration Center

  I used the default configuration center of SpringCloud before , today I will talk about the Nacos configuration center provided by Alibaba. It is really easy to use ( official document )
  
1. Import dependencies

<!-- nacos 配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2. Write the bootstrap.yaml file.
  Note that spring.application.name must be configured, which is part of the Nacos configuration management dataId field; Nacos configuration center reads the properties file by default. If you want to use yaml, you need to configure file-exetension. Currently Nacos only supports yaml and properties format

spring:
  application:
    name: gulimall-coupon # 服务名
  cloud:
    nacos:
      config:
        server-addr: 192.168.0.109:8848 # 配置中心地址
        file-extension: yaml # 默认使用properties

3. Write control classes for testing
  . Realize automatic configuration updates through @RefreshScope

@RefreshScope //配置自动更新
@RestController
@RequestMapping("/coupon")
public class CouponTestApi {
    
    
    @Value("${coupon.name}")
    private String name;

    /**
     * nacos 配置中心测试
     */
    @GetMapping("/nacos/config")
    public R nacosConfigTest() {
    
    
        return R.ok().put("name", name);
    }
}

4. Add configuration in Nacos console
Insert picture description here
Insert picture description here
5. Start test
  can realize dynamic update of configuration
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/111011504
Recommended