新建Spring Boot工程,命名为zuul

1.pom.xml添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dzpykj</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>zuul</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>

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

server:
port: 8767
spring:
application:
name: zuul
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
service1:
path: /service1/**
serviceId: ribbon
service2:
path: /service2/**
serviceId: feign

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

package com.dzpykj;

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; @SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication { public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}

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

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

package com.dzpykj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.dzpykj.hystrixService.HelloService; @RestController
public class HelloController { // @Autowired
// RestTemplate restTemplate; @Autowired
HelloService helloService; // @RequestMapping("/hello/{name}")
// public String hello(@PathVariable String name) {
// return restTemplate.getForObject("http://eurekaclient/hi?name="+name, String.class);
// } @RequestMapping("/hello/{name}")
public String hi(@PathVariable String name) {
return helloService.hello(name);
}
}

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

package com.dzpykj.hystrixService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class HelloService { @Value("${server.port}")
String port; @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloErrorFallBack")
public String hello(String name) {
return restTemplate.getForObject("http://eurekaclient/hi?name="+name+"(ribbon)", String.class);
} public String helloErrorFallBack(String name) {
return "Sorry "+name+",when you are visting ribbon hystrix project,port:"+port+",you meet an error";
}
}

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

package com.dzpykj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.dzpykj.feignInterface.HelloInterface; @RestController
public class HelloController { @Autowired
HelloInterface helloInterface; @RequestMapping("/hello/{name}")
public String hello(@PathVariable String name) {
return helloInterface.hello(name+"(feign)");
}
}

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. mui的上拉加载更多 下拉刷新 自己封装的demo

    ----------------------------------------------- 这是一个非常呆萌的程序妹子,深夜码的丑代码------------------------------- ...

  2. Codeforces 376A. Night at the Museum

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. js判断对象还是数组

    1.对于Javascript 1.8.5(ECMAScript 5),变量名字.isArray( )可以实现这个目的 var a=[]; var b={}; Array.isArray(a);//tr ...

  4. .Net 上传图片之前获取图片的宽高

    Stream st = Request.Files[0].InputStream;                  Byte[] buffer = new Byte[st.Length];      ...

  5. C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework)----(一):整体介绍

    C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework) 在现今软件系统纷纷"云化"的浪潮下,各种支持" ...

  6. 使用java生成mapbox-gl可读的vector tile

    概述 mapbox-gl主要数据源来自mapbox vector tile,本文就是要阐述怎样把postgresql中的地理空间数据转换成vector tile,流程图如下: 配置 该工程采用spri ...

  7. Appscan 配置中登录管理的问题

    一.登录录制时录制为空 这个问题出现在 9.0.3.5 版本上,当时同事一录制为空,我录制却ok,后来发现他录制前将谷歌浏览是打开状态,谷歌浏览关闭掉,再使用外部浏览器Chrome进行会话录制后,问题 ...

  8. seajs笔记

    Amd和Cmd的区别有哪些? 1. 对于依赖的模块,AMD 是提前执行,CMD 是延迟执行.不过 RequireJS 从 2.0 开始,也改成可以延迟执行(根据写法不同,处理方式不同).CMD 推崇 ...

  9. JS--我发现,原来你是这样的JS:面向对象编程OOP[2]--(创建你的那个对象吧)

    一.介绍 我们继续面向对象吧,这次是面向对象编程的第二篇,主要是讲创建对象的模式,希望大家能从博客中学到东西. 时间过得很快,还是不断的学习吧,为了自己的目标. 二.创建对象 1.前面的创建对象方式 ...

  10. ZooKeeper入门

    ZooKeeper简介 ZooKeeper是一个构建在Paxos算法上的高可用的分布式数据管理与系统协调框架,提供了一系列原语集,更上层的应用可以用它来实现同步,配置管理,名称服务,Master选举, ...