1.概况

  服务提供者9001和9002,他们是同一个服务,服务消费者83

2.创建服务提供者

  9001和9002除了端口是一样的,这里只演示一个

2.1项目结构

2.2依赖

nacos依赖

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

完整的

<dependencies>
<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-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>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>

2.3配置文件

server:
port: 9001 spring:
application:
name: nacos-payment-provider #将会作为注册到注册中心的服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址 management:
endpoints:
web:
exposure:
include: '*'

2.4主启动类

  注解使用的是@EnableDiscoveryClient

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @Classname PaymentMain9001
* @Description TODO
* @Date 2021/6/18 0018 下午 2:20
* @Created by jcc
*/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class,args);
}
}

2.5业务类

package com.atguigu.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; /**
* @Classname PaymentController
* @Description TODO
* @Date 2021/6/18 0018 下午 2:21
* @Created by jcc
*/
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort; @GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id)
{
return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
}
}

3.创建服务消费者

3.1项目结构

 3.2依赖

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

完整

<dependencies>

        <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> <dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <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>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>

3.3配置文件

server:
port: 83 spring:
application:
name: nacos-payment-consumer #将会作为注册到注册中心的服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址 service-url:
nacos-user-service: http://nacos-payment-provider

3.4主启动类

package com.atguigu.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
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; /**
* @Classname OrderNacosController
* @Description TODO
* @Date 2021/6/18 0018 下午 2:58
* @Created by jcc
*/ @RestController
@Slf4j
public class OrderNacosController { @Resource
private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}")
private String serverURL; @GetMapping(value = "/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Long id) { return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class); }
}

3.5配置类和业务类

nacos集成了Ribbon
package com.atguigu.springcloud.alibaba.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; /**
* @Classname ApplicationContextConfig
* @Description TODO
* @Date 2021/6/18 0018 下午 2:56
* @Created by jcc
*/
@Configuration
public class ApplicationContextConfig { @Bean
@LoadBalanced //nacos集成了Ribbon,这里是开启ribbon负载均衡
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
package com.atguigu.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
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; /**
* @Classname OrderNacosController
* @Description TODO
* @Date 2021/6/18 0018 下午 2:58
* @Created by jcc
*/ @RestController
@Slf4j
public class OrderNacosController { @Resource
private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}")
private String serverURL; @GetMapping(value = "/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Long id) { return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class); }
}

4.启动三个服务

  启动后,查看nacos管理页面,可以看到三个服务

  

  此时,访问服务消费者83,连续访问

  http://localhost:83/consumer/payment/nacos/13

  

  连续访问,发现 9001和9002轮询提供服务,因为Ribbon默认的策略是轮询

  关于ribbon的介绍:https://www.cnblogs.com/jthr/p/14694632.html

spring cloud alibaba - Nacos 作为注册中心基础使用-服务提供者和消费者的更多相关文章

  1. Spring Cloud Alibaba | Nacos服务注册与发现

    目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...

  2. Spring Cloud Alibaba Nacos 服务注册与发现功能实现!

    Nacos 是 Spring Cloud Alibaba 中一个重要的组成部分,它提供了两个重要的功能:服务注册与发现和统一的配置中心功能. 服务注册与发现功能解决了微服务集群中,调用者和服务提供者连 ...

  3. Spring Cloud Alibaba | Nacos服务中心初探

    目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...

  4. Spring Cloud Alibaba Nacos 的 2 种健康检查机制!

    Spring Cloud Alibaba Nacos 作为注册中心不止提供了服务注册和服务发现功能,它还提供了服务可用性监测的机制.有了此机制之后,Nacos 才能感知服务的健康状态,从而为服务调用者 ...

  5. Spring Cloud Alibaba | Nacos配置管理

    目录 Spring Cloud Alibaba | Nacos配置管理 1. pom.xml 项目依赖 2. 在 bootstrap.properties 中配置 Nacos server 的地址和应 ...

  6. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  7. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

  8. Spring Cloud Alibaba+Nacos搭建微服务架构

    1. Spring Cloud Alibaba 简介    Spring Cloud Alibaba是阿里巴巴为分布式应用提供的一站式解决方案,能够更方便快捷地搭建分布式平台,nacos拥有着替换eu ...

  9. Spring Cloud Alibaba | Nacos集群部署

    目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...

  10. Spring Cloud Alibaba | Sentinel: 服务限流基础篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...

随机推荐

  1. 异步编排 Spring(线程池)

    目录 异步编排 CompletableFuture 的详解 代码测试 配置类的引入 Demo1 Demo2 CompletableFuture的async后缀函数与不带async的函数的区别 Thre ...

  2. telnet 退出失败

    1. 根据提示,使用ctrl+] ,退出到telnet >界面 2. 使用quit退出 3. 最简单的退出方式,强制退出: ] + Enter键

  3. 2流高手速成记(之九):基于SpringCloudGateway实现服务网关功能

    咱们接上回 上一节我们基于Sentinel实现了微服务体系下的限流和熔断,使得整个微服务架构的安全性和稳定性上升了一个台阶 篇尾我们引出了一个问题,众多的微服务节点,我们如何部署才能满足客户端简洁高效 ...

  4. 解决"VLC 无法打开 MRL「screen://」。详情请检查日志" 问题

    问题描述 vlc 抓取桌面视频报这个错误 解决 sudo apt-get install vlc-plugin-access-extra 其他 不一定每次都在图形化界面调用,也可以直接在终端输入 vl ...

  5. js 评级五星设置

    <input type="text" class="tt" style="color:red;border-style:none"&g ...

  6. Type Script 在流程设计器的落地实践

    流程设计器项目介绍 从事过BPM行业的大佬必然对流程建模工具非常熟悉,做为WFMC三大体系结构模型中的核心模块,它是工作流的能力模型,其他模块都围绕工作流定义来构建. 成熟的建模工具通过可视化的操作界 ...

  7. 第三模块的下载、requests模块、openpyxl模块

    目录 第三方模块的下载安装 下载第三模块的方式 针对下载第三模块时可能会出现的问题 网络爬虫模块之requests模块 自动化办公领域之openpyxl模块 第三方模块的下载安装 第三方模块:别人写的 ...

  8. AssertionError: Class XXXXX missing "Meta.model" attribute

    源码示例: from rest_framework import serializers from set.models import Set class SetSerializers(seriali ...

  9. 比Sqoop功能更加强大开源数据同步工具DataX实战

    @ 目录 概述 定义 与Sqoop对比 框架设计 支持插件 核心架构 核心优势 部署 基础环境 安装 从stream读取数据并打印到控制台 读取MySQL写入HDFS 读取HDFS写入MySQL 执行 ...

  10. 使用time.Time数据类型获取时间报错

    报错类型:Error 1292: Incorrect datetime value: '0000-00-00' for column 'created_at' at row 1 在添加用户到数据库时, ...