spring-cloud-zuul服务网关(五)

1、Java结构
Eureka注册服务中心,Hystrix断路器、Ribbon服务调用负载均衡,以及spring cloud config 集群配置中心,似乎一个微服务框架已五脏俱全,last but not least,一个服务网关却不可或缺。在创建好注册中心、服务提供方的基础之上,再来演示一下zuul网关服务
spring-cloud-zuul服务网关(五)_第1张图片
spring-cloud-zuul服务网关(五)_第2张图片

2、application启动配置

package com.wei.qin.spring_cloud_zuul;

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

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

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

3、properties配置

在这里的serviceId即为其他客户端注册到服务中心的spring.application.name

spring.application.name=api-gateway
server.port=5555

zuul.routes.client.path=/client/**
zuul.routes.client.serviceId=spring-cloud-config-client

eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

4、pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.wei.qingroupId>
  <artifactId>spring-cloud-zuulartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>spring-cloud-zuulname>
  <url>http://maven.apache.orgurl>

   <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.5.RELEASEversion>
        <relativePath />
    parent>
   <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Dalston.RELEASEspring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

启动成功之后,可以看到网关也注册在服务发现中心,如下图所示:

spring-cloud-zuul服务网关(五)_第3张图片

使用网关来访问:http://localhost:5555/client/hello?name=LOVE
之前的访问地址是:http://localhost:8002/hello?name=LOVE
下面是两种URL访问的情况,如下图所示:

spring-cloud-zuul服务网关(五)_第4张图片
spring-cloud-zuul服务网关(五)_第5张图片

你可能感兴趣的:(java,spring,cloud)