您的位置:首页 > 编程语言 > Java开发

springcloud学习笔记(八)SpringCloud集成zuul路由(一)

2018-02-24 10:44 806 查看
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简答的微服务系统如下图:

 
注意:A服务和B服务是可以相互调用的,作图的时候忘记了。并且配置服务也是注册到服务注册中心的。在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。

一、Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。zuul有以下功能:Authentication
Insights
Stress Testing
Canary Testing
Dynamic Routing
Service Migration
Load Shedding
Security
Static Response handling
Active/Active traffic management

不多说,上面是复制的,下面开始代码测试:
项目结构如下图



首先,启动类加入注解@EnablezuulProxy:package com.xc;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class App_zuul {
public static void main(String[] args){
SpringApplication.run(App_zuul.class,args);
}
}资源文件application.yml:eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://xc:123@localhost:8761/eureka server:
port: 6543
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: cloud-test
api-b:
path: /api-b/**
serviceId: cloud-test2其中serviceId:cloud-test为微服务,并且有个服务为hello:



其中serviceId:cloud-test2为微服务,并且也有个服务为hello:



下面我们启动注册中心,以及路由微服务和两个测试微服务。
如下:



我们访问 http://localhost:6543/api-a/hello  成功访问到sercice1


http://localhost:6543/api-b/hello  成功访问到sercice2



ok,一个简单的路由就搭建起来了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: