SpringCloud Zuul构建网关

1、创建 eureka server 项目

使用intellij idea创建一个spring boot项目,创建服务中心。设置端口为5000。
修改配置文件,设置端口

server.port=5000

添加eureka-server依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

Application.java添加注解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

2、创建Eureka Client项目

分别新建两个 Spring Boot rest api接口服务 Customer、Product,分别设置端口号为9000和9001。
添加eureka-client依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

修改Customer配置文件,设置端口号、应用名称及eureka服务的地址,如下:

server.port=9000
spring.application.name=CustomerService
eureka.client.service-url.defaultZone=http://localhost:5000/eureka

修改Product配置文件,设置端口号、应用名称及eureka服务的地址,如下:

server.port=9001
spring.application.name=ProductService
eureka.client.service-url.defaultZone=http://localhost:5000/eureka

分别对两个项目的Application.java添加注解@EnableDiscoveryClient

3、创建api网关

使用intellij idea创建一个spring boot项目,创建Api网关服务。设置端口为5555。
添加eureka-client和zuul依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

修改配置文件,设置端口号、eureka服务的地址、应用名称及通过zuul.routes.名称.path和zuul.routes.名称.serviceId指定访问api服务对应的url路径

server.port=5555
eureka.client.service-url.defaultZone=http://localhost:5000/eureka

spring.application.name=ZuulGateway

zuul.routes.customers.path=/customers/**
zuul.routes.customers.service-id=CustomerService

zuul.routes.products.path=/products/**
zuul.routes.products.service-id=ProductService

注:service-id值必须与client项目配置文件中的spring.application.name值一致
Application.java添加@EnableDiscoveryClient和@EnableZuulProxy注解

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayApplication {

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

4、分别启动这4个应用,打开eureka服务:http://localhost:5000/

如下图所示全部注册成功:

eureka.png

打开http://localhost:5555/customers,返回结果如下:
image.png

打开http://localhost:5555/customers/10,返回结果如下:
image.png

打开http://localhost:5555/products,返回结果如下:
image.png

通过上面的例子说明Api网关服务已经生效。结合具体的业务场景,我们的生产环境只对外暴露5555端口,客户端访问Api网关,由Api网关去路由到各个服务节点,这样所有的客户端调用都统一了入口。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (git上的源码:https://gitee.com/rain7564/spring_microservices_...
    sprainkle阅读 17,086评论 3 32
  • 考虑一个问题,外部的应用如何来访问内部各种各样的微服务呢?在微服务架构中,后端服务往往不直接开放给调用端,而是通过...
    weisen阅读 6,296评论 1 9
  • Spring Cloud Netflix Dalston.RELEASE 该项目通过自动配置为Spring Boo...
    WorseRole阅读 3,561评论 0 0
  • 2019年-复盘 做复盘,我需要找一个安静的环境,没有老公和孩...
    AilwaDong老姐姐阅读 3,649评论 0 0
  • 我把影子,灵魂,肉体重叠 用卑微的重量 凹陷雪地 压出的轮廓 沾沾自喜的舒展 又在雪崩中淹没 有意识的太阳 如无影...
    罗子阅读 4,788评论 114 141