一、微服务架构

  简单的说,微服务是系统架构的一种设计风格,它的主旨是将一个原本独立的系统拆分为多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进行通信协作。被拆分的每一个小型服务都围绕着系统中的某一项或耦合度较高的业务功能进行构建,并且每个服务都维护着自身的数据存储、业务开发、自动化测试案例以及独立部署机制。由于有了轻量级的通信协作基础,所以这些微服务可以使用不同的语言来编写。

二、Spring Cloud简介

  Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具。它为微服务架构中设计的配置管理、服务治理、断路器、智能路由(路由网关)、微代理、控制总线、全局锁、决策竞选、分布式会话、和集群状态管理等操作提供了一种简单的开发方式。

  介绍一下Spring Cloud Netflix(核心组件):

  • Eureka:服务治理组件,包含服务注册中心、服务注册与发现机制的实现。
  • Hystrix:容错管理组件,实现断路器模式,帮助服务依赖中出现的延迟和为故障提供强大的容错能力。
  • Ribbon:客户端负载均衡的服务调用组件
  • Feign:基于Ribbon和Hystrix的声明式调用组件
  • Zuul:网关组件,提供智能路由、访问过滤等功能
  • Archaius:外部化配置组件

  当然还有其他组件,比如说Spring Cloud Config(配置管理工具)、Spring Cloud Bus(事件、消息总线) ……

三、Spring Cloud Eureka(微服务架构中最为核心和基础的模块)

  Spring Cloud Eureka 是Spring Cloud Netflix微服务套件中的一部分,主要负责完成微服务架构中的服务治理功能。Spring Cloud通过为Eureka增加了Spring Boot风格的自动化配置,我们只需要通过简单引入依赖和注解配置就能让Spring Boot构建的微服务应用轻松地与Eureka服务治理进行整合。

  Eureka服务端,服务注册中心

  Eureka客户端,处理服务的注册于发现

四、项目结构

  

  eureka-server:服务注册中心模块

  serice-hello:spring boot应用  也为客户端服务模块

  ribbon-consumer:负载均衡实现模块

  版本说明:Spring Cloud版本采用的是Brixton.SR5版本,基于Spring Boot 1.3.7版本

  注意:使用Spring Cloud版本的时候要注意Sring Boot的版本(之前下载好依赖之后,项目运行就会报错,这是版本不兼容的问题)

五、Eureka实现

  创建一个空项目,添加一个spring boot模块,命名为eureka-server,在pom.xml中加入必要的依赖:

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

  通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话:

package com.stonegeek;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* Created by StoneGeek on 2018/5/28.
* 博客地址:http://www.cnblogs.com/sxkgeek
*/ @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

  配置application.properties(eureka.client.register-with-eureka:由于该应用是注册中心,所以设置为false,代表不向注册中心注册自己,eureka.client.fetch-registry:由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false)

server.port=1111

eureka.instance.hostname=localhost

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#spring.application.name=eureka-server
#
#server.port=1111
#
#eureka.instance.hostname=peer1
#
#eureka.client.service-url.defaultZone=http://peer2:1112/eureka

  完成上述配置之后,启动应用并访问http://localhost:1111/。可以看到Eureka信息面板,其中Instances  currently registered with Eureka栏是空的,说明该注册中心还没有注册任何服务

  

   创建注册服务提供者

    在新建一个spring boot 模块,在pom文件中添加模块依赖:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

  新建hellocontroller.class,通过注入DiscoveryClient对象,在日志中打印出服务的相关内容

package com.stonegeek.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.logging.Logger; /**
* Created by StoneGeek on 2018/5/27.
* 博客地址:http://www.cnblogs.com/sxkgeek
*/
@RestController
public class HelloController {
private final Logger logger =Logger.getLogger(String.valueOf(getClass()));
@Autowired
private DiscoveryClient client; @RequestMapping(value = "hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance=client.getLocalServiceInstance();
logger.info("/hello, host:"+instance.getHost()+", service_id:"+instance.getServiceId()+"service_port:"+instance.getPort());
return "hello world ";
}
}

  在主类中通过加上@EnableDiscoveryClient注解,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例),才能实现上述controller中对服务信息的输出

