搭建springCloud网关zuul

时间:2023-03-09 05:25:35
搭建springCloud网关zuul

一.pom.xml

<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.ams.gateway</groupId>
<artifactId>ams-svc-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</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-starter-parent</artifactId>
<!--version>Dalston.SR1</version-->
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

二.yml配置

spring:
application:
name: gateway
tomcat:
uri-encoding: UTF-8
http:
encoding:
charset: UTF-8
enabled: true
force: true
devtools:
restart:
enabled: true
server:
port: 8443
ssl:
key-store: classpath:keystore.p12
key-store-password: ams2018
keyStoreType: PKCS12
keyAlias: tomcat
http:
port: 7000
eureka:
instance:
perferIpAddress: true
instanceId: ${spring.cloud.client.ipAddress}:${server.port}
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://admin:ams2018@127.0.0.1:7001/eureka/
#eureka.instance.preferIpAddress=true
#eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} #hystrix调用方法的超时时间,默认是1000毫秒,如果不调第一次启动会访问会报错,第二中方案是设置不超时hystrix.command.default.execution.timeout.enabled: false
#熔断超时
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000
#网关配置
zuul:
servlet-path: / #解决通过网关上传文件文件名乱码问题
host:
connect-timeout-millis: 60000
socket-timeout-millis: 60000
ribbon:
eager-load:
enabled: true #启动饥饿加载
routes:
api-service1:
path: /service1/**
serviceId: service1
stripPrefix: false
api-service2:
path: /**
serviceId:service2
stripPrefix: false #转发时不忽略前缀

三、启动类

package com;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean; @EnableZuulProxy
@SpringCloudApplication
public class Application
{
public static void main(String[] args)
{
new SpringApplicationBuilder(Application.class).web(true).run(args);
} }