1. Consul介绍  

    •  Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality. Each of these features can be used individually as needed, or they can be used together to build a full service mesh. Consul requires a data plane and supports both a proxy and native integration model. Consul ships with a simple built-in proxy so that everything works out of the box, but also supports 3rd party proxy integrations such as Envoy. 以上是官网介绍,大概意思是:Consul是一个service mesh 解决方案,包含服务发现、配置、外部服务;这些功能可以按需单独使用。
    • Consul核心能力包括:服务发现、健康检查、KV存储、安全通信、多数据中心。本章主要使用服务发现功能。Consul官网:https://www.consul.io/
  2. 2. Consul安装有两种方式:原生安装,docker安装,本例主要采用docker方式

    •   docker安装:安装方法参考:https://github.com/JensenShao/consul 。以单机模式安装Consul,第一步,获取镜像:docker pull wdijkerman/consul;第二步,docker宿主机上创建文件

      {
      "data_dir": "/consul/data",
      "log_level": "INFO",
      "client_addr": "0.0.0.0",
      "ports": {
      "dns": 53
      },
      "ui": true,
      "server": true,
      "bootstrap_expect": 1,
      "disable_update_check": true
      };第三步,启动镜像:docker run -u root -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h server1 -v /root/shaozj/consul/config/my_config.json:/consul/config/my_config.json:ro  --privileged=true wdijkerman/consul;更多参数配置可参考上面链接地址。
    • 原生安装方式参考:https://www.consul.io/docs/install/index.html

    3.spring 集成,springcloud对Consul的基本操作进行了封装,我们可以方便地使用consul的功能,本文中主要是使用consul的服务发现功能,客户端使用Springcloud feign做http客户端以及负载均衡,以用户管理为例描述服务发现功能。

    • 环境准备:本机安装Maven,JDK1.8,Intellij Idea环境;spring环境:spring boot使用2.0.8.RELEASE版本,spring cloud使用Finchley.SR2, 具体版本间的兼容关系请参考spring官网:http://spring.io/projects/spring-cloud,里面有明确说明
    • 创建工程骨架:创建父工程parent,创建三个module:user-api,user-service,user-consumer;其中user-api中定义了服务接口,user-service和user-consumer都会依赖这个接口;user-service是对于user-api中接口的实现;user-consumer主要通过feign来调用服务
    • 编写user-api代码:首先,由于api工程中会用到web的一些注解,需要pom中依赖spring-boot-starter-web。创建实体类User.java, 属性有id,name,创建接口UserService:
      public interface UserService{
      @GetMapping("/all")
      List<User> getAllUsers();
      }

      这样一个接口就完成了

      • 接下来编写user-service,主要是对user-api接口的实现,是服务的提供方。首先,pom中需要依赖user-api,另外需要依赖spring cloud consul服务发现组件:spring-cloud-starter-consul-discovery。创建UserService实现类UserServiceImpl:

        @RestController
        public class UserServiceImpl implements UserService{
        @Override
        public List<User> getAllUsers(){
        // 模拟DAO
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "consul"));
        userList.add(new User(1, "feign")); return userList;
        }
        }

        此处还需要添加一个springboot的启动类UserServiceApplication用于启动,

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

        其中@EnableDiscoveryClient注解作用是让注册中心能够发现并扫描该服务。最后添加spring配置application.yml:

        spring:
        application:
        name: user-service
        cloud:
        consul:
        host: 127.0.0.1
        port: 8500
        discovery:
        register: true
        hostname: 127.0.0.1 server:
        port: 10086

        其中register表示该工程下的服务会注册到注册中心

    • 实现user-consumer,该工程主要是服务的调用方。同样先在pom中添加依赖包:user-api, 同时需要依赖spring cloud feign:spring-cloud-starter-openfeign, 还需要依赖:spring-cloud-starter-consul-discovery作为http客户端。首先定义一个接口UserServiceClient继承UserService(此处为feign的使用方式):
      //其中@FeignClient注解表示要调用的微服务
      @FeignClient(value="user-service")
      public UserServiceClient extends UserService{}

      创建UserController调用服务:

      @RestController
      public class UserController{
      //自动注入feign客户端
      @Autowired
      private UserServiceClient userServiceClient; @GetMapping("/users")
      public List<User> getAllUsers(){
      //调用微服务
      return userServiceClient.getAllUsers();
      }
      }

      创建springboot启动类UserConsumerApplication:

      //@EnableFeignClients表示启用feign客户端,通过此注解可以使用feign
      @EnableFeignClients
      @SpringBootApplication
      public class UserConsumerApplication{
      public static void main(String[] args){
      SpringApplication.run(UserConsumerApplication.class, args);
      }
      }

      创建spring配置文件application.yml:

      spring:
      application:
      name: user-consumer
      cloud:
      consul:
      host: 127.0.0.1
      port: 8500
      discovery:
      # false表示该工程下的服务不会被注册
      register: false server:
      port: 10080
    • 分别启动user-service,user-consumer应用,在浏览器访问:http://localhost:10080/users,能够返回用户列表即表示成功

