使用Consul提供注册和发现服务

什么是 Consul

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具(比如 ZooKeeper 等)。使用起来也较为简单。Consul 使用 Go 语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。

Consul 安装

访问 Consul 官网 ,根据操作系统类型,选择下载 Consul 的最新版本。我这里选择windows版本。

下载下来是一个zip压缩包,解压之后,是一个exe可执行文件。

打开CMD终端,进入consul.exe所在目录,执行如下命令启动Consul服务。

cd C:\consul_1.3.0_windows_amd64  # 进入consul.exe所在目录
consul agent -dev # 启动服务, -dev 表示开发模式运行,另外还有 -server 表示服务模式运行

启动过程信息如下图所示。

启动成功之后,访问 http://localhost:8500 , 可以查看 Consul 管理界面。

Consul 服务提供者

服务注册发现中心有了,现在我们来开发服务提供者。

新建项目 spring-cloud-consul-producer,添加以下依赖。

pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

说明:

spring-boot-starter-actuator 健康检查依赖于此包。

spring-cloud-starter-consul-discovery Spring Cloud Consul 的支持。

注意添加Spring Cloud的依赖配置,完整内容参见源码pom文件。

  <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

添加配置

在配置文件添加内容如下。

application.yml

server:
port: 8511
spring:
application:
name: spring-cloud-consul-producer
cloud:
consul:
host: localhost
port: 8500
discovery:
serviceName: service-producer # 注册到consul的服务名称

Consul 的地址和端口号默认是 localhost:8500 ,如果不是这个地址可以自行配置,consul服务会占用8502接口,所以不要用8501、8502。
spring.cloud.consul.discovery.serviceName 是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用。

修改启动类

修改启动器类,添加 @EnableDiscoveryClient 注解,开启服务发现支持。

package com.louis.spring.cloud.consul.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ConsuleProducerApplication { public static void main(String[] args) {
SpringApplication.run(ConsuleProducerApplication.class, args);
}
}

添加服务

新建 HelloController,提供 hello 接口, 返回 hello consul 字符串。

package com.louis.spring.cloud.consul.producer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @RequestMapping("/hello")
public String hello() {
return "hello consul.";
}
}

为了模拟注册均衡负载,复制一份上面的项目,重命名为 spring-cloud-consul-producer2 ,修改对应的端口为 8512,修改 hello 方法的返回值为:"helle consul two",修改完成后依次启动两个项目,启动成功之后刷新Consul管理界面,发现我们注册的service-producer服务,并有2个节点实例。

点击进入节点详情页面,可以看到我们注册的8511和8512两个服务提供者节点实例。

Consul 消费者

服务注册发现中心有了,服务提供者也有了,现在我们来开发服务消费者。

新建项目 spring-cloud-consul-comsumer,依赖同提供者。

添加配置

修改配置文件如下。

application.yml

server:
port: 8521
spring:
application:
name: spring-cloud-consul-consumer
cloud:
consul:
host: localhost
port: 8500
discovery:
register: false #设置不需要注册到 consul 中

客户端可以设置是否注册到 Consul 中,具体需要根据我们的业务来选择,一般在需要对外提供服务时进行注册。

启动器类

ConsuleConsumerApplication.java

package com.louis.spring.cloud.consul.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConsuleConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsuleConsumerApplication.class, args);
}
}

服务消费者

添加消费服务测试类,添加两个接口,一个查询所有我们注册的服务,另一个从我们注册的服务中选取一个服务,采用轮询的方式。

ServiceController.java

package com.louis.spring.cloud.consul.producer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ServiceController { @Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private DiscoveryClient discoveryClient; /**
* 获取所有服务
*/
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("service-producer");
} /**
* 从所有服务中选择一个服务(轮询)
*/
@RequestMapping("/discover")
public Object discover() {
return loadBalancerClient.choose("service-producer").getUri().toString();
}
}

添加完成之后,启动项目, 访问 http://localhost:8521/services,返回两个服务,分别是我们注册的8511和8512。

[{
"serviceId": "service-producer",
"host": "GG20J1G2E.logon.ds.ge.com",
"port": 8511,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://GG20J1G2E.logon.ds.ge.com:8511",
"scheme": null
}, {
"serviceId": "service-producer",
"host": "GG20J1G2E.logon.ds.ge.com",
"port": 8512,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://GG20J1G2E.logon.ds.ge.com:8512",
"scheme": null
}]

反复访问 http://localhost:8521/discover,结果交替返回服务8511和8512,因为默认的负载均衡器是采用轮询的方式。

http://GG20J1G2E.logon.ds.ge.com:8511
http://GG20J1G2E.logon.ds.ge.com:8512
...

8511 和 8512 两个服务会交替出现,从而实现了获取服务端地址的均衡负载。

