美文网首页
SpringCloud -Zuul

SpringCloud -Zuul

作者: 刘小刀tina | 来源:发表于2020-02-01 16:18 被阅读0次

Zuul

路由分发:分发给不同的微服务(服务名不同);
负载均衡:分发给同一个微服务的不同实例;

  1. pom.xml
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--添加uereka client 依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--Actuator 开启健康检查依赖的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--zuul依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>


    <!--添加SpringCloud的依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  1. 启动类上添加注解
@SpringBootApplication
@EnableEurekaClient //表明是Eureka客户端
@EnableZuulProxy //开启zuul组件
public class SpringcloudZuul6001Application {

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

}


3,配置文件

#指定端口号
server:
  port: 6001

#配置Eureka
spring:
  application:
    name: zuul
#开启eureka安全认证
  security:
    user:
      password: 123
      name: tina
eureka:
  client:
    service-url:
      defaultZone: http://tina:123456@localhost:8001/eureka/
    healthcheck:
      #开启健康检查
      enabled: true
  instance:
    #采用IP注册
    prefer-ip-address: true
    #定义实例ID的格式
    instance.id: ${spring.application.name}:${sping.cloud.client.ip.address}:${server.port}
    #自定义实例跳转链接
#    status-page-url: http://tina:123456@localhost:6001/eureka-provider/
    #表示eureka client 发送心跳给server端的频率
    lease-renewal-interval-in-seconds: 5
    #表示eureka client 发送心跳给server端频率超过如下设置时间,service端则移除该实例
    lease-expiration-duration-in-seconds: 5

# 配置zuul
zuul:
  routes:
    eureka-provider: /eureka-provider/**
    eureka-consumer: /eureka-consumer/**
  prefix: /api #设置前缀

相关文章

网友评论

      本文标题:SpringCloud -Zuul

      本文链接:https://www.haomeiwen.com/subject/tufxxhtx.html