• nocos注册中心使用教程


    1.下载和安装

    进入到官网下载就好了
    在这里插入图片描述
    解压

    在这里插入图片描述
    启动
    在这里插入图片描述

    2.新建提供者模块

    2.1新建提供者模块cloudalibaba-provider-payment9001

    2.1.1在父项目中新加入依赖

    <dependency>
    	  <groupId>com.alibaba.cloudgroupId>
    	  <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    	  <version>2.1.0.RELEASEversion>
    	  <type>pomtype>
    	  <scope>importscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.1.2在cloudalibaba-provider-payment9001的pom中加入

    
    <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>cloud2020artifactId>
            <groupId>com.cao.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloudalibaba-provider-payment9001artifactId>
    
        <dependencies>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    2.1.3yml写配置

    server:
      port: 9001
    
    spring:
      application:
        name: nacos-payment-provider
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #配置Nacos地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.1.4写主启动类、和Controller的代码

    主启动类代码编写 @EnableDiscoveryClient

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class PaymentMain9001 {
    	public static void main(String[] args) {
    		SpringApplication.run(PaymentMain9001.class, args);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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;
    
    /**
     * @author caowenbin
     * @Title: PaymentController
     * @Package com.cao.cloudalibaba
     * @Description: nacos_Controller
     * @date 2023/10/16 0:02
     */
    @RestController
    public class PaymentController {
    	@Value("${server.port}")
    	private String serverPort;
    
    	// http://localhost:9001/payment/nacos/1
    	@GetMapping(value = "/payment/nacos/{id}")
    	public String getPayment(@PathVariable("id") Integer id) {
    		return "nacos registry, serverPort: " + serverPort + "\t id: " + id;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    2.1.5测试

    在这里插入图片描述

    2.2重复步骤2.1新建提供者cloudalibaba-provider-payment9002

    同一个服务名下会出现两个实例
    在这里插入图片描述
    查看详情:
    在这里插入图片描述

    2.2.1测试

    在这里插入图片描述

    3.新建消费者模块cloudalibaba-consumer-nacos-order83

    3.1pom文件加入依赖

    <?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>cloud2020</artifactId>
            <groupId>com.cao.springcloud</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>
            <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <dependency>
                <groupId>com.cao.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- SpringBoot整合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>
            <!--日常通用jar包配置-->
            <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    3.2yml写配置

    server:
      port: 83
    
    spring:
      application:
        name: nacos-order-consumer
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
    
    
    #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
    service-url:
      nacos-user-service: http://nacos-payment-provider
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.3主启动类

     package com.cao.springcloud.alibaba;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class OrderNacosMain83
    {
        public static void main(String[] args)
        {
            SpringApplication.run(OrderNacosMain83.class,args);
        }
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.4负载均衡

    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;
    
    /**
     * 负载均衡LoadBalanced
     */
    @Configuration
    public class ApplicationContextBean {
    	@Bean
    	@LoadBalanced
    	public RestTemplate getRestTemplate() {
    		return new RestTemplate();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.5controller

    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;
    
    
    /**
     * 消费者83调用提供者9001或者9002
     */
    @RestController
    public class OrderNacosController {
    	@Resource
    	private RestTemplate restTemplate;
    
    	//将之前固定的写法转成从配置文件中读取
    //    private static final String SERVICE_URL = "http://nacos-payment-provider";
    
    	@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);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    4.测试

    4.1同时启动9001、 9002、83

    在这里插入图片描述

    4.2查看注册中心

    在这里插入图片描述

    4.3用83访问9001/9002,轮询负载OK

    http://localhost:83/consumer/payment/nacos/2
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    【愚公系列】2022年08月 Go教学课程 029-面向对象简介
    实现定时器的两种方法:使用windows api定时器 和使用c++11/14 定时器
    Django笔记三十之log日志记录详解
    黑马点评-02使用Redis代替session,Redis + token机制实现
    【自然语言处理(NLP)】基于PaddleHub的文本审核
    七周成为数据分析师 | 业务
    使用esxcli命令升级VMware ESXi补丁
    Linux vim
    IntelliJ IDEA 常用快捷键一览表
    【Python 基础篇】Python环境搭建
  • 原文地址:https://blog.csdn.net/weixin_44137201/article/details/133849961