APIゲートウェイサービス:春クラウドZuul(f)のノートを使用して

zuulアプリケーション側

春・クラウドデモ・プロジェクトにモジュールを追加します。zuulアプリは、
pom.xmlファイルに依存して、コメントを追加しました:

<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>

リソースディレクトリ内のapplication.propertiesファイルを増やして、次の行を追加します。

server.port=8080
spring.application.name=zuul-app
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

zuul.routes.zuul-use.path=/api/**
zuul.routes.zuul-use.serviceId=zuul-use

JavaのカタログにSpringBootプロジェクトの起動クラスを作成します。次のようにZuulApplicationは、読み取ります。

/**
 * @CalssName ZuulApplication
 * @Description TODO
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {

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

zuul最終用途

春・クラウドデモ・プロジェクトにモジュールを追加します。zuul-使用は
のpom.xmlファイルに依存して参加します:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

リソースディレクトリ内のapplication.propertiesファイルを増やして、次の行を追加します。

server.port=8903
spring.application.name=zuul-use
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

JavaのカタログにSpringBootプロジェクトの起動クラスを作成します。次のようにZuulUseApplicationは、読み取ります。

/**
 * @CalssName ZuulUseApplication
 * @Description TODO
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ZuulUseApplication {

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

コントローラーの作成:次のようにHelloControllerは、読み取ります。

/**
 * @CalssName controller
 * @Description TODO
 */
@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello,I'm from zuul-use...";
    }
}

ユーレカサーバー、zuulアプリとzuul使用、ブラウザアクセスを開始するためにします。http:// localhost:8080 / API /こんにちは、出力は次のとおりです。

こんにちは、私はzuul-使用から来たんです...

説明zuul成功したアプリケーション

Zuul高度利用

次のように使用Zuulフィルタの例は、zuulアプリTokenFilterフィルタ構造では、例えば、トークンフィルタリングします。

/**
 * @CalssName TokenFilter
 * @Description Token Zuul过滤器
 * @Author Alyshen
 */
public class TokenFilter extends ZuulFilter {
    private final Logger logger = LoggerFactory.getLogger(TokenFilter.class);

    @Override
    public String filterType() {
        return "pre"; // 可以在请求被路由之前调用
    }

    @Override
    public int filterOrder() {
        return 0; // filter执行顺序,通过数字指定 ,优先级为0,数字越大,优先级越低
    }

    @Override
    public boolean shouldFilter() {
        return true;// 是否执行该过滤器,此处为true,说明需要过滤
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        logger.info("--->>> TokenFilter {},{}", request.getMethod(), request.getRequestURL().toString());

        String token = request.getParameter("token");// 获取请求的参数

        if (StringUtils.isNotBlank(token)) {
            ctx.setSendZuulResponse(true); //对请求进行路由
            ctx.setResponseStatusCode(200);
            ctx.set("isSuccess", true);
            return null;
        } else {
            ctx.setSendZuulResponse(false); //不对其进行路由
            ctx.setResponseStatusCode(400);
            ctx.setResponseBody("token is empty");
            ctx.set("isSuccess", false);
            return null;
        }
    }
}

次のように起動クラスでzuulアプリを追加しました:

@Bean
public TokenFilter tokenFilter() {
    return new TokenFilter();
}

おすすめ

転載: www.cnblogs.com/yhongyin/p/11183870.html