Spring 官方文档

注册中心model

buildscript {ext {springBootVersion = '1.5.6.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-starter-eureka-server')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
package registerserver;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** 注册中心*/
@EnableEurekaServer
@SpringBootApplication
public class RegisterCenterApplication {public static void main(String[] args) {SpringApplication.run(RegisterCenterApplication.class, args);}
}

application.yml

server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

项目目录结构

配置中心model

buildscript {ext {springBootVersion = '1.5.6.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-config-server')compile('org.springframework.cloud:spring-cloud-starter-eureka')compile('org.springframework.boot:spring-boot-starter-web')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
package com.cn.config;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigeServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigeServerApplication.class, args);}
}

application.yml


# 自定义变量
register-center-url: http://localhost:8761/eureka/
# 注册中心地址
eureka.client.serviceUrl.defaultZone: ${register-center-url}
# 配置中心存储配置文件的方式
spring.profiles.active: native
# 应用名称
spring.application.name: config-server
# 端口
server.port: 8888
#本地配置文件存放路径 (${spring.profiles.active} 为native 是才有效)
spring.cloud.config.server.native.searchLocations: classpath:/config
zuul.ignore-local-service=

项目目录结构

zuul网管 model

buildscript {ext {springBootVersion = '1.5.7.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'group = 'com.cn.pppcar'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-starter-zuul')compile('org.springframework.cloud:spring-cloud-starter-eureka')compile('org.springframework.cloud:spring-cloud-starter-config')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
package com.cn.pppcar;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulGatewayApplication {public static void main(String[] args) {SpringApplication.run(ZuulGatewayApplication.class, args);}@Beanpublic AccessFilter accessFilter() {return new AccessFilter();}
}
package com.cn.pppcar;import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.servlet.http.HttpServletRequest;/*** Created by nurmemet on 9/19/2017.*/public class AccessFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger(AccessFilter.class);@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return -10;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));return null;}}

bootstrap.properties

# Flag to say that remote configuration is enabled. Default true;
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
# Name of application used to fetch remote properties.
# 远程配置文件名称  如 service0-{profile}.properties
# @Value("${spring.application.name:application}")
#
spring.cloud.config.name=zuul-gateway
# The URI of the remote server (default http://localhost:8888). 配置服务器
spring.cloud.config.uri=http://localhost:8888
# 要启用的环境(test,pro,dev 之类的) 配置服务器得有 ${spring.cloud.config.name}-{spring.cloud.config.profile}.properties文件
spring.cloud.config.profile=test
#spring.cloud.config.label=config-label-test

在配置中心的配置

zuul:ignoredServices: '*'routes:service0:path: /hello/**stripPrefix: false
#      serviceId: service0
#也可以用serviceIdurl: http://localhost:8891/       sensitive-headers: Cookie,Set-Cookie
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/spring:application:name: api-gateway
server:port: 8002

项目目录结构

service0 model

buildscript {ext {springBootVersion = '1.5.6.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-starter-eureka')compile('org.springframework.cloud:spring-cloud-starter-config')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
package com.cn.nur;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class Service0Application {public static void main(String[] args) {SpringApplication.run(Service0Application.class, args);}}
package com.cn.nur;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created by nurmemet on 9/12/2017.*/
@RestController
@RequestMapping("hello")
public class MyController {@Value("${message}")String msg;@RequestMapping("hello")public String hello(){return "hello "+ msg;}}

bootstrap.yml

# Flag to say that remote configuration is enabled. Default true;
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
# Name of application used to fetch remote properties.
# 远程配置文件名称  如 service0-{profile}.properties
# @Value("${spring.application.name:application}")
#
spring.cloud.config.name=service0
# The URI of the remote server (default http://localhost:8888). 配置服务器
spring.cloud.config.uri=http://localhost:8888
# 要启用的环境(test,pro,dev 之类的) 配置服务器得有 ${spring.cloud.config.name}-{spring.cloud.config.profile}.properties文件
spring.cloud.config.profile=test
#spring.cloud.config.label=config-label-test

在配置中心的配置

eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
server:port: 8891spring.application.name: service0
message: nurmemet-test

依次启动 注册中心,配置中心,zuul网管,service0
打开浏览器 localhost:8671 所有服务是否注册成功
浏览器输入 localhost:8002/hello/hello
网管会把这个请求转发到 localhos:8891/hello/hello
所有hello开头的请求都会转发到service0微服务中去

