博客
关于我
Nacos作为服务注册中心演示
阅读量:115 次
发布时间:2019-02-26

本文共 3730 字,大约阅读时间需要 12 分钟。

一、基于Nacos的服务提供者

服务提供方 (Service Provider)

是指提供可复用和可调用服务的应用方

  1. 在父工程下新建module (cloudalibaba-provider-payment-9001)
  2. POM 引入
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
  1. YML
server:  port: 9001spring:  application:    name: nacos-payment-provider  cloud:    nacos:      discovery:        server-addr: 172.28.129.83:8848 #配置Nacos地址management:  endpoints:    web:      exposure:        include: '*'
  1. 主启动
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class PaymentApplication9001 {       public static void main(String[] args) {           SpringApplication.run(PaymentApplication9001.class,args);    }}
  1. 业务类
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;@RestControllerpublic 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;    }}

6.测试

  • 启动9001服务

在这里插入图片描述

打开nacos"服务列表” ,可发现服务
在这里插入图片描述
点击详情可见端口号为9001
在这里插入图片描述

二、 基于Nacos的服务消费者

服务消费方 (Service Consumer)

是指会发起对某个服务调用的应用方

  • 根据9001新建一个9002module (cloudalibaba-provider-payment-9002)
  • yml端口9001修改成9002
  • 启动9002服务
  • 刷新nacos,发现
  • 在这里插入图片描述
    在这里插入图片描述

1、nacos自带负载均衡演示

  • 新建Module (cloudalibaba-consumer-nacos-order-83)
  • POM
  • YML
server:  port: 83spring:  application:    name: nacos-order-consumer  cloud:    nacos:      discovery:        server-addr: 172.28.129.83:8848#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)service-url:  nacos-user-service: http://nacos-payment-provider
  • 主启动类
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class OrderNacosApplication83 {       public static void main(String[] args) {           SpringApplication.run(OrderNacosApplication83.class,args);    }}
  • 业务类
    新建包config 下新建类ApplicationContextConfig
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;@Configurationpublic class ApplicationContextConfig {       @Bean    @LoadBalanced  //负载均衡    public RestTemplate restTemplate(){           return new RestTemplate();    }}

新建controller包,新建类OrderNacosController

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;@RestController@Slf4jpublic 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);    }}
  • 测试
  • 输入消费服务端口
    http://localhost:83/consumer/payment/nacos/1
    刷新一次切换9001或者9002,所以得出结论,83访问9001/9002,轮询负载OK
    在这里插入图片描述
    在这里插入图片描述
    消费者83成功注册到nacos
    在这里插入图片描述
    在这里插入图片描述

为什么Nacos支持负载均衡,因为他整合了ribbon

在这里插入图片描述

转载地址:http://jhfk.baihongyu.com/

你可能感兴趣的文章
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>