idea创建springcloud项目图文教程(zuul实现api网关服务)(十)

1,新建一个SpringBoot项目,这里命名gateway,然后导入相关依赖:

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

 2,新建GatewayApplication类;加上两个注解     @EnableZuulProxy是开启网关功能

package com.hcmony;

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


@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(GatewayApplication.class, args);
	}
}

3,配置文件

    path:    我们要转发路径规则

    serviceId:是我们要转发路由的服务

zuul.routes.customer.path=/**
zuul.routes.customer.serviceId=springcloud-customer

eureka.client.service-url.defaultZone: http://localhost:8888/eureka/
server.port=8101
spring.application.name=springcloud-gateway

4,查看eureka中心,查看服务注册情况

5,请求http://localhost:8101/test (这个服务其实是customer的,它的端口号是8001)

     当我们请求时,他会转发到8001端口的服务上。

    这样就完成 了一个api网关服务。

idea创建maven项目,本教程适合各类小白(一)

idea创建maven,spring,springmvc,mybatis,项目(二)

idea创建maven,spring,springmvc,mybatis,项目(三)
idea创建springboot项目图文教程(四)
idea创建springboot项目图文教程(配置文件)(五)
idea创建springcloud项目图文教程(EurekaServer注册中心)(六)

idea创建springcloud项目图文教程(创建服务提供者)(七)

idea创建springcloud项目图文教程(创建消费者)(八)

idea创建springcloud项目图文教程(Feign实现负载均衡)(九)

猜你喜欢

转载自blog.csdn.net/hcmony/article/details/81636604