Building Spring Cloud Alibaba from scratch (1) ---NACOS Configuration Center

@[TOC](Spring Cloud Alibaba built from scratch (1)—NACOS Configuration Center)

Preface

Overall entrance:
https://blog.csdn.net/lwb314/article/details/108277732

Quick setup

  1. Build tool address: http://start.aliyun.com
    Because we are building it from scratch, we need to configure it first and only introduce one
    nacos config
    Insert image description here

POM.XML configuration

pom.xml is automatically generated and can be posted here as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lwb.demo</groupId>
    <artifactId>dubbo-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.lwb.demo.dubbotest.DubboTestApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Startup class

package com.lwb.demo.dubbotest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboTestApplication {
    
    

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

}

Get started with NACOS

1. Create the file bootstrap.yml. Delete the previous bootstrap.properties and application.properties. yml is a newer configuration format than properties and has some new usages, which will be introduced later.
Why is bootstrap.yml created here instead of application.yml? Because bootstrap is loaded before the spring boot framework is loaded, and spring boot and subsequent dubbo configurations directly use nacos, so the address and other information of the configuration center must be Written in bootstrap.yml configuration

2. Directly start the main function to see the error report

Question 1:
It prompts that the configuration does not take effect because we have not configured anything yet.

2021-08-13 13:57:16.334 ERROR 2240 --- [           main] c.a.n.c.config.http.ServerHttpAgent      : [NACOS SocketTimeoutException httpGet] currentServerAddr:http://localhost:8848, err : connect timed out
2021-08-13 13:57:16.334 ERROR 2240 --- [           main] c.a.n.c.config.http.ServerHttpAgent      : no available server
2021-08-13 13:57:16.341 ERROR 2240 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-localhost_8848] [sub-server] get server config exception, dataId=null.properties, group=DEFAULT_GROUP, tenant=

java.net.ConnectException: no available server

Question 1: According to the log, the connection is localhost:8848, but my service is actually on the server, so I first add the configuration of the nacos configuration center.

spring:
  cloud:
    nacos:
      config:
        server-addr: 10.10.10.230:8848

Question 2:

2021-08-13 14:07:35.799  INFO 14316 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-10.10.10.230_8848] [add-listener] ok, tenant=, dataId=null.properties, group=DEFAULT_GROUP, cnt=1

Error 2:
There is no spring.application.name, so let’s configure it.

java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"

Solution, add configuration

spring:
  application:
    name: dubbo_test_demo

Error 3: It
obviously prompts the program to register my local 8848 port again, but nacos is on the server, so continue to add configuration

com.alibaba.nacos.api.exception.NacosException: failed to req API:/nacos/v1/ns/instance/list after all servers([localhost:8848])

Guess you like

Origin blog.csdn.net/lwb314/article/details/119673562