SpringCloud has no introduction to quick use, and the basic use of the nacos registry

Problem background

Learn springcloud microservice project from scratch
matters needing attention:

  • Convention > configuration > encoding
  • IDEA version 2021.1
  • I have divided this project into many chapters, each of which is an operation step to make it more simple and clear
  • The controller calls service, and the service calls dao

nacos registry

1 You can directly refer to the installation manual on the official website to install nacos
2. Create a new sub module

3 select jdk1.8

4 enter the service name: cloudalibaba provider payment9001 and cloudalibaba provider payment9002

5. Introducing pom dependency

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud2022</artifactId>
        <groupId>com.yg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-provider-payment9001</artifactId>

    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringBoot integration Web assembly -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--Daily general jar Package configuration-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

6. Create a new application YML file

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Configure Nacos address

management:
  endpoints:
    web:
      exposure:
        include: '*'

7 Overall directory structure

Add cloudalibaba-consumer-nacos-order83 order micro service

1. Create a new service name: cloudalibaba-consumer-nacos-order83, import pom dependency

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud2022</artifactId>
        <groupId>com.yg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-nacos-order83</artifactId>

    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- Import self defined api General package, can use Payment payment Entity -->
        <dependency>
            <groupId>com.yg</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- SpringBoot integration Web assembly -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--Daily general jar Package configuration-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. Create application YML file

server:
  port: 83


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Configure Nacos address


#The name of the micro service that the consumer will access (the micro service provider that has successfully registered with nacos)
service-url:
  nacos-user-service: http://nacos-payment-provider

3 Create startup class

package com.yg.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Author suolong
 * @Date 2022/6/20 18:29
 * @Version 2.0
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain83 {

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

}

4. Add a load balancing http call configuration class

package com.yg.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Author suolong
 * @Date 2022/6/15 20:53
 * @Version 2.0
 */
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced // Use the @LoadBalanced annotation to give the RestTemplate the ability of load balancing
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }

}

5 add controller class

package com.yg.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author suolong
 * @Date 2022/6/20 18:33
 * @Version 2.0
 */
@RestController
public class OrderNacosController {

    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping("/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id)
    {
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }


}

6 overall directory structure

7 start Nacos, account: Nacos password: Nacos start various micro services, log in to Nacos: http://localhost:8848/nacos

8 use postman to call services to achieve load balancing: http://localhost:83/consumer/payment/nacos/1

nacos registry comparison

1 nacos panorama

2 nacos and CAP


3. CP and AP switching of Nacos

  • C means that all nodes see the same data at the same time; The definition of A is that all requests will receive A response.
  • When to choose which mode to use
  1. If you do not need to store service level information, and the service instance is registered through the Nacos client and can maintain heartbeat reporting, you can select the AP mode. The current mainstream services, such as Spring cloud and Dubbo services, are applicable to the AP mode. The AP mode weakens the consistency for the possibility of services. Therefore, only temporary instances can be registered in the AP mode
  2. If you need to edit or store configuration information at the service level, CP is required, and K8S service and DNS service are applicable to CP mode.
    In CP mode, persistent instance registration is supported. In this case, the Raft protocol is used as the cluster operation mode. In this mode, the service must be registered before registering the instance. If the service does not exist, an error will be returned
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'




As a programmer, write a lyric every time in the 184th article to see how many songs there are in life. wahahaha

Lyric: this music can be Waacking or Locking

Tags: Java Spring Cloud intellij-idea

Posted by echo10 on Fri, 01 Jul 2022 02:07:16 +0930