`

spring cloud zuul服务网关

阅读更多
spring cloud zuul服务网关

(1)pom依赖jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

(2)加上@EnableZuulProxy,开启zuul功能
@EnableZuulProxy
@EnableEurekaClient
@EnableFeignClients
@RibbonClient(name = "SPRING-CLOUD-CLIENT5",configuration = MyRibbonConfig.class)
@SpringBootApplication
public class App {
@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }
}

(3)application.properties
zuul.routes.hiapi.path=/api/**
zuul.routes.hiapi.url=http://localhost:8084/spring-cloud-client5
#这里的配置表示,访问/api/** 直接重定向到http://localhost:8084/spring-cloud-client5

(4)http://localhost:8084/spring-cloud-client5/user/
   可以这样访问:http://localhost:8084/spring-cloud-client5/api/user/
其实可以这样访问:http://localhost:8084/api/user/,我配置了context-path。

#反响代理配置
#这里的配置类似nginx的反响代理
#当请求/api/**会直接交给listOfServers配置的服务器处理
#当stripPrefix=true的时候 (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list)
#当stripPrefix=false的时候(http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list)
zuul.routes.api.path=/api/**
zuul.routes.api.stripPrefix=false
api.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080

路由是微服务架构中必须(integral )的一部分,比如,“/” 可能映射到你的WEB程序上,
”/api/users “可能映射到你的用户服务上,“/api/shop”可能映射到你的商品服务商。
(注解:我理解这里的这几个映射就是说通过Zuul这个网关把服务映射到不同的服务商去处理,从而变成了微服务!)
Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器.
Zuul功能:
认证
压力测试
金丝雀测试
动态路由
负载削减
安全
静态响应处理
主动/主动交换管理
Zuul的规则引擎允许通过任何JVM语言来编写规则和过滤器, 支持基于Java和Groovy的构建。

配置属性 zuul.max.host.connections 已经被两个新的配置属性替代,
zuul.host.maxTotalConnections (总连接数)和
zuul.host.maxPerRouteConnections,(每个路由连接数) 默认值分别是200和20.

一 . Zuul做反向代理
  当一个UI应用调用一个或更多的后端服务的时候,我们可以用Spring Cloud创建一个Zuul代理
减少开发是非常常见的例子。使用代理服务来避免必须的跨域资源共享(Cross-Origin Resource
Sharing)和所有的后端需要分别认证的问题。
  在Spring Boot主函数上通过注解 @EnableZuulProxy 来开启, 这样可以让本地的请求转发
到适当的服务. 按照约定, 一个ID为"users"的服务会收到 /users 请求路径的代理请求(前缀会被剥离).
Zuul使用Ribbon定位服务注册中的实例, 并且所有的请求都在hystrix的command中执行,
  所以失败信息将会展现在Hystrix metrics中, 并且一旦断路器打开, 代理请求将不会尝试去链接服务.

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics