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的更多相关文章

  1. Netflix Zuul 了解

    Zuul 是提供动态路由,监控,弹性,安全等的边缘服务.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.Zuul 可以适当的对多个 Amazon Auto Scal ...

  2. netflix zuul 学习

    netflix zuul 是netflix开发的一个EDGE SERVICE. 主要是作为一个API Gateway 服务器,可以实现安全,流量控制等功能. 我看的是1.x的版本,Zuul1.x的实现 ...

  3. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  4. springcloud(十):服务网关zuul

    前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...

  5. Spring REST 与 Zuul 代理

    http://www.baeldung.com/spring-rest-with-zuul-proxy 作者: Eugen Paraschiv 译者: http://oopsguy.com 1.概述 ...

  6. Zuul(SpringCloud学习笔记一)

    路由是微服务架构中必须(integral )的一部分,比如,"/" 可能映射到你的WEB程序上,"/api/users "可能映射到你的用户服务上," ...

  7. zuul超时的解决方案

    参考http://www.coolxuewang.com/view/10 在zuul的配置文件里增加如下配置: ribbon:    ConnectTimeout: 6000    ReadTimeo ...

  8. Spring Cloud Zuul

    新建Spring Boot工程,命名为zuul 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?&g ...

  9. Spring Cloud Zuul 添加 ZuulFilter

    紧接着上篇随笔Spring Cloud Zuul写,添加过滤器,进行权限验证 1.添加过滤器 package com.dzpykj.filter; import java.io.IOException ...

  10. springCloud zuul网关服务

    第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...

随机推荐

  1. [NOI2015]寿司晚宴(状压dp)

    为了庆祝NOI的成功开幕,主办方为大家准备了一场寿司晚宴.小G和小W作为参加NOI的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了n−1种不同的寿司,编号1,2,3,⋯,n-1,其中第种 ...

  2. Linux下启动,停止,重启Nginx、Mysql、PHP

    LINUX启动Nginx的命令: 一.查询是否启动 [root@jiang php-fpm.d]# ps -ef | grep nginx root 25225 1 0 19:26 ? 00:00:0 ...

  3. nuxt.js实战之用vue-i18n实现多语言

    一.实现步骤 1.安装vue-i18n并且创建store.js(vuex状态管理)文件 2.创建一个(middleware)中间件,用来管理不同的语言 3.创建不同语言的json文件作为语言包(例如: ...

  4. Typescript学习笔记(二)枚举

    跟随handbook的脚步,详细介绍一下枚举. enum Direction { Up = 1, Down, Left, Right } 一个枚举类型可以包含零个或多个枚举成员,每个枚举成员可以是一个 ...

  5. maven将项目及第三方jar打成一个jar包

    pom.xml中添加如下配置 把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持) <build> <plugins> <plugin> ...

  6. A1098. Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. 【P2303】Longge的问题

    题目大意:求\[\sum\limits_{i=1}^ngcd(n,i)\] 题解:发现 gcd 中有很多是重复的,因此考虑枚举 gcd. \[\sum\limits_{i=1}^ngcd(n,i)=\ ...

  8. django 通过ajax完成登录

    一. 在users/views.py中加代码: from django.http import HttpResponse class LoginView(View): ""&quo ...

  9. 第一篇:安装Android Studio问题及其解决方案

    ubuntu18.04配置android studio3.2.1环境 1.JDK安装与配置:https://www.cnblogs.com/yuanbo123/p/5819564.html(按照文档操 ...

  10. Python--多线程、多进程常用概念

    一.常用概念 进程 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 线程 线程的出现是为了降低上下文切换的消耗,提高系统的并发性,并突破一个进程只能干 ...