1、创建一个zuul-proxy模块
引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
配置启动类:
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulProxyAppRun {
public static void main(String[] args) {
SpringApplication.run(ZuulProxyAppRun.class, args);
}
}
配置文件:
server:
port: 7000
spring:
application:
name: zuul-proxy
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
启动服务:
Eureka-Server
Order-Service(两个)
Feign-Service
Zuul-Porxy
接口调用:多次调用,采用轮询的方式进行负载均衡
[http://localhost:7000/order-service/order/1](http://localhost:7000/order-service/order/1)
默认路由规则:虽然没有在配置文件配置服务的请求信息,但是默认会从Eureka中获取,所以可以直接访问