SpringCloud Eureka+zuul 注册中心以及路由搭建(二)

SpringCloud Eureka+zuul 注册中心以及路由搭建(二)

上一篇搭建了注册中心,这一篇主要就是搭建zuul路由。

zuul路由搭建

创建一个新项目,maven引用如下:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC1</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-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 注意是starter,自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- 不是starter,手动配置 -->
       <!-- <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>

配置文件application.yml

spring:
  application:
    name: zuulServer
server:
  port: 1006
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1000/eureka
  instance:
    lease-expiration-duration-in-seconds: 30 #服务过期时间配置,超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除
    lease-renewal-interval-in-seconds: 10 #服务刷新时间配置,每隔这个时间会主动心跳一次
zuul:
  host:
    connect-timeout-millis: 6000
    socket-timeout-millis: 10000 #默认就是10000
logging.level.org.springframework.security: DEBUG
ribbon:
  eureka:
    enabled: true
#  ConnectTimeout: 6000 #ribbon请求连接的超时时间- 限制3秒内必须请求到服务,并不限制服务处理的返回时间
#  ReadTimeout: 6000 #请求处理的超时时间 下级服务响应最大时间,超出时间消费方(路由也是消费方)返回timeout

最后启动文件加上
@EnableZuulProxy
@EnableDiscoveryClient
基本配置就好了。下一篇主要是zuul路由的异常拦截、跨域请求等

你可能感兴趣的:(springcloud)