SpringCloud XX, what Zuul Yes. Zuul basic routing configuration. Zuul routing access mapping rules.

What ①Zuul Yes.

zuul routing gateways.

 


Zuul request contains routing and filtering two main features:
wherein the routing function is responsible for forwarding the request to the external micro-specific service instance, is the basis of uniform external access inlet and the filter function is responsible for handling the request intervene in the process, is the basis to achieve .Zuul verification request, the service aggregator functions and integration Eureka, Zuul will register itself as a service management applications in Eureka, while obtaining information from the other widgets and services in Eureka, i.e. after access services are obtained by micro Zuul after the jump.

Note: Zuul service will eventually enrolled at Eureka
 
provides routing + + = proxy filter three functions.

Zuul can you do? Routing and filtering.

Information on the official website: https://github.com/Netflix/zuul/wiki/Getting-Started

②Zuul basic routing configuration.
 

The first step: New Module Modules microservicecloud-zuul-gateway-9527

 

 

 

 

 

 

 

 

 

Step Two: Module module microservicecloud-zuul-gateway-9527 pom file changes.

microservicecloud-zuul-gateway-9527 pom file modification of the contents of Module Module are:


     <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>

 

Complete Module Module microservicecloud-zuul-gateway-9527's pom files are:

<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>
	<parent>
		<groupId>com.lss.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-zuul-gateway-9527</artifactId>
	<dependencies>
		<!-- zuul路由网关 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<!-- actuator监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- hystrix容错 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- 日常标配 -->
		<dependency>
			<groupId>com.atguigu.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</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-test</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

</project>

 

 

The third step: Module module microservicecloud-zuul-gateway-9527 yml ​​file modifications.

Module Module complete content microservicecloud-zuul-gateway-9527's yml files are:

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 


info:
  app.name: lss-microcloud
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

 

Step four: hosts changes.

 

 

 

 

Figure fancy, leaving only the middle of a space. Two or more spaces will be wrong.

 

 

Step 5: Create the master boot class Zuul_9527_StartSpringCloudApp.

 

 

 

 

 

 

主启动类Zuul_9527_StartSpringCloudApp.java的完整内容是:

package com.lss.springcloud;

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

@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {

	public static void main(String[] args) {
		 SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}

}

 

第六步:启动。

三个eureka,一个服务提供类microservicecloud-provider-dept-8001,一个路由。

 

第七步:测试

不用路由:http://localhost:8001/dept/get/2

用路由:http://myzuul.com:9527/microservicecloud-dept/dept/get/2

microservicecloud-dept是eureka注册中心里面的一个微服务。

 

 

 

③Zuul路由访问映射规则。不暴露真正的注册进eureka的微服务名称。

在microservicecloud-zuul-gateway-9527子工程上做出改变。

第一步:对microservicecloud-zuul-gateway-9527子工程上的yml文件进行修改。

zuul: 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

 

before
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
 

after
http://myzuul.com:9527/mydept/dept/get/1
 

 

microservicecloud-zuul-gateway-9527子工程上的yml文件完整内容是:

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 


info:
  app.name: lss-microcloud
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

zuul:  
  routes:  
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

 

第二步:但是目前有一个问题:新路径访问ok,原路径访问也ok

http://myzuul.com:9527/microservicecloud-dept/dept/get/2

http://myzuul.com:9527/mydept/dept/get/1

两条路径都OK则不安全。需要将原路径访问拒绝。

原真实服务名忽略

对microservicecloud-zuul-gateway-9527子工程上的yml文件进行修改。

zuul: 
  ignored-services: microservicecloud-dept 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

 

 

microservicecloud-zuul-gateway-9527子工程上的yml文件完整内容是:

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 


info:
  app.name: lss-microcloud
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

zuul:
  ignored-services: microservicecloud-dept   
  routes:  
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

单个的微服务名称具体写是哪一个,多个的微服务名称需要忽略可以用"*"星号,如下所示:

zuul: 
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

 

第三步:设置统一公共前缀。

对microservicecloud-zuul-gateway-9527子工程上的yml文件进行修改。


zuul: 
  prefix: /lss
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

microservicecloud-zuul-gateway-9527子工程上的yml文件完整内容是:

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 


info:
  app.name: lss-microcloud
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

zuul:
  prefix: /lss
  ignored-services: "*"
  routes:  
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

 

测试:启动eureka,启动provider,启动zuul

http://myzuul.com:9527/lss/mydept/dept/get/1

且原始路径也不能访问了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了155 篇原创文章 · 获赞 1 · 访问量 1万+

Guess you like

Origin blog.csdn.net/lbh19630726/article/details/104215543