Spring Cloud Alibaba - 03 注册中心Nacos 应用篇(上)

Posted 小小工匠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Alibaba - 03 注册中心Nacos 应用篇(上)相关的知识,希望对你有一定的参考价值。

文章目录


无注册中心直接调用Demo

package com.artisan.v1.controller;

import com.artisan.common.entity.OrderInfo;
import com.artisan.common.entity.ProductInfo;
import com.artisan.common.vo.OrderVo;
import com.artisan.mapper.OrderInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2022/2/1 21:20
 * @mark: show me the code , change the world
 */
@RestController
public class OrderInfoController 

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    /**
     * 调用地址,硬编码
     */
    public static final String uri = "http://localhost:9999/selectProductInfoById/";

    @RequestMapping("/selectOrderInfoById/orderNo")
    public Object selectOrderInfoById(@PathVariable("orderNo") String orderNo) 

        OrderInfo orderInfo = orderInfoMapper.selectOrderInfoById(orderNo);
        if (null == orderInfo) 
            return "根据orderNo:" + orderNo + "查询没有该订单";
        
        // 发起远程Http调用
        ResponseEntity<ProductInfo> responseEntity = restTemplate.getForEntity(uri + orderInfo.getProductNo(), ProductInfo.class);

        ProductInfo productInfo = responseEntity.getBody();

        if (productInfo == null) 
            return "没有对应的商品";
        

        OrderVo orderVo = new OrderVo();
        orderVo.setOrderNo(orderInfo.getOrderNo());
        orderVo.setUserName(orderInfo.getUserName());
        orderVo.setProductName(productInfo.getProductName());
        orderVo.setProductNum(orderInfo.getProductCount());

        return orderVo;
    

    

Order服务通过RestTemplate 调用方式调用远端的Product服务

我们来分析一下缺点

  • 在调用的时候,请求的Ip地址和端口是硬编码的.若此时,服务提供方(product)服务部署的机器换了端口或者是更换了部署机器的Ip,那么我们需要修改代码重新发布部署.

  • 假设我们的product服务压力过大,我们需要把product服务作为集群,那么意味着product是多节点部署比如原来的,我们只有一台服务器,现在有多台服务器,那么作为运维人员需要在服务消费方进行手工维护一份注册表(容易出错)

  • 当然了,上面的问题可以通过ng来做负载均衡,我首先认为这是可行的,但当时大规模的微服务, ng的配置文件复杂度可想而知。


注册中心的演进

详见 ProcessOn 6个版本的演进


Nacos Server 安装

下载地址 :https://github.com/alibaba/Nacos/releases

Linux 下的 安装 (单节点)

部署文档: https://nacos.io/zh-cn/docs/deployment.html


[root@VM-0-7-centos lb]# tar -xvzf  压缩包.tar.gz
 
[root@VM-0-7-centos lb]# cd nacos/bin
  
[root@VM-0-7-centos bin]# ./startup.sh  -m standalone   单机模式下运行
 
[root@VM-0-7-centos bin]#  lsof -i:8848

[root@VM-0-7-centos bin]#  sh shutdown.sh 停止nacos 

访问服务 http://ip:8848/nacos , 默认的用户名密码是 nocas/nocas


Nacos Client 接入

Step 1 搞依赖

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

Step 2 搞注解 @EnableDiscoveryClient (高版本可省略)

@SpringBootApplication
@EnableDiscoveryClient
public class ArtisanNacosClientOrderApplication 

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

    

Step 3 搞配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 4.117.97.88:8848
  application:
    name: artisan-product-center

server-addr 不需要写协议(http) ,直接写 ip:port

Step 4 启动服务端和客户端

  1. 启动nacos server ,
  2. 启动 artisan-cloud-nacosclient-order 注册到nacos server
  3. 启动 artisan-cloud-nacosclient-product 注册到nacos server


Step 5 验证测试

OrderInfoController 类增加

    @GetMapping("/getInstance")
    public List<ServiceInstance> getInstances(@RequestParam(required = true) String appName) 
        return discoveryClient.getInstances(appName);
    


    @GetMapping("/getServices")
    public List<String> getServices() 
        return discoveryClient.getServices();
    


源码

https://github.com/yangshangwei/SpringCloudAlibabMaster

以上是关于Spring Cloud Alibaba - 03 注册中心Nacos 应用篇(上)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Alibaba商城实战项目基础篇(day03)

Spring Cloud Alibaba环境搭建

Spring Cloud Alibaba全家桶——Spring Cloud Alibaba介绍

Spring Cloud Alibaba系列教程——Spring Cloud Alibaba开篇

spring boot 整合spring cloud alibaba

spring boot 整合spring cloud alibaba