整理自该文章

一、Consul 服务端
接下来我们开发 Consul 的服务端,创建一个 spring-cloud-consul-producer 项目

1、添加依赖包

<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 Boot 版本使用的是 2.0.3.RELEASE,Spring Cloud 最新版本是 Finchley.RELEASE 依赖于 Spring Boot 2.x.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <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>

2、application.properties配置文件

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

Consul的地址和端口号默认是 localhost:8500 ,如果不是这个地址可以自行配置。
spring.cloud.consul.discovery.serviceName 是指注册到 Consul 的服务名称,消费端会根据这个名称来进行服务调用。

3、启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProducerApplication { public static void main(String[] args) {
SpringApplication.run(ConsulProducerApplication.class, args);
}
}  

添加了 @EnableDiscoveryClient 注解表示支持服务发现。

4、提供服务
我们创建一个 Controller,提供 hello 服务。

@RestController
public class HelloController { @RequestMapping("/hello")
public String hello() {
return "helle consul";
}
}

启动spring-cloud-consul-producer 项目,访问 http://localhost:8500,显示如下:

发现页面多了 service-producer 服务,点击进去后可以看到页面的服务提供者:

这样服务提供者就准备好了。

二、Consul 消费端
创建一个 spring-cloud-consul-consumer 项目,pom 文件和上面示例保持一致。

1、application.properties配置文件

spring.application.name=spring-cloud-consul-consumer
server.port=8503
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#设置不需要注册到consul中
spring.cloud.consul.discovery.register=false

客户端可以设置注册到 Consul 中,也可以不注册到 Consul 注册中心中,根据我们的业务来选择,只需要在使用服务时通过 Consul 对外提供的接口获取服务信息即可。

2、启动类

@SpringBootApplication
public class ConsulConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsulConsumerApplication.class, args);
}
}

3、进行测试
我们先来创建一个 ServiceController,试试如何去获取 Consul 中的服务。

@RestController
public class ServiceController { @Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient; /**
* 获取所有服务
*/
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("service-producer");
} /**
* 从所有服务中选择一个服务(轮询)
*/
@RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose("service-producer").getUri().toString();
} }

Controller 中有俩个方法,一个是获取所有服务名为service-producer的服务信息并返回到页面,一个是随机从服务名为service-producer的服务中获取一个并返回到页面。

添加完 ServiceController 之后我们启动项目,访问地址:http://localhost:8503/services,返回:

[{"serviceId":"service-producer","host":"DESKTOP-4RRQ4K1","port":8501,"secure":false,"metadata":{"secure":"false"},"uri":"http://DESKTOP-4RRQ4K1:8501","scheme":null}]

访问地址:http://localhost:8503/discover,返回:

http://DESKTOP-4RRQ4K1:8501

4、模拟调用服务端提供的 hello 方法

@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:8503/call,依次返回结果如下:

helle consul

说明我们已经成功的调用了 Consul 服务端提供的服务,并且实现了服务端的均衡负载功能。通过今天的实践我们发现 Consul 提供的服务发现易用、强大。

Spring Cloud Consul使用——服务注册与发现(注册中心)的更多相关文章

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

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

  2. 干货|基于 Spring Cloud 的微服务落地

    转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...

  3. 基于Spring Cloud的微服务落地

    微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...

  4. Spring Cloud Consul 之Greenwich版本全攻略

    什么是Consul Consul是HashiCorp公司推出的开源软件,使用GO语言编写,提供了分布式系统的服务注册和发现.配置等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全 ...

  5. Spring Cloud构建微服务架构

    Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...

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

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

  7. 服务注册发现、配置中心集一体的 Spring Cloud Consul

    前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...

  8. Spring Cloud Eureka 实现服务注册与发现

    微服务 是一种架构模式,跟具体的语言实现无关,微服务架构将业务逻辑分散到了各个服务当中,服务间通过网络层进行通信共同协作:这样一个应用就可以划分为多个服务单独来维护发布.构建一个可靠微服务系统是需要具 ...

  9. Spring Cloud构建微服务架构(一)服务注册与发现

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

随机推荐

  1. springboot自带定时任务和集成quartz

    1,springboot自带的定时任务  默认是单线程 有这个依赖就可以 <dependency> <groupId>org.springframework.boot</ ...

  2. 探索未知种族之osg类生物---呼吸分解之更新循环一

    上节总结 前几天我们大体上介绍完成了osg的事件循环的介绍,总结一下osg的时间循环主要就是得到平台(windows)的所有消息,并遍历所有的node的eventCallback,并对他们进行处理.接 ...

  3. usb协议栈学习笔记

    1.usb 集线器为什么一般都是只有4个扩展口? PC的根集线器可为每个A型连接器提供5V.500mA电源.一个总线供电的外部集线器可为每个端口提供100mA电流.由于USB为为外部集线器电路分配10 ...

  4. PowerShell工作流学习-2-工作流运行Powershell命令

    关键点: a)inlineScript 活动具有活动通用参数,但不具有PowerShell 通用参数,且inlineScript 脚本块中的命令和表达式不具有工作流的功能b)默认inlineScrip ...

  5. Python开发——7.迭代器、生成器和装饰器

    一.迭代器 1.迭代器协议 (1)迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么引起一个StopIteration移除异常以中止迭代. (2)可迭代对象:实现了迭 ...

  6. java_io

    JAVA IO流(一)参考文章:http://ifeve.com/java-io-network/,并发编程网原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: Java ...

  7. 关于SQL\SQL Server的三值逻辑

    在SQL刚入门的时候,我们筛选为某列值为NULL的行,一般会采用如下的方式: SELECT * FROM Table AS T WHERE T.Col=NULL 而实际上此种写法无法得到想要的结果.此 ...

  8. 使用Phabricator进行代码审查

    Pharicator 是FB的代码审查工具,主要开发者为Evan Priestley,是一个开源软件,可在Apache许可证第2版下作为自由软件分发.详细信息可查看官方文档.这里从应用的角度,一步一步 ...

  9. 关于JavaScript的操作

    一:js基础. 1. var是定义js变量的关键字. 如: var leng=5;定义一个变量为5 var length = 16; // Number 通过数字字面量赋值 var points = ...

  10. Android 框架式编程 —— 起篇

    一般的,在开发的时候,写过的代码在需求变更后,发现需要改动非常多的地方,那么说明之前的代码的架构肯定是存在问题的. 下面我们结合面向对象的六大基本原则谈Android 框架式编程.首先先介绍一下面向对 ...