新建Spring Boot工程,命名为zuul

1.pom.xml添加依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.dzpykj</groupId>
  7. <artifactId>zuul</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>zuul</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.5.9.RELEASE</version>
  18. <relativePath /> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-dependencies</artifactId>
  32. <version>Edgware.RELEASE</version>
  33. <type>pom</type>
  34. <scope>import</scope>
  35. </dependency>
  36. </dependencies>
  37. </dependencyManagement>
  38.  
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-web</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.cloud</groupId>
  51. <artifactId>spring-cloud-starter-eureka</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.cloud</groupId>
  55. <artifactId>spring-cloud-starter-zuul</artifactId>
  56. </dependency>
  57. </dependencies>
  58.  
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-maven-plugin</artifactId>
  64. </plugin>
  65. </plugins>
  66. </build>
  67.  
  68. <repositories>
  69. <repository>
  70. <id>spring-milestones</id>
  71. <name>Spring Milestones</name>
  72. <url>https://repo.spring.io/milestone</url>
  73. <snapshots>
  74. <enabled>false</enabled>
  75. </snapshots>
  76. </repository>
  77. </repositories>
  78. </project>

2.将application.properties重命名为application.yml,并且添加配置

  1. server:
  2. port: 8767
  3. spring:
  4. application:
  5. name: zuul
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:8761/eureka/
  10. zuul:
  11. routes:
  12. service1:
  13. path: /service1/**
  14. serviceId: ribbon
  15. service2:
  16. path: /service2/**
  17. serviceId: feign

3.启动类添加注解@EnableZuulProxy开启Zuul

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

4.改造前几篇随笔中的代码

4.1 将ribbon工程com.dzpykj.controller.HelloController的代码改造

  1. package com.dzpykj.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import com.dzpykj.hystrixService.HelloService;
  9.  
  10. @RestController
  11. public class HelloController {
  12.  
  13. // @Autowired
  14. // RestTemplate restTemplate;
  15.  
  16. @Autowired
  17. HelloService helloService;
  18.  
  19. // @RequestMapping("/hello/{name}")
  20. // public String hello(@PathVariable String name) {
  21. // return restTemplate.getForObject("http://eurekaclient/hi?name="+name, String.class);
  22. // }
  23.  
  24. @RequestMapping("/hello/{name}")
  25. public String hi(@PathVariable String name) {
  26. return helloService.hello(name);
  27. }
  28. }

4.2 将ribbon工程com.dzpykj.hystrixService.HelloService的代码改造

  1. package com.dzpykj.hystrixService;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.client.RestTemplate;
  7.  
  8. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  9.  
  10. @Service
  11. public class HelloService {
  12.  
  13. @Value("${server.port}")
  14. String port;
  15.  
  16. @Autowired
  17. RestTemplate restTemplate;
  18.  
  19. @HystrixCommand(fallbackMethod = "helloErrorFallBack")
  20. public String hello(String name) {
  21. return restTemplate.getForObject("http://eurekaclient/hi?name="+name+"(ribbon)", String.class);
  22. }
  23.  
  24. public String helloErrorFallBack(String name) {
  25. return "Sorry "+name+",when you are visting ribbon hystrix project,port:"+port+",you meet an error";
  26. }
  27. }

4.3 将feign工程com.dzpykj.controller.HelloController的代码改造

  1. package com.dzpykj.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import com.dzpykj.feignInterface.HelloInterface;
  9.  
  10. @RestController
  11. public class HelloController {
  12.  
  13. @Autowired
  14. HelloInterface helloInterface;
  15.  
  16. @RequestMapping("/hello/{name}")
  17. public String hello(@PathVariable String name) {
  18. return helloInterface.hello(name+"(feign)");
  19. }
  20. }

5.依次启动Eureka服务集群、Eureka客户端集群、Ribbon工程

5.1 按照Spring Cloud Eureka Server集群Demo级搭建的步骤启动Eureka服务peer1,peer2集群

5.2 按照Spring Cloud Eureka服务Demo级搭建最后面的步骤,分别启动8763,8764两个Eureka客户端形成集群

5.3 启动Ribbon工程

5.4 启动Feign工程

5.5 启动Zuul工程

6.访问http://127.0.0.1:8767/service1/hello/chaixy;http://127.0.0.1:8767/service2/hello/chaixy

7.这篇随笔代码较琐碎,GitHub源码在此处 https://github.com/PinBo1991/springcloud

Spring Cloud Zuul的更多相关文章

  1. Spring Cloud Zuul 添加 ZuulFilter

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

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

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

  3. 笔记:Spring Cloud Zuul 快速入门

    Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...

  4. Spring Cloud Zuul 限流详解(附源码)(转)

    在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...

  5. Spring Cloud Zuul 中文文件上传乱码

    原文地址:https://segmentfault.com/a/1190000011650034 1 描述 使用Spring Cloud Zuul进行路由转发时候吗,文件上传会造成中文乱码“?”.1. ...

  6. spring cloud zuul参数调优

    zuul 内置参数 zuul.host.maxTotalConnections 适用于ApacheHttpClient,如果是okhttp无效.每个服务的http客户端连接池最大连接,默认是200. ...

  7. Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务

    API 网关的出现的原因是微服务架构的出现,不同的微服务一般会有不同的服务地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会 ...

  8. spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号)

    spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号) 比如我原来有,spring-boot-user微服务,后台进行迭代更新,另外其了一个微服务: sprin ...

  9. spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关)

    spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关) zuul: routes: #路由配置表示 myroute1: #路由名一 path: ...

  10. spring cloud: zuul: 微网关-简单使用与路由配置

    spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...

随机推荐

  1. 【开源】【前后端分离】【优雅编码】分享我工作中的一款MVC+EF+IoC+Layui前后端分离的框架——【NO.1】框架概述

    写博客之前总想说点什么,但写的时候又忘了想说点什么,算了,不说了,还是来送福利吧. 今天是来分享我在平时工作中搭建的一套前后端分离的框架. 平时工作大多时候都是在做管理类型的软件开发,无非就是增.删. ...

  2. c#把汉字转化成全拼音函数(全拼)

    /// <summary>        /// 把汉字转换成拼音(全拼)        /// </summary>        /// <param name=&q ...

  3. HTTP-FLV直播初探

    目前几种视频流的简单对比: 协议 httpflv rtmp hls dash 传输方式 http流 tcp流 http http 视频封装格式 flv flv tag Ts文件 Mp4 3gp web ...

  4. 前端面试题系列(1):doctype作用 标准模式与兼容模式

    1.doctype作用 <!DOCTYPE>声明位于HTML文档的第一行.处于<HTML>标签之前.告知浏览器的解析器用什么文档标准解析这个文档.DOCYTYPE不存在或格式不 ...

  5. python --- 协程编程(第三方库gevent的使用)

    1. 什么是协程? 协程(coroutine),又称微线程.协程不是线程也不是进程,它的上下文关系切换不是由CPU控制,一个协程由当前任务切换到其他任务由当前任务来控制.一个线程可以包含多个协程,对于 ...

  6. 转: .Net 4.0 ExpandoObject 使用

    本篇文章中就ExpandoObject的基本使用进行一些demo.我们几乎都知道dynamic特性是.net 4.0中一个主要的新特性,而ExpandoObject正是这样的一个动态的类型.Expan ...

  7. Linux 性能搜集【linux_reports-cpu/memory/disks/network】

    为方便问题发生后,问题原因的分析排查,我们可以在服务器中事先部署如下脚本,方便故障发生后,问题原因的分析排查 脚本部署方法: 1.将脚本[linux_reports.sh]上传到服务器 2.登陆虚拟机 ...

  8. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...

  9. MySQL执行一个查询的过程

    总体流程 客户端发送一条查询给服务器: 服务器先会检查查询缓存,如果命中了缓存,则立即返回存储在缓存中的结果.否则进入下一阶段: 服务器端进行SQL解析.预处理,再由优化器生成对应的执行计划: MyS ...

  10. ZZCMS8.1|代码审计

    这周的审计任务 ZZCMS8.1是站长招商网内容管理系统.审计这个CMS的原因很多,这里就不详说了(其实是漏洞类型多点)                                         ...