原文地址:http://blog.csdn.net/qq_18675693/article/details/53282031

本案例将打架一个微服务框架,参考来源官方参考文档
微服务:是什么?网上有一堆资料。不做叙述。
标题提到的框架是spring-cloud-netflix相关开源框架。
demo源码github

摘自官方说明:

Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with battle-tested Netflix components. The patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon)..

通过上述可以了解到:

Eureka:服务发现
Hystrix:断路器
Zuul:智能路由
Ribbon:客户端负载均衡

先图话后

Eureka:实际上在整个过程中维护者每个服务的生命周期。每一个服务都要被注册到Eureka服务器上,这里被注册到Eureka的服务又称为Client。Eureka通过心跳来确定服务是否正常。Eureka只做请求转发。同时Eureka是支持集群的呦!!!
Zuul:类似于网关,反向代理。为外部请求提供统一入口。
Ribbon/Feign:可以理解为调用服务的客户端。
Hystrix:断路器,服务调用通常是深层的,一个底层服务通常为多个上层服务提供服务,那么如果底层服务失败则会造成大面积失败,Hystrix就是就调用失败后触发定义好的处理方法,从而更友好的解决出错。也是微服务的容错机制。

多说不如看代码

下面我们将搭建一个单Eureka服务器的案例,我们将会注册两个微服务,并配置Zuul。
目标:
1.模拟外部请求,访问暴漏端口(Eureka监听端口)访问其中一个内部服务。
2.演示服务间如何通过Feign进行调用。
综述:
我们要写的包括:
一个Eureka服务器
两个微服务

