一、简介
zuul路由网关,是指用户访问服务时,先访问网关,由网关转发给消费者,这样的一个过程。在本文中,网关将会转发用户的请求到消费者。
二、pom文件
<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.sustly</groupId>
<artifactId>blog_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>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
三、主配置类
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@EnableEurekaClient
public class BlogZuul {
public static void main(String[] args) {
SpringApplication.run(BlogZuul.class, args);
}
}
注意:@EnableDiscoveryClient 服务发现注解,一定要加
四、application.properties
server.port=9527
spring.application.name=blog_zuul
eureka.client.service-url.defaultZone=http://localhost:7001/eureka
eureka.instance.instance-id=blog_zuul
eureka.instance.prefer-ip-address=true
zuul.ignored-services=blog-consumer-feign
zuul.routes.consumer.service-id=blog-consumer-feign
zuul.routes.consumer.path=/blog/**
blog-consumer-feign.ribbon.ConnectTimeout=5000
blog-consumer-feign.ribbon.ReadTimeout=5000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
info.app.name=blog_zuul
info.company.name=www.sustly.xyz
info.build.artifactId=${project.artifactId}
info.build.version=${project.version}
五、博客系统GitHub地址
https://github.com/sustly/blog_vue_server
注意:master分支才是springcloud版本