1. 程式人生 > 其它 >springboot整合zuul實操

springboot整合zuul實操

選擇在專案中新起一個zuul 模組,以達到業務需要。網上大部分是要zuuleureka 一起使用的demo,但是eureka 我們專案業務沒有用到,沒必要整合。

我參考的部落格地址(步驟有問題,專案起不來,後面詳細說):

https://www.cnblogs.com/gdjlc/p/11899276.html

第一步:新起一個maven 工程

第二步:新增maven pom.xml依賴

<?xml version="1.0" encoding="UTF-8"?>
<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"> <parent> <artifactId>icnoc-data</artifactId> <groupId>com.hginfo.icnoc-data</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>zuul-core</artifactId> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import
</scope> </dependency> </dependencies> </dependencyManagement> </project>
View Code

第三步:編寫啟動類,開啟zuul

package com.hginfo.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @EnableZuulProxy public class ZuulServerApplication { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication.class, args); } @GetMapping("/hello/{name}") public String hello(@PathVariable String name){ return "hello " + name; } }
View Code

第四步:配置application.yml

zuul:
  routes:
    test:
      path: /a/**
      url: http://localhost:8090
    b:
      url: https://www.baidu.com/

server:
  port: 8090
View Code

第五步:加入LoadBalancerAutoConfiguration 配置類(不然啟動ribbon報錯)

package com.hginfo.zuul.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

/**
 * Auto configuration for Ribbon (client side load balancing).
 *
 * @author Spencer Gibb
 * @author Dave Syer
 */
//表示這是一個配置類
@Configuration
//必須存在RestTemplate類
@ConditionalOnClass(RestTemplate.class)
//必須存在LoadBalancerClient型別的bean
@ConditionalOnBean(LoadBalancerClient.class)
public class LoadBalancerAutoConfiguration {

    //所有被@LoadBalanced註解修飾的RestTemplate
//    @LoadBalanced
    @Autowired(required = false)
    private List<RestTemplate> restTemplates = Collections.emptyList();

    //對於所有被@LoadBalanced註解修飾的RestTemplate,呼叫SmartInitializingSingleton的
    //customize方法
    @Bean
    public SmartInitializingSingleton loadBalancedRestTemplateInitializer(
            final List<RestTemplateCustomizer> customizers) {
        return new SmartInitializingSingleton() {
            @Override
            public void afterSingletonsInstantiated() {
                for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) {
                    for (RestTemplateCustomizer customizer : customizers) {
                        customizer.customize(restTemplate);
                    }
                }
            }
        };
    }

    //對於所有被@LoadBalanced註解修飾的RestTemplate,增加loadBalancerInterceptor屬性
    @Bean
    @ConditionalOnMissingBean
    public RestTemplateCustomizer restTemplateCustomizer(
            final LoadBalancerInterceptor loadBalancerInterceptor) {
        return new RestTemplateCustomizer() {
            @Override
            public void customize(RestTemplate restTemplate) {
                List<ClientHttpRequestInterceptor> list = new ArrayList<>(
                        restTemplate.getInterceptors());
                list.add(loadBalancerInterceptor);
                restTemplate.setInterceptors(list);
            }
        };
    }

    //產生一個LoadBalancerInterceptor型別的bean,包含loadBalancerClient型別的bean
    @Bean
    public LoadBalancerInterceptor ribbonInterceptor(
            LoadBalancerClient loadBalancerClient) {
        return new LoadBalancerInterceptor(loadBalancerClient);
    }
}
View Code

然後訪問:http://localhost:8090/a/hello/xiaoming,效果如下圖

訪問 http://localhost:8090/b/,效果如下: