consulandnacos
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>
在创建相关的yml配置,在里面配置consul调用主机地址,主机名,端口号以及调用的名称
server:port: 8000spring:cloud:consul:host: localhost # 主机名port: 8500 # 端口号discovery:service-name: ${spring.application.name} # 注册到服务方的地址prefer-ip-address: true # 注册的ipapplication:name: consul_provider # 两个不同的类名称不一样,如果是consul_consumer的话名字就死活consul_consumer
启动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@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate 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;}
}
创建调用的类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 {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}
最后在启动一下代码看看是否成功,如果在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>
启动注册中心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 {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate 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;}
}
String host = serviceInstance.getHost();
String url=“http://”+host+“:”+port+“/goods/findOne/”+id;
Goods goods = restTemplate.getForObject(url, Goods.class);return goods;
}
}
yml配置
spring:cloud:nacos:discovery:server-addr: 127.0.0.1:8848 # 配置nacos 服务端地址application:name: nacos-provider # 服务名称
consulandnacos相关推荐
最新文章
- Linux之bash shell基本命令
- 信息学奥赛C++语言:上课的人数
- 汇成企业网站管理系统v1.0源码
- 前世档案 (15 分)
- 外媒晒一加7 Pro相机实拍样张:不惧极速F1赛车
- mysql重置密码报错,吐血整理
- 两个Listbox的关联(省名 和 该省城市的对应)
- drools部署教程
- Rust: 在子线程启动子线程
- esp连接服务器的协议,【零知ESP8266教程】WIFI TCP协议通信 TCP服务器示例
- [置顶] 图灵访谈系列之九:CNode社区谈Node.js技术及生态
- Python之Sqlitespy连接并把Excel内容写到数据库的表中
- golang 腾讯AI 机器翻译调用 实现中英互译服务
- 实现炫酷的卡片式动画!
- No module named 'exceptions'
- 1944 Problem D	八进制
- 学习笔记:IDF 移动端UX设计 1.11 课程回顾
- Android 图片选择库美哭了
- 开源聚合路由 OpenMPTCProuter 配置使用
- 如何设置网络投票制作投票链接售价多少钱平台投票