文章目录
1.概述
2.创建一个业务服务:demo-spring-cloud-zuul-bussiness
2.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">
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaoxinfu.demo.spring.cloud</groupId>
<artifactId>demo-spring-cloud-zuul-bussiness</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo-spring-cloud-zuul-bussiness</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.首先,我们的项目是springboot项目,所以我们必须引入spring-boot-starter-parent
2.另外,我们的项目是web项目,因此引入spring-boot-starter-web
2.2.创建服务类
package com.gaoxinfu.demo.spring.cloud.zuul;
import org.springframework.web.bind.annotation.*;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-23 16:16
*/
@RestController
public class HelloController {
@RequestMapping(value = "/say/{message}",method = RequestMethod.GET)
@ResponseBody
public String say(@PathVariable String message){
return "HelloController.say = "+message;
}
}
2.3.配置文件:application.yml
server:
port: 8080
spring:
application:
name: zuul-bussiness
1.服务的端口是8080,项目名称zuul-bussiness
2.4.创建启动类
package com.gaoxinfu.demo.spring.cloud.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-23 16:13
*/
@SpringBootApplication
public class ZuulBussinessApp {
public static void main(String[] args) {
SpringApplication.run(ZuulBussinessApp.class,args);
}
}
2.6.启动验证
http://127.0.0.1:8080/say/hello
3.创建一个zuul网关:demo-spring-cloud-zuul-gateway
3.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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaoxinfu.demo.spring.cloud</groupId>
<artifactId>demo-spring-cloud-zuul-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo-spring-cloud-zuul-gateway</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.由于我们这里需要使用zuul进行网关的代理路由配置,所以我们引入了spring-cloud-starter-netflix-zuul
3.2.配置文件:application.yml
server:
port: 8079
spring:
application:
name: zuul-gateway
#zuul related configuration
zuul:
routes:
zuul-bussiness:
url: http://localhost:8080
ribbon:
eureka:
enabled=false:
1.zuul网关项目的端口8079
3.3.创建Filter过滤器:SimpleFilter
package com.gaoxinfu.demo.spring.cloud.zuul;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-23 15:56
*/
public class SimpleFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
return null;
}
}
3.3.1.filterType
filterType | 描述 |
---|---|
pre | filters run before the request is routed. |
route | filters can handle the actual routing of the request. |
post | filters run after the request has been routed. |
error | filters run if an error occurs in the course of handling the request. |
3.4.创建启动类:NacosZuulGatewayApp
package com.gaoxinfu.demo.spring.cloud.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-23 15:41
*/
@SpringBootApplication
@EnableZuulProxy
public class NacosZuulGatewayApp {
public static void main(String[] args) {
SpringApplication.run(NacosZuulGatewayApp.class,args);
}
/**
* 应用启动的时候,我们将SimpleFilter注册到Spring容器之中
* @return
*/
@Bean
public SimpleFilter simpleFilter(){
return new SimpleFilter();
}
}
3.5.验证
http://127.0.0.1:8079/zuul-bussiness/say/hello11