Spring Cloud 微服务一:Consul注册中心的更多相关文章

  1. SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)

    微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...

  2. spring cloud微服务实践二

    在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...

  3. Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结

    在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实 ...

  4. Spring Cloud微服务限流之Sentinel+Apollo生产实践

    Sentinel概述 在基于Spring Cloud构建的微服务体系中,服务之间的调用链路会随着系统的演进变得越来越长,这无疑会增加了整个系统的不可靠因素.在并发流量比较高的情况下,由于网络调用之间存 ...

  5. 如何优化Spring Cloud微服务注册中心架构?

    作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...

  6. spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现

    0.前言 什么是Spring Cloud Alibaba? Spring Cloud Alibaba 是阿里开源的,致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便 ...

  7. Spring Cloud 微服务架构全链路实践

    阅读目录: 1. 网关请求流程 2. Eureka 服务治理 3. Config 配置中心 4. Hystrix 监控 5. 服务调用链路 6. ELK 日志链路 7. 统一格式返回 Java 微服务 ...

  8. Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)

    导读 我们知道在基于Spring Cloud的微服务体系中,各个微服务除了在内部提供服务外,有些服务接口还需要直接提供给客户端,如Andirod.IOS.H5等等. 而一个很尴尬的境地是,如果直接将提 ...

  9. 全链路实践Spring Cloud 微服务架构

    Spring Cloud 微服务架构全链路实践Spring Cloud 微服务架构全链路实践 阅读目录: 网关请求流程 Eureka 服务治理 Config 配置中心 Hystrix 监控 服务调用链 ...

  10. Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin

    前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...

随机推荐

  1. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---24

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  2. 多线程中sleep和wait的区别

    前几天去UC笔试,有一道简答题问到了.之前还真一直没留意到这个问题,所以答得也不好. 无论学习什么都好,通过对比学习更有利于发现事物的共性和个性,对于知识点的理解更有明显效果(这也可能是UC笔试题上, ...

  3. WKWebView遇到的问题汇总

    一.手势放大缩小页面解决方法 1.通过操作webview中scrollview的代理方法来关闭 -(UIView *)viewForZoomingInScrollView:(UIScrollView ...

  4. NYOJ 27.水池数目-DFS求连通块

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  5. Hrbust 2319 Number Game(贪心)

    题目链接  Hrbust 2319 首先把二元组排序,$ai$大的排前面,$ai$相同的$bi$大的排前面. 这样的话就满足了Kim的取数顺序,即选每次$ai$最大的. 考虑得坏一些,在$ai$相同的 ...

  6. Spring Cloud Stream介绍-Spring Cloud学习第八天(非原创)

    文章大纲 一.什么是Spring Cloud Stream二.Spring Cloud Stream使用介绍三.Spring Cloud Stream使用细节四.参考文章 一.什么是Spring Cl ...

  7. mysql 性能容量评估

    性能容量评估   分析上线业务场景 评估数据库服务器所需性能指标 预估可能成为瓶颈的服务器资源 帮助数据库性能调优   数据库服务器硬件性能指标项: 磁盘IO性能 内存容量 CPU 网络吞吐量 磁盘容 ...

  8. ios界面笔记(一)

    基于一个简单视图的view解析

  9. Python moni模拟鼠标事件

    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 4 ...

  10. HDU2550 百步穿杨

    百步穿杨 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...