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. 使用git快捷方便的保存代码

    大家都在使用git保存和备份代码,下面我们就来学习下吧. 一.本地安装和配置git 1.安装git pacman -S git //如果没有问题的话就可以安装成功了 2.验证 git --versio ...

  2. python fromkeys的坑

    有个不定长的列表,想把列表中的每个值当做字典的key, 初始值为空列表,于是想到了fromkeys这个方法 In [337]: l = ['a','b','c'] In [338]: res = di ...

  3. Virtualbox 设置虚拟机上网并和主机互通(如ping等)

    我的主机是Ubuntu12.04, 安装virtualbox虚拟了一个xp系统.把xp作为一个开发用的机器,需要上网,并且和主机以及虚拟机之间互相访问. 1. 在virtual设置界面,将xp系统的网 ...

  4. 学习总结——JMeter做http接口压力测试

    JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需要考虑这几个方面: 场景设定 场景分单场景和混合场景.针对一个接口做压力测试就是单场景,针对一个流程做 ...

  5. luogu P2423 双塔

    题目描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“911”事件,Mr. F决定自己用水晶来搭建一座双塔.Mr. F有N块水晶,每块 ...

  6. 使用PreloadJS加载图片资源

    一. 使用createjs里的LoadQueue函数实现异步加载图片,监听加载进度 1.实例对象LoadQueue加载队列对象 var queue = new createjs.LoadQueue(f ...

  7. Using Single Alert For Messages And Confirmation Messages In Oracle Forms With Set_Alert_Button_Property

    Learn how to use single Oracle Form's Alert object for warning/information messages and confirmation ...

  8. 获取Android系统默认给每个app分配的内存上限

    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); int ...

  9. AlphaGo GITHUB

    AlphaGo GITHUB https://github.com/Rochester-NRT/AlphaGo

  10. 2016.7.12 错误:syntax error at or near “(”

    错误提示:     错误原因:漏了个,号.