首页 > 其他分享 >HM-SCAli4【服务治理介绍、nacos入门】

HM-SCAli4【服务治理介绍、nacos入门】

时间:2022-10-18 09:24:39浏览次数:49  
标签:服务 org SCAli4 nacos springframework 注册 HM import

1 服务治理介绍

image-20221018081458407

先来思考一个问题
通过上一章的操作,我们已经可以实现微服务之间的调用。但是我们把服务提供者的网络地址(ip,端口)等硬编码到了代码中,这种做法存在许多问题:
一旦服务提供者地址变化,就需要手工修改代码
一旦是多个服务提供者,无法实现负载均衡功能
一旦服务变得越来越多,人工维护调用关系困难
那么应该怎么解决呢, 这时候就需要通过注册中心动态的实现服务治理。

image-20221018082604698

服务治理是微服务架构中最核心最基本的模块。用于实现各个微服务的自动化注册与发现。

  • 服务注册:在服务治理框架中,都会构建一个注册中心,每个服务单元向注册中心登记自己提供服务的详细信息。并在注册中心形成一张服务的清单,服务注册中心需要以心跳的方式去监测清单中的服务是否可用,如果不可用,需要在服务清单中剔除不可用的服务。
  • 服务发现:服务调用方向服务注册中心咨询服务,并获取所有服务的实例清单,实现对具体服务实例的访问。

image-20221018082136906

image-20221018082155779

常用的注册中心: Zookeeper、Nacos、Eureka、Consul

2 nacos实现服务调用

2.1 搭建nacos环境

image-20221018083241968

image-20221018083334866

image-20221018083622296

也可双击.cmd启动(默认即单节点模式启动)

image-20221018083725916

2.2 将商品微服务注册到nacos

image-20221018083835511

image-20221018084430795

image-20221018084450435

image-20221018084516933

image-20221018084557123

image-20221018084626081

2.3 将订单微服务注册到nacos

操作步骤同上

image-20221018084937705

2.4 通过nacos实现微服务调用

DiscoveryClient是专门负责服务注册和发现的,我们可以通过它获取到注册到注册中心的所有服务

package com.yppah.controller;

import com.alibaba.fastjson.JSON;
import com.yppah.domain.Order;
import com.yppah.domain.Product;
import com.yppah.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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 java.util.List;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("客户下单,此时要调用商品微服务查询商品信息");
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        Product product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product) + "<<");

        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户1");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.save(order);
        return order;
    }

    /*// 模拟买一件商品
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("客户下单,此时要调用商品微服务查询商品信息");
        // 通过restTemplate调用商品微服务
//        硬编码URL存在的问题:
//        1 一旦服务提供者的地址信息变化了,我们就不得不去修改服务调用者的java代码
//        2 一旦无法提供者做了集群,服务调用者一方无法实现负载均衡的去调用
//        3 一旦微服务变得越来越多,如何来管理这个服务清单就成了问题
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product) + "<<");

        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户1");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }*/
}

重启order微服务,访问创建订单方法进行测试

image-20221018090319715

标签:服务,org,SCAli4,nacos,springframework,注册,HM,import
From: https://www.cnblogs.com/yppah/p/16801471.html

相关文章