springcloud项目创建笔记4 之《zuul网关》

zuul提供api网关功能,包括统一请求转发、负载均衡、校验过滤等。

1、在主项目建立maven-module,名称mycloud-zuul

2、编辑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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>mycloud-zuul</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mycloud-zuul</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 健康监控配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- zuul -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

引入spring-cloud-starter-netflix-zuul,另外要从eureka拉取服务仍需要spring-cloud-starter-netflix-eureka-client

3、在src/main/java建立包com.example.mycloud.run
添加启动类ZuulApplication.java

package com.example.mycloud.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulApplication {
	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
	}
}

@EnableZuulProxy注解标识开启zuul网关功能
@EnableDiscoveryClient注解标识本服务需要注册到Eureka

4、在src/main/resources下创建application.yml
注:fetchRegistry: true  表示要从eureka拉取服务信息

spring:
  application:
    name: zuul

server:
  port: 8012

eureka:
  server:
    port: 8010
  instance:
    hostname: localhost
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${eureka.server.port}/eureka/
# 网关路由
zuul:
  prefix: /api
  routes:
    sp1: #这个地方的值是可以任意的字符串
      path: /sp1/** #映射路径
      serviceId: service-provider1 #服务的spring.application.name

映射路径说明:
path: /sp1/**表示可以匹配/sp1后的任意路径/sp1/a/b/c/d等

5、运行项目springboot的启动类(ZuulApplication.java的main方法)
浏览器访问
http://localhost:8011/InstanceInfo
http://localhost:8012/api/sp1/InstanceInfo

DESKTOP-PB5DAU8.mshome.net:service-provider1:8011

结果显示相同。

6、zuul负载均衡
zuul默认集成了ribbon并实现了负载均衡,只要启动多个实例即可查看到负载均衡的效果,默认策略是轮询
将mycloud-service-provider1复制一份为mycloud-service-provider2,修改相应参数,并启动

访问:http://localhost:8012/api/sp1/InstanceInfo
依次显示DESKTOP-PB5DAU8:service-provider1:8011和DESKTOP-PB5DAU8:service-provider1:8013

7、自定义负载均衡策略

#为服务Id名称为service-provider1的项目配置负载均衡策略为随机
service-provider1:
  ribbon:
      NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

参考资料:
https://www.jianshu.com/p/6679199e70e8
https://blog.csdn.net/liuchuanhong1/article/details/54693124

注:最新代码上传至https://github.com/csj50/mycloud
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值