Chapter 19 Detailed Explanation of Nacos Unified Configuration Center

1. nacos as a unified configuration center

1. His way of managing configuration files is to form a version library on his own server, so there is no need to create a remote version library. 2.
When nacos manages configuration files as a unified configuration center, there is also version control

2. Steps to use Nacos Unified Configuration Center

1. Create a client of an independent configuration center

       Here, the order-server and product-server in Chapter 18 are used as Nacos config clients. And import client dependencies in Nacos configuration.

<!-- Introduce Nacos Config Client configuration center client dependencies -->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

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

        The springcloud2020 version disables Bootstrap by default , and spring.config.import adds support for decryption. For the configuration import of Config Client, Consul, Vault, and Zookeeper, if you need to use the original bootstrap configuration guidance function , you need to import o rg.springframework.cloud:spring-cloud-starter-bootstrap dependency into the project.


2. Give your own configuration to Nacos config management

       Here, all the configurations in the order-server and product-server in Chapter 18 are transferred to the Nacos config center.

 

3. Notify the client to pull the file location

       In the bootstrap in the client's resources, tell the client where to pull the file.

  • Inform the config server address, that is, the Nacos server address
  • Tells the group of the service.
  • Tell the name of the configuration file 

spring:
  cloud:
    nacos:
      config:
        #Tell the client which server to go to to find the total address of the nacos server registration center
        server-addr: localhost:8848 # Configure nacos server registration center address
        group: DEFAULT_GROUP # Specify the group to pull
        name: order-prod # Specify the name of the file to be pulled
        file-extension: yaml

spring:
  cloud:
    nacos:
      config:
        #The client specifies the address of the configuration center to pull the file
        server-addr: localhost:8848 # configure nacos server configuration center address
        group: DEFAULT_GROUP # Specify the group to pull
        name: product-prod # Specify the name of the pulled file
        file-extension: yaml

3. Dynamic Refresh

       Here, the order-server and product-server controllers in Chapter 18 are modified based on the annotation @RefreshScope  , and the code is modified as follows:

  • OrderController uses the annotation @RefreshScope

OrderController 

import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {

    @Autowired
    IOrderFeignService orderFeignService;

    @RequestMapping("/buy/{id}")
    public Product buy(@PathVariable Long id) {
        Product product = orderFeignService.findOrderById(id);
        return product;
    }
}
  • ProductController uses annotation @RefreshScope 

 ProductController 

import com.hwadee.springcloud.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;

@RestController
@RequestMapping("/product")
@RefreshScope
public class ProductController {
    //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;

    @RequestMapping("/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要测试负载均衡,所以返回 ip 地址及端口号
        product.setName("当前访问服务地址:" + ip + ":" + port+"  "+"查询商品订单,订单号:"+id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
}
  • Restart the service and modify the configuration file on Nacos for testing

Four, three important concepts

  • Namespace: The default namespace is public, which isolates the configuration files of multiple projects from the perspective of the project
  • Group: The group of each namespace defaults to DEFAULT_GROUP, which isolates multiple configuration files from the perspective of different microservices of the same project.
  • DataID: the unique identifier of the file (ie the file name)

 1. Namespace

     create namespace

2. Create Group and DataID 

 3. Specify Namespace, Group and DataID

     Modify the bootstrap file and specify the namespace. Remember, the namespace uses its id.

  • Order-server guide file bootstrap.yml modification
spring:
  cloud:
    nacos:
      config:
        #The client specifies the address of the configuration center to pull the file
        server-addr: localhost:8848 # configure nacos server configuration center address
        namespace: bbcccf09-be17-419e-864f-d2cce63bb258 #Specify namespace 
        group: DEV #Specify the group to pull
        name: order-dev # Specify the name of the file to pull
        file-extension: yaml
  •  Product-server bootstrap file bootstrap.yml modification
spring:
  cloud:
    nacos:
      config:
        #The client specifies the address of the configuration center to pull the file
        server-addr: localhost:8848 # configure nacos server configuration center address
        namespace: bbcccf09-be17-419e-864f-d2cce63bb258 #Specify namespace 
        group: DEV #Specify the group to pull
        name: order-dev # Specify the name of the file to pull
        file-extension: yaml

 4. Start the test

Five, persistent switching configuration

       By default, Nacos uses its own embedded database derby to store data. Report configuration file information such as order-prod.yaml locally. If you restart Nacos the file will disappear. And if you start multiple Nacos nodes under the default configuration, there will be consistency problems in data storage. In order to solve this problem, Nacos adopts a centralized storage method to support cluster deployment, and currently only supports MySQL storage.

Nacos configuration persistent MySQL storage steps:

1. Install the database mysql database

2. Execute the mysql script
 

Chapter 18: Chapter 18 Detailed Explanation of Nacos Registration Center - Getting Started Cases and Service Communication

Chapter 20: Detailed Explanation of Alibaba Sentinel-Introduction and Download and Installation

Guess you like

Origin blog.csdn.net/qq_41946216/article/details/127611755