Spring Cloud 配置Zuul實現路由轉發

當前使用框架:SSM+Eureka+OpenFeign

1、pom.xml導入Zuul依賴

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

2、Application.class配置

package com;

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

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulServiceApplication {
	public static void main(String[] args) {
		SpringApplication.run(ZuulServiceApplication.class, args);
	}

}

3、application.properties配置

server.port=8773
eureka.client.serviceUrl.defaultZone=http://localhost:8771/eureka/
spring.application.name=zuulservice

#設置路由 common可以任意命名
zuul.routes.common.path=/common/**
zuul.routes.common.serviceId=commonservice

4、在CommonService寫入測試代碼

package com.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping("/testZuul")
    public String testZuul(){
        return "這裏是Zuul!!!";
    }
}

5、在地址欄進行路由轉發測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章