当前位置:   article > 正文

consulandnacos_nacos 和 consul

nacos 和 consul

consul注册中心

概述

​ Consul 是由 HashiCorp 基于 Go 语言开发的,支持多数据中心,分布式高可用的服务发布和注册服务软件。用于实现分布式系统的服务发现与配置。 使用起来也较 为简单。具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署 。
官网地址: https://www.consul.io

创建两个调用方和被调用方

​ 创建两个类分别为consul_provider(被调用方),consul_consumer(调用方),名称自定义,然后分别导入相关的springboot启动依赖以及consul的客户端依赖

<dependencies>

        <!--consul 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--spring boot web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

​ 在创建相关的yml配置,在里面配置consul调用主机地址,主机名,端口号以及调用的名称

server:
  port: 8000

spring:
  cloud:
    consul:
      host: localhost # 主机名
      port: 8500 # 端口号
      discovery:
        service-name: ${spring.application.name} # 注册到服务方的地址
        prefer-ip-address: true # 注册的ip
  application:
    name: consul_provider # 两个不同的类名称不一样,如果是consul_consumer的话名字就死活consul_consumer
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

启动consul

​ 再启动consul 的时候和其他的启用方式不同,需要使用到cmd,winds+R——》cmd,然后找到consul解压之后的consul.exe的目录,输入启动的命令consul agent -dev回车启动

在调用方完成调用

​ 然后在consumer里面controller完成调用代码如下

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    //同样也需要用到RestTemplate完成调用,先在consumer里面创建一个包config创建类RestTemplateConfig然后在里面创建一个对象并且返回RestTemplate
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("goods/{id}")
    public Goods findGoodsById(@PathVariable("id") int id){

        System.out.println("findGoodsById:"+id);
        List<ServiceInstance> instances = discoveryClient.getInstances("consul_provider");
        if(instances==null || instances.size()==0){
            return null;
        }
        ServiceInstance serviceInstance = instances.get(0);
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();
        String url="http://"+host+":"+port+"/goods/findOne/"+id;

        Goods goods = restTemplate.getForObject(url, Goods.class);

        return goods;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

​ 创建调用的类RestTemplateConfig注册到Bean,方便在后面调用

package com.miracle.consumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

​ 最后在启动一下代码看看是否成功,如果在consul控制台里面刷新出来被调用的值就说明成功了

Nacos注册中心

概述

​ Nacos(Dynamic Naming and Configuration Service) 是阿里巴巴2018年7月开源的项目。它专注于服务发现和配置管理领域 致力于帮助您发现、配置和管理微服务。Nacos 支持几乎所有主流类型的“服务”的发现、配置和管理。一句话概括就是Nacos = Spring Cloud注册中心 + Spring Cloud配置中心。
官网:https://nacos.io/
下载地址: https://github.com/alibaba/nacos/releases

创建调用和被调用类

​ 创建类被调用者nacos_provider和调用者nacos_consumer,同样名称也是可以自定义的,然后倒入相关的依赖

<dependencies>

        <!--nacos-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!--springboot web启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--这个是做测试的时候需要的springboot的依赖,如果不做测试就不需要-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

启动注册中心Nacos

​ 在本地找到下载的nacos找到bin目录下的startup.cmd,双击启动即可,使用网址为http://http://localhost:8848/nacos,找到下面找到服务列表查看是否成功就可以了

完成调用

​ 同样需要使用restTemplate实现远程调用,在controller里面实现代码为

package com.miracle.nacosconsumer.controller;

import com.miracle.nacosconsumer.domain.Goods;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/goods/{id}")
    public Goods findGoodsById(@PathVariable("id") int id){
        System.out.println("findGoodsById:"+id);
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos_provider");
        if(instances==null || instances.size()==0){
            return null;
        }
        ServiceInstance serviceInstance = instances.get(0);
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();
        String url="http://"+host+":"+port+"/goods/findOne/"+id;

        Goods goods = restTemplate.getForObject(url, Goods.class);

        return goods;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

String host = serviceInstance.getHost();
String url=“http://”+host+“:”+port+“/goods/findOne/”+id;

    Goods goods = restTemplate.getForObject(url, Goods.class);

    return goods;
}
  • 1
  • 2
  • 3
  • 4

}

yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr:  127.0.0.1:8848 # 配置nacos 服务端地址
  application:
    name: nacos-provider # 服务名称
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/564234
推荐阅读
相关标签
  

闽ICP备14008679号