Giter Site home page Giter Site logo

springcloudexample's Introduction

springcloudexample

spring cloud 学习例子
在微服务架构中,需要几个关键的组件,服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,
由这几个组件可以组建一个简单的微服务架构。

服务发现(Eureka、consul)

consul-client(consul服务提供方)

声明式REST客户端-Feign的使用

  • Feign是一种声明式、模板化的HTTP客户端。
  • 在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。
  • eureka-consumer-feign(服务消费者-feign): http://localhost:2103/feign?name=jon
  • feign是自带断路器的,此版本hystrix是关闭的 ,如果使用feign想用断路器的话,可以在配置文件中开启它,配置如下:feign.hystrix.enabled=true

断路器(Hystrix)

智能路由(zuul)

智能路由(zuul)- 过滤器ZuulFilter

  • 过滤器类型返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:filterType如下:
  1. pre:路由之前
  2. routing:路由之时
  3. post: 路由之后
  4. error:发送错误调用
  5. filterOrder:过滤的顺序
  6. shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
  7. run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
  • 参考代码 DemoFilter
  • 修改请求地址
  1. http://localhost:3101/baidu?token=org.lv.jon.token
  2. http://localhost:3101/index?token=org.lv.jon.token
  3. http://localhost:3101/api-a/consumer?token=org.lv.jon.token
  4. http://localhost:3101/api-b/feign?name=jon&token=org.lv.jon.token

分布式配置中心(Spring Cloud Config)

在分布式系统中,spring cloud config 提供一个服务端和客户端去提供可扩展的配置服务。 我们可用用配置服务中心区集中的管理所有的服务的各种环境配置文件。配置服务中心采用Git的方式存储配置文件, 因此我们很容易部署修改,有助于对环境配置进行版本管理。

config-server

高可用配置中心 config-eureka-server 使用eureka做为配置服务注册中心,需要启动eureka-server配合使用。

访问配置信息的URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties application:表示应用名称,在client中通过spring.config.name配置,profile:表示获取指定环境下配置,例如开发环境、测试环境、生产环境 默认值default,实际开发中可以是 dev、test、demo、production等 label: git标签,默认值master
  • http://localhost:8888/config-client/dev

config-client

config-client 配置文件必须要使用bootstrap.properties
高可用配置中心客户端 config-eureka-client 使用eureka做为配置服务注册中心,需要启动eureka-server,config-eureka-server配合使用。 http://localhost:8001/jon

动态更新配置参数

  1. 在客户端启动类添加注解 @RefreshScope 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。
  2. 在客户端pom增加spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。
  3. springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties中关闭安全认证:management.security.enabled=false
  4. 以post请求的方式来访问http://localhost:8001/refresh 就会更新修改后的配置文件。
  5. 查看更新后的参数 http://localhost:8001/jon

消息总线(Spring Cloud Bus)

服务链路追踪(Spring Cloud Sleuth)

高可用的服务注册中心

断路器监控(Hystrix Dashboard)

断路器聚合监控(Hystrix Turbine)

springcloudexample's People

Contributors

l316476844 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.