抽取出公共的POM部分,让我们专注重点

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xbz.eureka.demo</groupId>
<artifactId>demo-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<java.version>1.7</java.version>
<file.encoding>UTF-8</file.encoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入的依赖 -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${file.encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50


Eureka单例服务器(即不将自身作为一个Client)
我们只需要三个步骤就可以启动服务器
1.POM引入

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.搞个Main启动类
@EnableEurekaServer
@EnableZuulProxy

package com.xbz.eureka.demo.server;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableEurekaServer
@EnableZuulProxy
public class EnurekaServer {
public static void main(String[] args) {
new SpringApplicationBuilder(EnurekaServer.class).web(true).run(args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.重点:application.yml

#单例模式启动Eureka Server
server:
port: 8761 #启动端口
eureka:
client:
registerWithEureka: false #false:不作为一个客户端注册到注册中心
fetchRegistry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
zuul:
prefix: /techouse #为zuul设置一个公共的前缀
#ignoredServices: '*'
routes:
cloud-client: #随便定义,当不存在serviceId时,默认该值为serviceId(就是注册服务的名称,属性spring.application.name)
path: /usersystem/** #匹配/techouse/usersystem/** 均路由到cloud-client
serviceId: cloud-client #指定路由到的serviceId
ribbon:
eureka:
enabled: false #配置zuul路由时用将此属性设置为false cloud-client:
ribbon:
listOfServers: 127.0.0.1:8800 #为cloud-client服务指定一组服务地址,应该是用于负载均衡
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21


两个微服务
服务cluod-client
客户端pom

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这个服务我们在上面的application.yml中配置了,等下我们就可以用Eureka服务器地址访问此服务。
该服务包含一个HellController,和ServerApplication(启动类)
HelloController.java

package com.xbz.eureka.demo.client.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController
public class HelloController {
@RequestMapping("/hello/{fallback}")
@HystrixCommand(fallbackMethod="helloFallbackMethod")/*调用方式失败后调用helloFallbackMethod*/
public String hello(@PathVariable("fallback") String fallback){
if("1".equals(fallback)){
throw new RuntimeException("...");
}
return "hello";
} public String helloFallbackMethod(String fallback){
return "fallback 参数值为:"+fallback;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

ServerApplication.java
@EnableEurekaClient,确保该应用注册到Eureka服务器
@EnableHystrix 断路器生效

package com.xbz.eureka.demo.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

重要的application.xml

server:
port: 8800
spring:
application:
name: cloud-client #为你的应用起个名字,该名字将注册到eureka注册中心
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #去哪里注册,eureka服务地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

服务cluod-client-consumer
这个服务要调用cloud-client服务中的/hello/{fallback}方法
使用@Feign(“服务名称”),接口中随便定义方法,然后@RequestMapping(“/hello/{fallback}”)即可,内部调用就是如此简单。
涉及HelloService(演示如何使用@Feign)调用服务。HelloController演示调用服务的输出结果,以及ConsumerApplication启动类。
HelloService.java

package com.xbz.eureka.demo.consumer;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("cloud-client")
public interface HelloService {
@RequestMapping("/hello/{fallback}")
public String hello(@PathVariable("fallback") String fallback);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

HelloController.java

package com.xbz.eureka.demo.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloServcie;
@RequestMapping("/test/{fallback}")
public String hello(@PathVariable("fallback") String fallback){
String res=helloServcie.hello(fallback);
return "调用服务结果为"+res;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

启动类ConsumerApplication.java
@EnableEurekaClient注册到Eureka
@EnableFeignClients 内部使用Feign调用服务
@EnableZuulProxy 必须要有这个注解

package com.xbz.eureka.demo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

重要application.xml

server:
port: 8080
spring:
application:
name: cloud-consumer #为你的应用起个名字,该名字将注册到eureka注册中心
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
client:
serviceUrl:
defaultZone: http://hadoopMaster:8761/eureka/ #去哪里注册,eureka服务地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12


至此,我们的服务就搭建起来了!!!
按顺序启动三个类:
EurekaServer
ServiceApplication
ConsumerApplication
正常情况下,访问http://127.0.0.1:8761将看到如下界面

图中展示了我们启动的两个服务。
1.访问http://127.0.0.1:8080/test/1测试服务内部调用,且失败会调用fallback方法返回值。
2.访问http://127.0.0.1:8080/test/2测试服务内部调用,则正常调用cloud-client的服务。
3.我们可以使用统一入口,调用cloud-client服务:
访问的url:http://127.0.0.1:8761/techouse/usersystem/hello/2
以上便是一个简单的微服务搭建喽!!!

微服务:Eureka+Zuul+Ribbon+Feign+Hystrix构建微服务架构的更多相关文章

  1. Spring Cloud 微服务:Eureka+Zuul+Ribbon+Hystrix+SpringConfig实现流程图

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  2. springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig

    原文地址:http://blog.csdn.net/yp090416/article/details/78017552 springcloud微服务实战:Eureka+Zuul+Ribbon+Hyst ...

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

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

  4. 玩转Windows服务系列——使用Boost.Application快速构建Windows服务

    玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...

  5. 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)

    前言 上两章节,介绍了下关于注册中心-Eureka的使用及高可用的配置示例,本章节开始,来介绍下服务和服务之间如何进行服务调用的,同时会讲解下几种不同方式的服务调用. 一点知识 何为负载均衡 实现的方 ...

  6. 服务消费者(RestTemplate+Ribbon+feign)

    负载均衡 ​ spring cloud 体系中,我们知道服务之间的调用是通过http协议进行调用的.注册中心就是维护这些调用的服务的各个服务列表.在Spring中提供了RestTemplate,用于访 ...

  7. Spring Cloud构建微服务架构

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

  8. 基于Spring Cloud和Netflix OSS 构建微服务-Part 1

    前一篇文章<微服务操作模型>中,我们定义了微服务使用的操作模型.这篇文章中,我们将开始使用Spring Cloud和Netflix OSS实现这一模型,包含核心部分:服务发现(Servic ...

  9. 使用 API 网关构建微服务-2

    「Chris Richardson 微服务系列」使用 API 网关构建微服务 Posted on 2016年5月12日 编者的话|本文来自 Nginx 官方博客,是微服务系列文章的第二篇,本文将探讨: ...

随机推荐

  1. python 对字典分别按照key值、value值进行排序

    1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...

  2. 利用jquery实现前端同步请求---判断姓名是否为空并设置事件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...

  3. Java 网络编程初探

    Java 网络编程 网络编程 网络编程:进行服务器端与客户端编程的开发操作实现. java.net:网络操作包 B/S结构: 浏览器/服务器模式(Browser/Server) 不在开发客户端代码 开 ...

  4. 利用Azure虚拟机安装Dynamics 365 Customer Engagement之五:安装SQL Server

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  5. Flutter 基础控件

    内容: Button Image.Icon Switch.Checkbox TextField Form 1.Button RaisedButton 漂浮按钮 FlatButton 扁平按钮 Outl ...

  6. Consul作为配置中心,配置Asp.Net Core应用程序

    前言 最近项目逐步转向基于.Net Core,目前dotnet core 虽然已出3.0了但还没有特别成熟的框架,要实现微服务,必须要解决配置中心的问题 .不管是不是微服务,节点多了配置文件一个个更改 ...

  7. 流程控制语句if基本概述

    目录 1. 流程控制语句if基本概述 2. 流程控制语句if文件比较 判断文件是否存在,返回方式 使用变量的方法进行判断 请输入你要备份的数据库名称: wordpress 请输入你要备份的数据库密码: ...

  8. ORA-04045: errors during recompilation/revalidation of LBACSYS.LBAC_EVENTS

    使用orachk工具检查数据库实例的时候,发现报告里面有类似下面这样一些错误(最近有给Oracle 10g应用补丁PSU 10.2.0.5.180717,不清楚是这个产生的还是其他原因导致),使用脚本 ...

  9. jstree级联禁用后代节点的选择框

    用jstree+jquery,做的树形展示. 这个话题,在Stack Overflow上有问答,要获取要禁用的节点,然后用获取子节点方法遍历后代节点,设置禁用选择框. 之后发现,jstree的获取子节 ...

  10. 图像处理&计算机视觉中upscale,downscale的翻译理解

    最近在看SAN网络(Second-order Attention Network for Single Image Super-Resolution)的论文,其中的Upscale module理解的不 ...