Spring Cloud Zuul相关推荐

  1. Zuul spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL解决方案

    Zuul spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL解决方案 参考文章: (1)Zuul spring clo ...

  2. Spring Cloud Zuul中使用Swagger汇总API接口文档

    有很多读者问过这样的一个问题: 虽然使用Swagger可以为Spring MVC编写的接口生成了API文档,但是在微服务化之后,这些API文档都离散在各个微服务中,是否有办法将这些接口都整合到一个文档 ...

  3. Spring Cloud -Zuul

    服务网关和zuul 一个角色来充当request的请求入口,处理非业务功能的场所(防刷,协议监控) 要素: 稳定性,高可用 性能.并发性 安全性 扩展性 常用的网关方案 Nginx+Lua(性能极高, ...

  4. Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式

    时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...

  5. 关于Spring Cloud Zuul网管上传文件乱码问题

    Spring Cloud Zuul现在对于上传文件有两种处理方式,一种是用spring mvc,另一种是zuulServlet.spring mvc对文件处理不是很好,会导致乱码问题,zuulServ ...

  6. Spring Cloud Zuul The 'Access-Control-Allow-Origin' header contains multiple values

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> 发布Spring Cloud Zuul的时候,前端遇到如下错误:  The 'Access-Control-Al ...

  7. Spring Cloud Zuul支持–配置超时

    Spring Cloud为Netflix Zuul提供了支持 -Netflix Zuul是用于创建具有路由和过滤功能的边缘服务的工具包. 在Spring Cloud站点上非常全面地记录了Zuul代理支 ...

  8. Spring Cloud Zuul的fallback优化

    如何在Zuul中使用fallback功能 我们在项目中使用Spring cloud zuul的时候,有一种这样的需求,就是当我们的zuul进行路由分发时,如果后端服务没有启动,或者调用超时,这时候我们 ...

  9. Spring Cloud Zuul重试机制探秘

    简介 本文章对应spring cloud的版本为(Dalston.SR4),具体内容如下: 开启Zuul功能 通过源码了解Zuul的一次转发 怎么开启zuul的重试机制 Edgware.RC1版本的优 ...

  10. Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式。

    时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...

最新文章

  1. UISegmentControl
  2. %matplotlib inline %config InlineBackend.figure_format = “retina为了将图片嵌入notebook及提高分
  3. MFC滑块的使用方式
  4. vc6.0能编辑html,科学网—VC6.0的18个实用小技巧 - 梁才的博文
  5. c语言next的用法,C语言strchr使用之Next查找和截断想要的字符串
  6. 【WordPress 建站教程】在 正文顶端或末尾插入固定的内容
  7. signature=01a8bb5f15835faa2985256d36b2fe94,Point of Maintenance
  8. mysql update修改数据_MYsql如何用update语句修改数据,值得一看
  9. ip、子网掩码、默认网关以及传输过程
  10. HUB、Switch、Router在OSI模型层次信息
  11. adb连接 vivo_vivo手机驱动
  12. android 系统GPS模块
  13. Android Studio User Manual
  14. 汇编程序:找出最小值
  15. Git出现 Your local changes to the following files would be overwritten by merge: con
  16. Word学习笔记:P12-合并打印信封与标签设定
  17. PMP 项目质量管理
  18. AS 编写 Xposed 插件需要修改的地方
  19. Protocol handler start failed
  20. 仿百度html页面,HTML实战篇:html仿百度首页

热门文章

  1. document.execCommand()方法处理Html数据
  2. 实用ExtJS教程100例-009:ExtJS Form无刷新文件上传
  3. WIN7如何替换开机登录画面
  4. 痞子衡嵌入式:第一本Git命令教程(7.1)- 清理之缓存(stash)
  5. 虚拟机中LINUX系统的安装
  6. 伪静态技术(SEO) 摘自:http://bbs.admin5.com/thread-8522290-1-1.html
  7. 设计模式 — 结构型模式 — 桥接模式
  8. OpenStack 系列文章
  9. 用 Flask 来写个轻博客 (6) — (M)VC_models 的关系(one to many)
  10. Python 进阶_OOP 面向对象编程_实例属性和方法