A simple introduction to Spring Cloud Alibaba Nacos

1. Introduction to Nacos

Why is it called Nacos

The first four letters are the first two letters of Nameing and Configuration, and the last s is Service.

What is it?

Nacos(Nacos: Dynamic Naming and Configuration Service) is a dynamic service discovery, configuration management and service management platform that is easier to build native applications. Nacos is a combination of registration center + configuration center, which is equivalent to Eureka+ Config+ Bus.

What can you do?

It can be used Eurekaas a service center instead of Configa microservice configuration center.

2. Install Nacos

Download first before installation Nacos, download address https://github.com/alibaba/nacos/releases . Note here that if it is Windows (with Java+MySQL8.0 or lower), it can be downloaded out of the box. Windows starts the following command.

startup.cmd -m standalone

If your MySQLversion is 8.x, use the method as follows:
1. Download the source code git clone https://github.com/alibaba/nacos.git
2. Open the project with Idea and modify pom.xmlthe MySQLversion inside

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!-- <version>5.1.34</version> -->
    <version>8.0.18</version>
</dependency>

3. Modify the MySQL driver com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java

// import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;原数据在24行
//将其替换
import com.mysql.cj.jdbc.MysqlDataSource;

4. Recompile

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

5. Copy the compiled file to the place where you want to store nacos:

cp  distribution  xxx
mv distribution nacos

At this time, it is equivalent to downloading a supported MySQL8.xone Nacos. You can start it with the corresponding command.
The download link has been compiled: https://pan.baidu.com/s/1xVeg0t98BKlUbgvQMzTBxA Extraction code: vssl

If it is Linux/Mac, the startup command is as follows.

sh startup.sh -m standalone

The Nacosclient can be accessed after startup , the default username and password are both nacos
Insert picture description here

3. Nacos as the registration center

Service provider

1. Create a springboot project and add dependencies

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>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>

2. Configuration file configuration


server.port=9001
spring.application.name=nacos-provider
# nacos注册中心地址
spring.cloud.nacos.discovery.server-addr=localhost:8848

3. Add comments to the startup file

@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaProviderApplication {

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

}

4. Provide a consumer interface

@RestController
public class HelloController {

    @Value("${server.port}")
    String serverPort;

    @GetMapping("/provider/hello")
    public String hello(){
        return "nacos1 hello"+serverPort;
    }
}

5. Create another project as configured above for later load balancing test, or directly copy the virtual port mapping as shown in the figure below.
Insert picture description here
6. Start the interface provided by the project test: http://localhost:9001/provider/hello
Insert picture description here

Service consumer

1. Create a springboot project and add dependencies, where the dependencies are the same as the service provider
2. Configure application.properties

server.port=9011
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=localhost:8848
        
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url.nacos-user-service=http://nacos-provider

3. Configure registration RestTemplatefor inter-service interface call

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4. Call the service provider interface

@RestController
public class OrderNacosController {
    @Autowired
    RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverUrl;
    
    @GetMapping("/consumer/hello")
    public String hello(){
        return restTemplate.getForObject(serverUrl+"/provider/hello",String.class);
    }

}

5. Start project test: http://localhost:9011/consumer/hello


Comparison of service registration centers (both supported by Nacos CA/AP): When the
Insert picture description here
service fails, the CP directly refuses the service, which guarantees consistency, and the AP guarantees availability. Data inconsistencies are allowed for a period of time, but they will eventually tend to be consistent. .

4. Nacos as the configuration center

1. Create a springboot project and introduce dependencies

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</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>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>

2. Configure bootstrap.propertiesand application.propertiessum up the two configuration files , boostrap loading priority is higher than application
bootstrap.properties:


server.port=3377

spring.application.name=nacos-config-client
# 注册中心
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 配置中心
spring.cloud.nacos.config.server-addr=localhost:8848
# 这里指定的文件格式需要和nacos上新建的配置文件后缀相同,否则读不到
spring.cloud.nacos.config.file-extension=properties
# 分组配置
#spring.cloud.nacos.config.group=DEV_GROUP
# 命名空间配置
# spring.cloud.nacos.config.namespace=782bd58a-0902-4ac4-bef3-f145806eff3c

# 配置文件名称拼接公式,拼接后与nacos中相对于
#  ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

For detailed instructions, please refer to https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

application.properties configuration:

spring.profiles.active=dev

3. Define the interface to obtain configuration information

@RestController
@RefreshScope  //支持nacos动态刷新
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

4. Define configuration information. The
Insert picture description here
Insert picture description here
above configuration is the default group, we can also customize the group () and namespace (namespace).

5. After configuration, start the call interface to obtain configuration information http://localhost:3377/config/info
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41262903/article/details/106546679