java architecture Road - (Micro Service Focus) nacos cluster succinctly combat

Last Review:

  The last blog, we say that the major micro-development services and nacos cluster to build stand-alone, single -m standalone need to start, the cluster is recommended to use nginx reverse proxy to do something, self-assurance and mysql ngxin high availability.

The positioning:

  A, nacos interior concept

  Second, the basic use of nacos

  Third, prepare for the next ribbon assembly

nacos interior concept

  The last time we build a good nacos clusters, the clusters that we just continue on the basis that the last time we registered only when it comes to how a service to our nacos, but the call has not said that the various calls us to say it (load balancing algorithm called the ribbon to say).

  ①. Call, let's call a simple, user system to call order system, check orders.

   Springboot build two projects, adding registry configuration. Configuring last blog said, not repeat them here, and write a method in the order service.

@RestController
 public  class OrderController { 

    @GetMapping ( "/ getOrderData" )
     public String getOrderData () {
         return "service order data obtained" ; 
    } 


    @GetMapping ( "/ getOrderDataB" )
     public String getOrderDataB () {
         return "Order obtained service data B " ; 
    } 
}

  I wrote a simple, and can get to the following description can also string when calling.

  In customer service, the write config configuration.

@Configuration
 public  class Client Config { 

    @Bean 
    public Residual Template rest template () {
         return  new Rest Template (); 
    } 
}

  Start calling friends

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/getOrder")
    public String getOrderData(){
        List<ServiceInstance> orderInfo = discoveryClient.getInstances("nacos-order");
        if(null == orderInfo || orderInfo.isEmpty()) {
            return "用户微服务没有对应的实例可用";
        }
      
        String targetUri = orderInfo.get(0).getUri().toString();

        String forObject = restTemplate.getForObject(targetUri + "/getOrderData", String.class);
        System.out.println("forObject = " + forObject);

        return forObject;
    }
}

  In this way, the easiest call will be realized. (No load balancing algorithm, I directly get (0) a).

  ②.namespace

  Here we look at what namespace, if we are to develop, test environments share a nacos, we must address the interface is different, and you are in the development process, it is not recommended freely configurable test environment, then we should use namespace to isolate our space.

  Open our nacos page, click on the namespace, New, enter the information you can.

   Let us look at the code to achieve, how to achieve isolation.

  First, add a namespace configured in the configuration file. namespace: namespace ID

spring:
  application:
    name: nacos-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.138.119:8848
        namespace: 0610f97e-c25d-4f49-bfb8-b340e3584b82
server:
  port: 8888

   订单服务的配置为开发环境,用户服务配置为测试环境的,我们来运行一下代码,理论上应该调用不通的,我们来看一下结果。

  现实如此无法调用,也就做到了我们的隔离环境。如果不写namespace,默认是public命名空间。

  ③.group

  group分组,也是用来隔离的,打个比方啊,加入我们的用户服务,订单服务,仓储服务和物流服务四个服务,订单服务中有一个接口叫getData,仓储服务中也有一个接口叫getData,我们的用户服务只想调用到我们的订单服务的getData,不想调用到仓储服务的getData,这时我们可以用group分组来隔离。

   我们来看一下配置吧。只需要加入group:分组名称即可,nacos客户端不需要任何设置,这里需要注意的是你的父类项目依赖,2.1.0.RELEASE版本是没有group的,也无法设置group(反正我没找到什么好办法)。需要改配置为2.1.1.RELEASE

<!--spring cloud alibaba依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.1.1.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
spring:
  application:
    name: nacos-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.138.119:8848
        namespace: 0610f97e-c25d-4f49-bfb8-b340e3584b82
        group: pay
server:
  port: 8888

  ④.还有一个概念是service服务集群,什么意思呢?先上个图。

   就是什么呢,我们现在有两组集群,一个是北京的订单服务集群,一个是北京的用户服务集群,还有一个上海的订单服务集群,我们希望北京的用户集群,优先去调用北京的订单系统,上海的优先调用上海的集群服务。并不希望我们跨地区远程调用(如果组内实在没有服务了,也可以调用,但是优先考虑同一集群的)。配置文件如下所示

spring:
  application:
    name: nacos-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.138.119:8848
        namespace: 0610f97e-c25d-4f49-bfb8-b340e3584b82
        group: pay
        cluster-name: BJ-cluster
server:
  port: 8888

  ⑤.再就是版本了,也是我知道的最后一个了,比如我们要进行灰度发布了,我们有100台服务集群,但是这次新功能很重要,不能一次性全部更新,我们选择灰度发布,我们选出5台服务作为新版发布的,我们定义为V2,内部接口时不兼容的,所以我们只能要5台新用户服务去调用5台订单服务,原有的95台用户服务还是继续调用那95台订单服务,我们定义为V1。

  我们来看一下代码配置

spring:
  application:
    name: nacos-order
  cloud:
    nacos:

      discovery:
        server-addr: 192.168.138.119:8848
        namespace: 0610f97e-c25d-4f49-bfb8-b340e3584b82
        group: namespace-one
        cluster-name: BJ-cluster
        metadata:
          version: v1
server:
  port: 8888

  剧透一下,nacos是用map来进行隔离的,大致结构式Map<namespace,Map<group::serviceName,Service>>,也是我们为什么cluster-name和版本为什么做不到完全隔离,后面的源码博客会说详细说这个问题的。

总结:

  这次我们主要说了nacos的使用,原理?原理还没说,等到我们的源码带着说一下吧,不难,就是一个心跳的问题。nacos要知道namespace、group、cluster-name、版本主要是用来做什么的,什么时候我们该选择什么。下次我们来说一下Ribbon,也就是我们服务直接调用的一个插件。

 

最进弄了一个公众号,小菜技术,欢迎大家的加入

Guess you like

Origin www.cnblogs.com/cxiaocai/p/12283412.html