spring cloud-构建微服务架构的网关(API GateWay)
2017-09-18 10:24
811 查看
在我们前面的博客中讲到,当服务A需要调用服务B的时候,只需要从Eureka中获取B服务的注册实例,然后使用Feign来调用B的服务,使用Ribbon来实现负载均衡,但是,当我们同时向客户端暴漏多个服务的时候,客户端怎么调用我们暴漏的服务了,如果我们还想加入安全认证,权限控制,过滤器以及动态路由等特性了,那么就需要使用Zuul来实现API GateWay了,下面,我们先来看下Zuul怎么使用。
一、加入Zuul的依赖
[html] view
plain copy
<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>
由于,我们需要将Zuul服务注册到Eureka Server上,同时从Eureka Server上发现注册的服务,所以这里我们加上了Eureka的依赖。
二、在应用Application主类上开启Zuul支持
[java] view
plain copy
@SpringBootApplication
@EnableZuulProxy // 使用@EnableZuulProxy来开启Zuul的支持,如果你不想使用Zuul提供的Filter和反向代理的功能的话,此处可以使用@EnableZuulServer注解
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
三、在application.yml中增加Zuul的基础配置信息
[java] view
plain copy
spring:
application:
name: gateway-zuul # 应用名
server:
port: 8768 #Zuul Server的端口号
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
四、在application.yml中增加服务路由配置
前提:在Eureka Server已经注册了2个服务,分别是:springboot-h2-service和springboot-rest-template-feign,其中springboot-rest-template-feign服务会调用springboot-h2-service服务,springboot-rest-template-feign服务是我们对外提供的服务,也就是说,springboot-rest-template-feign服务是我们暴漏给客户端调用的。
[java] view
plain copy
# 路由配置方式一
#zuul:
# routes:
# springboot-rest-template-feign: /templateservice/** #所有请求springboot-rest-template-feign的请求,都会被拦截,并且转发到templateservice上
# 路由配置方式二
zuul:
routes:
api-contract: # 其中api-contract是路由名称,可以随便定义,但是path和service-id需要一一对应
path: /templateservice/**
service-id: springboot-rest-template-feign # springboot-rest-template-feign为注册到Eureka上的服务名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule # 配置服务端负载均衡策略
五、验证
下面我们就可以来进行验证了,在浏览器中输入:http://localhost:8768/templateservice/template/1就可以看到测试结果了。Zuul的基本使用就这些,关于Filter以及高可用,请见在后面的博客中。
一、加入Zuul的依赖
[html] view
plain copy
<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>
由于,我们需要将Zuul服务注册到Eureka Server上,同时从Eureka Server上发现注册的服务,所以这里我们加上了Eureka的依赖。
二、在应用Application主类上开启Zuul支持
[java] view
plain copy
@SpringBootApplication
@EnableZuulProxy // 使用@EnableZuulProxy来开启Zuul的支持,如果你不想使用Zuul提供的Filter和反向代理的功能的话,此处可以使用@EnableZuulServer注解
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
三、在application.yml中增加Zuul的基础配置信息
[java] view
plain copy
spring:
application:
name: gateway-zuul # 应用名
server:
port: 8768 #Zuul Server的端口号
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
四、在application.yml中增加服务路由配置
前提:在Eureka Server已经注册了2个服务,分别是:springboot-h2-service和springboot-rest-template-feign,其中springboot-rest-template-feign服务会调用springboot-h2-service服务,springboot-rest-template-feign服务是我们对外提供的服务,也就是说,springboot-rest-template-feign服务是我们暴漏给客户端调用的。
[java] view
plain copy
# 路由配置方式一
#zuul:
# routes:
# springboot-rest-template-feign: /templateservice/** #所有请求springboot-rest-template-feign的请求,都会被拦截,并且转发到templateservice上
# 路由配置方式二
zuul:
routes:
api-contract: # 其中api-contract是路由名称,可以随便定义,但是path和service-id需要一一对应
path: /templateservice/**
service-id: springboot-rest-template-feign # springboot-rest-template-feign为注册到Eureka上的服务名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule # 配置服务端负载均衡策略
五、验证
下面我们就可以来进行验证了,在浏览器中输入:http://localhost:8768/templateservice/template/1就可以看到测试结果了。Zuul的基本使用就这些,关于Filter以及高可用,请见在后面的博客中。
相关文章推荐
- Spring Cloud构建微服务架构(五)服务网关
- Spring Cloud构建微服务架构(五)服务网关
- Spring Cloud构建微服务架构—服务网关过滤器
- Spring Cloud构建微服务架构(五)服务网关
- Spring Cloud构建微服务架构(五)服务网关
- Spring Cloud构建微服务架构:服务网关(过滤器)【Dalston版】
- Spring Cloud构建微服务架构:服务网关(基础)【Dalston版】
- Spring Cloud构建微服务架构(五)服务网关
- Spring Cloud构建微服务架构(五)服务网关 原创 2016-07-12 翟永超 Spring Cloud 被围观 53984 次 通过之前几篇Spring Cloud中几个核心组件的介
- Spring Cloud微服务架构的构建:分布式配置中心(加密解密功能)
- Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
- Spring Cloud构建微服务架构:分布式配置中心
- Spring Cloud构建微服务架构(四)分布式配置中心(续)
- (七)整合spring cloud云服务架构 - HongHu common-service 项目构建过程
- Spring Cloud架构教程 (五)服务网关(过滤器)
- Spring Cloud构建微服务架构(四)分布式配置中心(续)
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构(七)消息总线
- (九)整合spring cloud云服务架构 - HongHu commonservice-eureka 项目构建过程
- 整合spring cloud云服务架构 - 企业分布式微服务云架构构建