Nacos学习笔记 Nacos整合SpringCloud流程

Posted 鮀城小帅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nacos学习笔记 Nacos整合SpringCloud流程相关的知识,希望对你有一定的参考价值。

分别创建两个SpringBoot项目,一个是生产者,一个是消费者。

1. 生产者

1.1 首先我们创建一个生产者

使用IDE的Spring插件创建一个Spring boot项目,pom内容如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>nacos_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos_demo</name>
    <description>nacos_demo</description>
    <properties>
        <java.version>17</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-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-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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2 添加配置 application.yml

server:
  port: 8080
spring:
  application:
    name: nacos-provide
  cloud:
    nacos:
      config:
        max-retry: 10
      discovery:
        server-addr: 127.0.0.1:8848

1.3 为启动类添加@EnableDiscoveryClient注解。

package com.example.nacos_demo;

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

@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication 

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


1.4 接下来创建一个Controller

package com.example.nacos_demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wushaopei
 * @create 2022-12-20 10:30
 */
@RestController
public class ProviderController 
    @GetMapping("/helloNacos")
    public String helloNacos() 
        return "你好,nacos!";
    

2. 创建消费者

2.1 使用IDE的Spring插件创建一个Spring boot项目,pom内容如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>nacos_consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos_consumer</name>
    <description>nacos_consumer</description>
    <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-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-tomcat</artifactId>
            <scope>provided</scope>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 添加 application.yml 配置

server:
  port: 8181
spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      config:
        max-retry: 10
      discovery:
        server-addr: 127.0.0.1:8848

2.3 创建Controller

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author wushaopei
 * @create 2022-12-20 10:33
 */
@RestController
public class ConsumerController 

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
        return new RestTemplate();
    

    @GetMapping("/consumer")
    public String test1() 
        return restTemplate.getForObject("http://nacos-provide/helloNacos",String.class);
    


3. 启动Nacos服务、生产者、消费者

分别启动消费者与生产者,然后进入nacos主页面。

可以看到生产者与消费者分别在nacos中注册。

此时我们调用一下消费者的接口。

192.168.174.1:8181/consumer

 当我们调用消费者接口时,会通过nacos查找注册的生产者,告诉消费者如何找到生产者。

以上是关于Nacos学习笔记 Nacos整合SpringCloud流程的主要内容,如果未能解决你的问题,请参考以下文章

Nacos学习笔记 Nacos整合SpringCloud流程

Nacos学习笔记 Nacos整合SpringCloud流程

Nacos学习笔记 Nacos集群部署

Nacos学习笔记 Nacos集群部署

Nacos学习笔记 Nacos基础核心特性

Nacos学习笔记 Nacos的简介与安装