搭建SpringCloud Gateway
創建microservicecloud-springcloud-gateway-9528工程
pom文件 依賴:
<dependencies>
<!-- 將微服務provider側注冊進eureka -->
<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-gateway</artifactId>
</dependency>
<!-- swagger-spring-boot -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
<!--api接口-->
<dependency>
<groupId>com.yehui</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
yml文件
server: port: 9528 spring: application: name: springcloudgetway cloud: gateway: discovery: locator: enabled: true lowerCaseServiceId: true routes: #id標簽配置的是router的id,每個router都需要一個唯一的id - id: dept_client #uri配置的是將請求路由到哪里, uri: lb://microservicecloud-dept
predicates: - Path=/dept_client/client/** #如果請求地址滿足/microservicecloud-dept/**,則轉發到microservicecloud-dept服務 filters: - StripPrefix=2 eureka: client: #客戶端注冊進eureka服務列表內 service-url: defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/,http://localhost:7001/eureka/ instance: prefer-ip-address: true
啟動類
@SpringBootApplication public class SpringCloudGateWayApp { public static void main(String[] args) { SpringApplication.run(SpringCloudGateWayApp.class); } }
啟動服務測試;
訪問:http://localhost:9528/dept_client/client/dept/findAll
