Spring Cloud Zuul-阿里云开发者社区

开发者社区> 开发与运维> 正文

Spring Cloud Zuul

简介: Spring 官方文档

Spring 官方文档

注册中心model

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Dalston.SR3'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
package registerserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


/**
 * 注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class RegisterCenterApplication {

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

application.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/



项目目录结构

image

配置中心model

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Dalston.SR3'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
package com.cn.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigeServerApplication {

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

application.yml


# 自定义变量
register-center-url: http://localhost:8761/eureka/
# 注册中心地址
eureka.client.serviceUrl.defaultZone: ${register-center-url}
# 配置中心存储配置文件的方式
spring.profiles.active: native
# 应用名称
spring.application.name: config-server
# 端口
server.port: 8888
#本地配置文件存放路径 (${spring.profiles.active} 为native 是才有效)
spring.cloud.config.server.native.searchLocations: classpath:/config
zuul.ignore-local-service=

项目目录结构
image

zuul网管 model

buildscript {
    ext {
        springBootVersion = '1.5.7.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.cn.pppcar'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Dalston.SR3'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-zuul')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
package com.cn.pppcar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulGatewayApplication {

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


    @Bean
    public AccessFilter accessFilter() {
        return new AccessFilter();
    }
}
package com.cn.pppcar;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by nurmemet on 9/19/2017.
 */

public class AccessFilter extends ZuulFilter {
    private static Logger log = LoggerFactory.getLogger(AccessFilter.class);
    @Override
    public String filterType() {
        return "pre";
    }
    @Override
    public int filterOrder() {
        return -10;
    }
    @Override
    public boolean shouldFilter() {
        return true;
    }
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
        return null;
    }

}

bootstrap.properties



# Flag to say that remote configuration is enabled. Default true;
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
# Name of application used to fetch remote properties.
# 远程配置文件名称  如 service0-{profile}.properties
# @Value("${spring.application.name:application}")
#
spring.cloud.config.name=zuul-gateway
# The URI of the remote server (default http://localhost:8888). 配置服务器
spring.cloud.config.uri=http://localhost:8888
# 要启用的环境(test,pro,dev 之类的) 配置服务器得有 ${spring.cloud.config.name}-{spring.cloud.config.profile}.properties文件
spring.cloud.config.profile=test
#spring.cloud.config.label=config-label-test

在配置中心的配置

zuul:
  ignoredServices: '*'
  routes:
    service0:
      path: /hello/**
      stripPrefix: false
#      serviceId: service0   
#也可以用serviceId
      url: http://localhost:8891/       
      sensitive-headers: Cookie,Set-Cookie
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


spring:
  application:
    name: api-gateway
server:
  port: 8002

项目目录结构
image

service0 model

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Dalston.SR3'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
package com.cn.nur;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient
public class Service0Application {

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




}
package com.cn.nur;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by nurmemet on 9/12/2017.
 */
@RestController
@RequestMapping("hello")
public class MyController {

    @Value("${message}")
    String msg;

    @RequestMapping("hello")
    public String hello(){
        return "hello "+ msg;
    }





}

bootstrap.yml



# Flag to say that remote configuration is enabled. Default true;
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
# Name of application used to fetch remote properties.
# 远程配置文件名称  如 service0-{profile}.properties
# @Value("${spring.application.name:application}")
#
spring.cloud.config.name=service0
# The URI of the remote server (default http://localhost:8888). 配置服务器
spring.cloud.config.uri=http://localhost:8888
# 要启用的环境(test,pro,dev 之类的) 配置服务器得有 ${spring.cloud.config.name}-{spring.cloud.config.profile}.properties文件
spring.cloud.config.profile=test
#spring.cloud.config.label=config-label-test

在配置中心的配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8891

spring.application.name: service0
message: nurmemet-test

依次启动 注册中心,配置中心,zuul网管,service0
打开浏览器 localhost:8671 所有服务是否注册成功
浏览器输入 localhost:8002/hello/hello
网管会把这个请求转发到 localhos:8891/hello/hello
所有hello开头的请求都会转发到service0微服务中去

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章