package com.stonegeek;

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

  最后,我们需要在application.properties配置文件中,通过spring.application.name属性来为服务命名,此处命名为service-hello,再通过eureka.client.serviceUrl.defaultZone属性来指定服务注册中心地址,这里使用之前构建的服务注册中心地址,

spring.application.name=service-hello
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

  之后,我们分别启动上述两个模块、服务注册中心模块(eureka-server),客户端服务模块(service-hello),

  service-hello控制台打印:

  eureka-server打印:

  eureka信息面板:

  访问localhost:8080/hello ,service-hello控制台打印:

  以上输出内容就是之前我们在helloController中注入的DiscoveryClient接口对象,从服务注册中心获取的服务相关信息

六、Ribbon负载均衡实现

  上述我们已经有了服务注册中心和服务提供者,现在就来构建一个服务消费者,而且Ribbon是一个基于HTTP和TCP的客户端负载均衡器

  创建一个springboot的基础工程来实现服务消费者,命名为ribbon-consumer,并在pom.xml中添加相关依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

  在应用主类RibbonConsumerApplication中通过@EnableDiscoveryClient注解让该应用注册为Eureka客户端应用,以获得服务发现能力,同时在主类中创建RestTemplate的spring bean 实例,并通过@LoadBanlanced注解开启客户端负载均衡。

package com.stonegeek;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; /**
* Created by StoneGeek on 2018/5/28.
* 博客地址:http://www.cnblogs.com/sxkgeek
*/ @EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication { @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}

  创建ConsumerController类并实现/ribbon-consumer接口。在该接口中,通过在上面创建的RestTemplate来实现对SERIVCE-HELLO服务提供的/hello接口进行调用。可以看到这里访问的地址是服务名SEVICE-HELLO,而不是一个地址,这在服务治理框架中是一个很重要的特性

package com.stonegeek.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* Created by StoneGeek on 2018/5/28.
* 博客地址:http://www.cnblogs.com/sxkgeek
*/
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://SERVICE-HELLO/hello",String.class).getBody();
}
}

  配置application.properties ,配置Eureka服务注册中心的位置,需要与之前SERVICE-HELLO一样,否则发现不了该服务,同时设置该消费者端口我9000,不能与之前的应用端口冲突。

spring.application.name=ribbon-consumer

server.port=9000
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

  现在开始启动三个模块,首先启动服务注册中心,然后在IDEA中打包SERVICE-HELLO生成JAR文件,然后在命令行中输入java -jar 生成的JAR包 --server.port=8081,然后将8081修改为8082

  再启动ribbon-comsumer模块后,可以看到eureka信息面板,除了SERVICE-HELLO(两个实例单元,分别是8081,8082)、还多了一个RIBBON-COMSUMER服务:

  通过访问http://localhost:9000/ribbon-consumer发出GET请求,成功返回“ Hello,World”。此时在ribbon-consumer控制台看到:当前客户端维护的SERCVICE-HELLO的服务列表情况。其中包含了各个实例的位置,Ribbon就是按照此信息进行轮询访问的,以实现基于客户端的负载均衡:

  再发送几次请求,观察两个SERVICE-HELLO的控制台,可以发现Ribbon轮询访问这两个SERVICE实例:

  自此Eureka的 服务注册中心与发现、Ribbon的客户端负载均衡也就搭建完成了!!!后续还会补充其他核心组件的搭建!!!

  

  

  

  

  

