This is the 17th day of my participation in the More text Challenge. For more details, see more text Challenge

Nacos

An overview,

Website: https://nacos.io/zh-cn/docs/what-is-nacos.html

Registry and configuration center

2. Environment configuration

Download and install

  • Download Nacos:https://github.com/alibaba/nacos/releases
  • Start Nacos: Run the command in the bin directory of the decompressed foldercmd startup.cmd -m standalone, standalone (non-cluster startup)
  • accesshttp://localhost:8848/nacosTo access the main interface, the account password is nacos

Rely on

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code

configuration

server:
  port: 9001
spring:
  application:
    name: nacos-provider-payment
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848   Nacos configuration center address
management:   # Expose the monitoring address
  endpoints:
    web:
      exposure:
        include: The '*'
Copy the code

The main start class

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class, args); }}Copy the code

Once the service is started, you can see the current service on the NACOS home page.

3. Nacos load balancing

According to the 9001Copy a 9011 service is used to provide services.

Create a consumer for the service cloudalia-consumer-nacos-Order83

Depend on the same

The configuration file

server:
  port: 83
spring:
  application:
    name: nacos-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848   Nacos configuration center address

service-url:  # Optional, user access to the microservice name, convenient later use of the service path
  nacos-user-service: http://nacos-provider-payment
Copy the code

The config and Controller

@SpringBootConfiguration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(a){
        return newRestTemplate(); }}@RestController()
public class OrderNacosController {
    @Resource
    private RestTemplate restTemplate;

    // Read the microservice name from the configuration file
    @Value("${service-url.nacos-user-service}")
    private String serverUrl;

    @GetMapping("/consumer/nacos/getPort/{id}")
    public String paymentInfo(@PathVariable("id") String id) {
        return restTemplate.getForObject(serverUrl + "/nacos/getPort/"+ id, String.class); }}Copy the code

For Nacos with Ribbon, so naturally has the ability to load balance, visit http://localhost:83/consumer/nacos/getPort/1 now, will visit polling 9001 and 9011 two services.

Switching between AP and CP is supported

A To ensure availability and C to ensure data consistency.

If you do not need to store service-level messages, if the service instance is registered with nacOS-Client, and if you can keep heartbeat reporting, then you can choose AP mode. Current mainstream services such as Spring Cloud and Dubbo.

CP is required if information needs to be edited or stored at the service level, such as K8S service and DNS service.

Mode switching:

curl -X PUT `$NACOS_SERVER:8848/nacos/v1/ns/operator/switches? entry=serverMode&value=CP’

4. Nacos Service Configuration Center

newcloudalibaba-config-nacos-client3377, import dependencies

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Copy the code

Use two configuration files, an Application and a higher-level bootstrap

application.yml

spring:
  profiles:
    active: dev
Copy the code

bootstrap,yml

server:
  port: 3377
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848   Nacos configuration center address
      config:
        server-addr: 127.0. 01.: 8848 # nacOS as configuration center
        file-extension: yaml      Specifies the configuration file format
Copy the code

Add the configuration file on the NACos main screen

Note: The format of Data ID is${prefix}-${spring.profiles.active}.${file-extension}, as shown in the above configurationnacos-config-client-dev.yaml

Add a Controller to access the corresponding configuration file

@RestController
@RefreshScope       // Automatically refresh the configuration with Spring Cloud native annotations
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/getInfo")
    public String getConfigInfo(a) {
        returnconfigInfo; }}Copy the code

The RefreshScope annotation has been configured, so dynamic refresh is included.

5. NamesPace, DataID and grouping of Nacos

The default NamesPase of Nacos is Public. NamesPase is mainly used for isolation. Isolation based on different development environments.

Groups can be used to Group different microservices into the same Group.

A Service is a microservice. A Service can contain multiple microservices, also known as a Cluster.

Instance is the Instance of the microservice.

DataID scheme

Create nacos-config-client-test.yaml and change active to test in application.yml. The configuration content corresponding to the DataID is also obtained.

The Group project

The default is DEFAULT_GROUP

You can also obtain different configuration information based on different groups. Add a configuration for DEV_GROUP and add a configuration for Group to the config file.

config:
  server-addr: 127.0. 01.: 8848 # nacOS as configuration center
  file-extension: yaml      Specifies the configuration file format
  group: DEV_GROUP
Copy the code

The NamesPace solution

Adding a namespace to the NACOS interface generates a namespace ID

Add a namespace configuration to the config of the configuration file

config:
  server-addr: 127.0. 01.: 8848 # nacOS as configuration center
  file-extension: yaml      Specifies the configuration file format
  group: DEV_GROUP
  namespace: 9ed7b1a9-6220-4bc5-b271-e39df2651aa8
Copy the code

The configuration file now accessed is the configuration file with DataID nacos-config-client-dev.yaml in the DEV_GROUP group in namespace 9ed7b1a9-6220-4bc5-b271-e39df2651aa8.

Nacos clustering and persistence configuration

Official statement https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html

Nacos database switchover

Nacos comes with an embedded database, Derby, that needs to be switched to MySQL.

  • Find it in the conf foldernacos-mysql.sqlTo run the SQL
  • Modify theapplication.propertiesFile, modify item referencehttps://nacos.io/zh-cn/docs/deployment.html
  • Restart nacos. The embedded database has been switched to MySQL database.

Configure the Nacos cluster and Nginx on Linux

Modify cluster. Conf
172.20.102.156:3333
172.20.102.156:4444
172.20.102.156:5555
Copy the code

Note: 172.20.102.156 is obtained using hostname -i

Modify the startup. Sh

If the memory is too small to start three Nacos, change here

Modify the nginx.conf configuration

Start the Nacos
./startup.sh -p 3333
./startup.sh -p 4444
./startup.sh -p 5555
#See how much startup success
ps -ef | grep nacos | grep -v grep | wc -l
Copy the code
Start the Nginx

./nginx

test

Create a Cloudalia-Provider-Payment9002 service

The configuration file

server:
  port: 9002
spring:
  application:
    name: nacos-provider-payment
  cloud:
    nacos:
      discovery:
        server-addr: 47.95226.96.: 1111   Nacos configuration center address
management:   # Expose the monitoring address
  endpoints:
    web:
      exposure:
        include: The '*'
Copy the code

Visit http://Linux at 1111/nacos