SpringCloud Alibaba之Nacos

一、SpringCloud Alibaba

SpringCloud Alibaba official website: https://github.com/alibaba/spring-cloud-alibaba/blob/2.2.x/README-zh.md

Why does SpringCloud Alibaba appear?
spring cloud Netflix enters maintenance mode
1. What is the use?
insert image description here
2. What components are there?
insert image description here

2. Nacos

1. Nacos is a dynamic service discovery, configuration management and service management platform that is easier to build cloud remote applications.
2. Instead of Eureka as the registration center, instead of Config as the configuration center, that is, Nacos = Eureka + Config + Bus
3. Nacos download and installation: Nacos official website download and installation
Baidu network disk link: https://pan.baidu.com/s/17F4yz-GxBSqsgvaRK1ABmAPassword: 1usz
Shang Silicon Valley SpringCloud Alibaba
4. Nacos supports AP mode and CP mode switching, C means that the data seen by all nodes at the same time is consistent; and the definition of A is that all requests will receive responses.

What is CAP theory?
C: Consistency Consistency: Whether the data can maintain consistency among multiple copies.
A: Availability Availability: The services provided by the system must always be available, and the request for each operation must return the result within a limited time.
P: Tolerance of network Partition Partition fault tolerance: When a distributed system encounters a network partition failure, it still needs to ensure the consistency and availability of services to the outside world, unless the entire network fails.

5. Comparison between Nacos and other registries
insert image description here

3. How to use Nacos?

Parent project pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.12</junit.version>
    <log4j.version>1.2.17</log4j.version>
    <lombok.version>1.16.18</lombok.version>
    <mysql.version>5.1.47</mysql.version>
    <druid.version>1.1.16</druid.version>
    <mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子模块不用写groupId和version  -->
<dependencyManagement>
    <dependencies>
        <!--  springboot 2.2.2    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--  springcloud cloud Hoxton.SR1   -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--  springcloud cloud alibaba 2.1.0.RELEASE    -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

1. Service provider registration
New service provider: nacos-payment-provider9001
① pom.xml

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

<dependencies>
    <!--  SpringCloud alibaba nacos    -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--  web组件      -->
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

② application.yml

server:
  port: 9001 #端口号
spring:
  application:
    name: nacos-payment-provider #服务名称
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置nacos地址
management:
  endpoints:
    web:
      exposure:
        include: "*" #监控的东西

③ Startup class PaymentMain9001.java

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

2. Service consumers and load balancing
Nacos automatically integrates load balancing.
Under the parent project, create another service provider: nacos-payment-provider9002registration. Then create a service consumer: consumer-nacos-order80
① pom.xml

<dependencies>
    <!--  SpringCloud alibaba nacos    -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--  web组件      -->
    <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>
    <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>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

② application.yml

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

③ configuration class

//配置类
@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced  //负载均衡
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
}

④ business class controller

@RestController
@Slf4j
public class NacosOrderController {
    
    
    @Resource
    private RestTemplate restTemplate;
    @Value("${server-url.nacos-user-service}")
    private String serverUrl;  //在yml里面写的提供者服务路径,  值为:http://nacos-provider
    @GetMapping(value="consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") int id)
    {
    
    
        return restTemplate.getForObject(serverUrl+"/payment/nacos/"+id,String.class);
    }
}

⑤ Startup class

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

Start the service providers 9001 and 9002 respectively, service consumer 80, start Nacos, and visit Nacos to see that the service registration is successful.
insert image description here
Access the service consumer in the browser: http://localhost:80/consumer/payment/nacos/1001, refresh the access continuously, you can see that the load balancing (polling) of the consumer has been realized.
insert image description here
3. Nacos as a distributed configuration center
① pom.xml

<dependencies>
    <!--   nacos config     -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!--  SpringCloud alibaba nacos    -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--  web组件      -->
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-context</artifactId>
    </dependency>
</dependencies>

② Configuration file: bootstrap.yml

# nacos 配置
server:
  port: 3377
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #nacos作为配置中心的地址
        file-extension: yaml  # 指定yaml格式的配置

③ Configuration file: application.yml

spring:
  profiles:
    active: dev

The name of the configuration file that needs to be created in Nacos follows the following rules. The result after splicing is: Create a configuration file nacos-config-client-dev.yaml
insert image description here
named: in Nacos . ④ business controllernacos-config-client-dev.yaml
nacos-config-client-dev.yaml

@RestController
@RefreshScope   //开启刷新,否则分布式配置不会生效
public class ConfigCenterController {
    
    
    @Value("${config.info}")
    private String configInfo;

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

⑤ Startup class:

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

Start the service configuration center microservice, visit: http://localhost:3377/config/info, you can see that the information configured in Nacos has been read.
insert image description here
It comes with dynamic refresh. If you modify the configuration file information in Nacos and refresh it again, the accessed configuration information will also be automatically refreshed.
insert image description here
4. What is the relationship between Nacos namespace, grouping and DataID
?
Similar to the package name and class name in Java. The outermost namespace can be used to distinguish deployment environments, and Group and DataID logically distinguish two target objects.
insert image description here
The three situations
The default situation: namespace=public, group=default_group, the default cluster is default.
For example, if we have three environments: development, testing, and production environments, we can create three namespaces, and different namespaces are isolated.

Three schemes to load configuration

(1) DataID configuration of Nacos
insert image description here
(2) group grouping of Nacos
insert image description here
(3) namespace space scheme of Nacos
insert image description here

Construction of Nacos cluster

Nacos adopts a centralized storage method to support cluster deployment, and currently only supports MySQL storage.
Three deployment modes supported by Nacos:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46081857/article/details/123690976