大多数情况下我们希望使用均衡负载的形式去获取服务端提供的服务,因此使用第二种方法来模拟调用服务端提供的 hello 方法。

创建 CallHelloController.java

package com.louis.spring.cloud.consul.producer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class CallHelloController { @Autowired
private LoadBalancerClient loadBalancer; @RequestMapping("/call")
public String call() {
ServiceInstance serviceInstance = loadBalancer.choose("service-producer");
System.out.println("服务地址:" + serviceInstance.getUri());
System.out.println("服务名称:" + serviceInstance.getServiceId()); String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
System.out.println(callServiceResult);
return callServiceResult;
} }

使用 RestTemplate 进行远程调用。添加完之后重启 spring-cloud-consul-consumer 项目。

在浏览器中访问地址:http://localhost:8521/call,依次返回结果如下:

helle consul
helle consul two
...

服务注册和发现(Consul)的更多相关文章

  1. 分布式服务注册和发现consul 简要介绍

    Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式",内置了服务注册与发现框 架 ...

  2. 微服务之服务注册与发现--Consul(转载)

    http://blog.csdn.net/buxiaoxia/article/details/69788114 https://www.cnblogs.com/xiaohanlin/p/8016803 ...

  3. 5.服务注册与发现Consul,简学API,手动注册和删除服务

    package main import ( httptransport "github.com/go-kit/kit/transport/http" mymux "git ...

  4. C#使用Consul集群进行服务注册与发现

    前言 我个人觉得,中间件的部署与使用是非常难记忆的:也就是说,如果两次使用中间件的时间间隔比较长,那基本上等于要重新学习使用. 所以,我觉得学习中间件的文章,越详细越好:因为,这对作者而言也是一份珍贵 ...

  5. 温故知新,.Net Core遇见Consul(HashiCorp),实践分布式服务注册与发现

    什么是Consul 参考 https://www.consul.io https://www.hashicorp.com 使用Consul做服务发现的若干姿势 ASP.NET Core 发布之后通过命 ...

  6. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  7. 基于 Consul 实现 MagicOnion(GRpc) 服务注册与发现

    0.简介 0.1 什么是 Consul Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置. 这里所谓的服务,不仅仅包括常用的 Api 这些服务,也包括软件开发过程 ...

  8. Docker+Consul+Registrator 实现服务注册与发现

    Docker+Consul+Registrator实现服务注册与发现 逻辑图 实现nginx节点自动化加入容器IP代理 1.三台Consul agent server作为高可用通过Consul Tem ...

  9. Spring Boot + Spring Cloud 构建微服务系统(一):服务注册和发现(Consul)

    使用Consul提供注册和发现服务 什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul ...

随机推荐

  1. CC2 条理分明-----AACTP教你谈恋爱

      AACTP是一个神奇的地方,这里可以培养你的自信,培养你的沟通表达能力,培养你的领导管理能力:但是你不知道的是AACTP还可以培养你恋爱的本领.想知道是怎么回事吗?听我给你慢慢说来.我把恋爱分为三 ...

  2. C# : 泛型的继承关系实现的一个可以存放不同数据类型的链表

    以下定义的是一个链表结点类型: internal sealed class Node<T> { public T m_data; public Node<T> m_next; ...

  3. python中简单的递归(断点报错的小福利)

    首先要先理解什么是递归? 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 下面讲了一个很简单的递归函数 def clac(n): print(n) if int( ...

  4. PHP curl是什么

    PHP curl是什么 一.总结 一句话总结:PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯. libcurl库 允许你与各种的 ...

  5. maven的安装以及问题

    https://blog.csdn.net/machao0903/article/details/73368909https://www.cnblogs.com/jiejiecool/p/421885 ...

  6. [Solution] The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    HttpServlet需要tomcat等. 右键project点开properties>project facets> 在右侧栏的Runtime tab中勾选tomcat或者新建tomca ...

  7. gem "ransack"(4000✨) 简单介绍

    Object-based searching:演示. git:  https://github.com/activerecord-hackery/ransack Gorails视频和我的博客记录:ht ...

  8. codeforces668b //Little Artem and Dance// Codeforces Round #348

    题意:2种操作,转动或者奇偶位互换. 不论怎么交换,1的后两位一定是3,3的后两位一定是5.因此只要记录1,2的位置. //#pragma comment(linker,"/STACK:10 ...

  9. android studio 中的快捷键汇总

    1.Ctrl + B 跳入/跳出方法或者资源文件.将鼠标光标定位到某个方法或者资源id的调用处,按Ctrl + B,将会跳入该方法或者资源文件内部,功能等同于Ctrl + 鼠标左键.如果将鼠标光标定位 ...

  10. ssd制作数据和训练

    1.在/data/VOCdevkit下建立自己的数据集名称如MyDataSet,在MyDataSet目录下需包含Annotations.ImageSets.JPEGImages三个文件夹: 2.Ima ...