SpringBoot + Spring Cloud Consul 服务注册和发现详细解析

网友投稿 258 2022-11-29


SpringBoot + Spring Cloud Consul 服务注册和发现详细解析

什么是Consul

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

Consul安装

官网(consul.io)最新版本1.8.0 提供了MacOS,Windows, Linux, 如果你不知道怎么安装,官方还提供了视频。

我这里使用docker安装,安装过程总结起来为三句话:

docker search consul

docker pull consul

docker run --name consul -d -p 8600:8500 consul

没问题的话,本机访问http://localhost:8600则可以打开consul自带的管理系统,默认情况下没有服务注册进来

准备工作新建父工程, 主要约定SpringCloud, SpringBoot版本号,我使用的是Hoxton.SR1, SpringBoot2.2

新建3个子Module,这里两个service,一个consumer, 两个service我用8001和8002端口来区分,主要是想在服务消费时反应客户端负载均衡。

项目结构如下图

父工程pom.xml

org.springframework.boot

spring-boot-dependencies

2.2.2.RELEASE

pom

import

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR1

pom

import

服务提供者

服务注册中心有了,那么我们来开发两个服务提供者,这里新建了两个Module,端口8001和8002。两个Module代码相同,主要为了演示负载使用。

新建Module,添加spring-cloud-starter-consul-disconvery依赖

pom.xml

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-consul-discovery

org.springframework.boot

spring-boot-starter-test

添加配置(application.yml)

server:

port: 8001

spring:

application:

name: consul-student-service

cloud:

consul:

port: 8600

host: 127.0.0.1

discovery:

service-name: ${spring.application.name}

修改启动类,添加服务

这里我直接写了一个测试接口放在启动类里。这里我只贴了端口8001的代码,8002代码结构相同,只是端口不同。

@SpringBootApplication

@EnableDiscoveryClient

@RestController

public class ConsulStudentService8001 {

public static void main(String[] args) {

SpringApplication.run(ConsulStudentService8001.class,args);

}

@GetMapping("/student/version")

public String version(){

return "8001,202007222300";

}

}

OK,到这一步,启动两个服务,不出异常的情况下,可在注册中心查看当前的服务实例。

Consul消费者

服务注册中心有了,服务提供http://者也有了,我们再来开发一个服务消费者。

新建Module,同样添加spring-cloud-starter-consul-disconvery依赖

pom.xml

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-consul-discovery

org.springframework.boot

spring-boot-starter-test

添加配置(application.yml)

server:

port: 8080

spring:

application:

name: consul-student-consumer

cloud:

consul:

host: 127.0.0.1

port: 8600

discovery:

service-name: ${spring.application.name}

# 不需要注册到consul中

register: false

修改启动类,调用服务

开发RestTemplate配置类,调用REST接口时使用。

@Configuration

public class ApplicationContextConfig {

@Bean

@LoadBalanced

public RestTemplate restTemplate(){

return new RestTemplate();

}

}

修改启动类

@SpringBootApplication

@EnableDiscoveryClient

@RestController

public class ConsulStudentConsumer {

public static void main(String[] args) {

SpringApplication.run(ConsulStudentConsumer.class,args);

}

@Autowired

RestTemplate restTemplate;

@GetMapping("/consul/student/version")

public String version(){

//这里使用服务实例名调用REST接口

return restTemplate.getForObject("http://consul-student-service/student/version",String.class);

}

}

OK, 这一步完成之后,可以启动消费者接口,刷新几次,从返回结果上能看出来是轮训调用服务提供者接口实例。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java Random.nextInt()方法的具体使用
下一篇:Spring MVC 拦截器 interceptor 用法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~