SpringCloud之Eureka、Ribbon的更多相关文章

  1. SpringCloud学习之Ribbon

    一.负载均衡与Ribbon 负载均衡,在集群中是很常见的一个“名词”,顾名思义是根据一定的算法将请求分摊至对应的服务节点上,常见的算法有如下几种: 轮询法:所有请求被依次分发到每台应用服务器上,每台服 ...

  2. Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)

    Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...

  3. SpringCloud创建Eureka模块集群

    1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...

  4. SpringCloud中eureka配置心跳和剔除下线的服务的时间

    在默认的springCloud中eureka注册中心在服务下线时表现的非常不灵敏,用惯了dubbo的zk注册中心表示很不习惯,eureka设计的本意是在服务不会频繁上下线和网络稳定的内网,这种设计在生 ...

  5. springcloud(二) eureka的使用

    上一节讲到order微服务是通过rest调用user微服务的地址.但是,user微服务的地址是写死的, 如果user微服务集群的话,那么order微服务该如何调用呢?这个时候注册中心该上场了 演示eu ...

  6. spring-cloud配置eureka客户端

    spring-cloud配置eureka客户端 eureka用来发现其他程序 需要提前配置eureka服务端,具体看 https://www.cnblogs.com/ye-hcj/p/10292944 ...

  7. 浅谈SpringCloud (二) Eureka服务发现组件

    上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...

  8. SpringCloud之Eureka:集群搭建

    上篇文章<SpringCloud之Eureka:服务发布与调用例子>实现了一个简单例子,这次对其进行改造,运行两个服务器实例.两个服务提供者实例,服务调用者请求服务,使其可以进行集群部署. ...

  9. SpringCloud学习笔记(三、SpringCloud Netflix Eureka)

    目录: 服务发现简介 SpringCloud Netflix Eureka应用 Eureka高可用 Eureka源码分析 >>> Eureka Client初始化(客户端定时获取服务 ...

  10. springCloud 之 Eureka注册中心高可用配置

    springCloud的eureka高可用配置方案思路是:几个服务中心之间相互注册,比如两个注册中心,A注册到B上,B注册到A上,如果是三个注册中心则是:A注册到BC上,B注册到AC上,C注册到AB上 ...

随机推荐

  1. Tomcat性能调优参数简介

    近期,我们的一个项目进入了试运营的阶段,在系统部署至阿里云之后,我们发现整个系统跑起来还是比较慢的,而且,由于代码的各种不规范,以及一期进度十分赶的原因,缺少文档和完整的测试,整个的上线过程一波三折. ...

  2. requests + BeautifulSoup + json

    requests: response.text      以 unicode 格式显示响应的文本 response.content    以 二进制 格式显示响应的文本 BeautiSoup: sou ...

  3. Spring MVC+ajax进行信息验证

    本文是一个ajax结合Spring MVC使用的入门,首先我们来了解一下什么是Ajax AJAX 不是新的编程语言,而是一种使用现有标准的新方法.AJAX 最大的优点是在不重新加载整个页面的情况下,可 ...

  4. Net Core基于TopShelf程序运行于服务模式

    目录 Net Core基于TopShelf程序运行于服务模式 1 背景 2 优势 2.1 服务模式可设置重启条件 2.2 避免误操作 3.使用 3.1 GUI方式安装Topshelf包 4 配置 5 ...

  5. MySQL查看当前用户

    mysql> select current_user();+----------------+| current_user() |+----------------+| root@localho ...

  6. Android 点九图机制讲解及在聊天气泡中的应用

    点九图简介 Android为了使用同一张图作为不同数量文字的背景,设计了一种可以指定区域拉伸的图片格式".9.png",这种图片格式就是点九图. 注意:这种图片格式只能被使用于An ...

  7. 生成式学习算法(三)之----高斯判别分析模型(Gaussian Discriminant Analysis ,GDA)

    高斯判别分析模型(Gaussian Discriminant Analysis ,GDA) 当我们分类问题的输入特征$x $为连续值随机变量时,可以用高斯判别分析模型(Gaussian Discrim ...

  8. 如何使用rsync备份

    已知3台服务器主机名分别为web01.backup .nfs主机信息见下表: 角色 外网IP(NAT) 内网IP(LAN) 主机名 WEB eth0:10.0.0.7 eth1:172.16.1.7 ...

  9. MOOC 数据库系统笔记(二):数据库系统的基本结构及其演变发展

    数据库系统的结构抽象与演变 数据库的标准结构 DBMS管理数据的三个层次 1.External Level = User Level 某一用户能够看到与处理的数据,全局数据中的某一部分 2.Conce ...

  10. Elasticsearch(10) --- 内置分词器、中文分词器

    Elasticsearch(10) --- 内置分词器.中文分词器 这篇博客主要讲:分词器概念.ES内置分词器.ES中文分词器. 一.分词器概念 1.Analysis 和 Analyzer Analy ...