springboot12-zuul
Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门,提供动态路由,监控,弹性,安全等的边缘服务
所有请求都经过网关(API Gateway)zuul,然后转发到各个子服务上去

1.注册中心eureka
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
@EnableEurekaServer//注解启动一个服务注册中心
@SpringBootApplication
public class EurekaApp { public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
#application配置
server.port=8888 #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false #服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
启动app,访问http://localhost:8888/

2.service1
@EnableDiscoveryClient//激活Eureka中的DiscoveryClient实现
@SpringBootApplication
public class Service1App { public static void main(String[] args) {
SpringApplication.run(Service1App.class, args);
}
}
server.port=1111
server.address=localhost
spring.application.name=service1 #注册中心地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
public class UserController {
private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class);
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/index")
public String index(HttpServletRequest request){
return "service1 被调用, 请求地址:" + request.getRequestURI();
}
}
3.service2
@EnableDiscoveryClient//激活Eureka中的DiscoveryClient实现
@SpringBootApplication
public class Service2App { public static void main(String[] args) {
SpringApplication.run(Service2App.class, args);
}
}
server.port=2222
server.address=localhost
spring.application.name=service2 #注册中心地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
public class User2Controller {
private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class);
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/index")
public String index(HttpServletRequest request){
return "service2 被调用, 请求地址:" + request.getRequestURI();
}
}
4.zuul网关
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
@EnableZuulProxy//声明zuul代理
@SpringBootApplication
public class ZuulMainApp { public static void main(String[] args) {
SpringApplication.run(ZuulMainApp.class, args);
}
}
#注册中心地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ #路由配置
#url形式
zuul.routes.baidu.path=/baidu/**
zuul.routes.baidu.url=http://www.baidu.com
#注册服务
zuul.routes.service1.path=/service1/**
zuul.routes.service1.serviceId=service1
zuul.routes.service2.path=/service2/**
zuul.routes.service2.serviceId=service2
测试:启动sevice1,service2,zuul
查看注册中心:

通过网关+路由名称访问:
http://localhost:8080/baidu
http://localhost:8080/service1/index
http://localhost:8080/service2/index



项目地址:https://github.com/yangzhenlong/mySpringBootDemo/tree/master/springboot12-zuul
springboot12-zuul的更多相关文章
- Netflix Zuul 了解
Zuul 是提供动态路由,监控,弹性,安全等的边缘服务.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.Zuul 可以适当的对多个 Amazon Auto Scal ...
- netflix zuul 学习
netflix zuul 是netflix开发的一个EDGE SERVICE. 主要是作为一个API Gateway 服务器,可以实现安全,流量控制等功能. 我看的是1.x的版本,Zuul1.x的实现 ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- springcloud(十):服务网关zuul
前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...
- Spring REST 与 Zuul 代理
http://www.baeldung.com/spring-rest-with-zuul-proxy 作者: Eugen Paraschiv 译者: http://oopsguy.com 1.概述 ...
- Zuul(SpringCloud学习笔记一)
路由是微服务架构中必须(integral )的一部分,比如,"/" 可能映射到你的WEB程序上,"/api/users "可能映射到你的用户服务上," ...
- zuul超时的解决方案
参考http://www.coolxuewang.com/view/10 在zuul的配置文件里增加如下配置: ribbon: ConnectTimeout: 6000 ReadTimeo ...
- Spring Cloud Zuul
新建Spring Boot工程,命名为zuul 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?&g ...
- Spring Cloud Zuul 添加 ZuulFilter
紧接着上篇随笔Spring Cloud Zuul写,添加过滤器,进行权限验证 1.添加过滤器 package com.dzpykj.filter; import java.io.IOException ...
- springCloud zuul网关服务
第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...
随机推荐
- layui记录
layui 官网 layui 独立版 layui mobile layui 社区
- 51nod1079 poj2891 中国剩余定理与其扩展
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1079 一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K. ...
- bootstrap 栅栏系统
媒体查询 /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 Bootstrap 中是默认的(还记得 Bootstrap 是移动设备优先的吗?) */ /* 小 ...
- A1098. Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- [luogu1020][导弹拦截]
题目位置 https://www.luogu.org/problemnew/show/P1020 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的 ...
- 在Derby中取得刚刚插入的“递增”类型的字段值
现在才发现采用不同的数据库,对写程序影响很大. 以前常用SQL Server2000或Access,可能是因为都是Microsoft公司的产品,所以在从不同的平台转换的时候问题不是很大. 现在采用De ...
- How MVC pattern Flows
以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)
- java 分隔函数split("",-1)的用途
转: java 分隔函数split("",-1)的用途 2017年12月14日 11:37:58 jaryle 阅读数:8517 1.如果字符串最后一位有值,则没有区别, 2. ...
- Luogu P1552 [APIO2012]派遣 主席树
题目链接 Click Here 这个题好像大多数人用的都是左偏树啊?这里我来贡献一发主席树的解法. 把题目中的问题抽象出来,其实就是询问每一个点的子树中,工资前\(tot_i\)大的点,使它们的和满足 ...
- CodeForces7D 字符串hash + dp
https://cn.vjudge.net/problem/20907/origin 长度是 n 的字符串 s,如果它自身是回文数,且它的长度为 的前缀和后缀是 (k - )-回文数,则它被称作 k- ...