# ZUUL 使用

2018-11-01  本文已影响0人  夜寻

ZUUL 使用

Zuul项目

pom.xml 中添加

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

ZuulDemoApplication.java中标记启用Zuul

/**
 * @author HouZm
 * @EnableZuulProxy简单理解为@EnableZuulServer的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用@EnableZuulProxy。
 * @EnableZuulProxy 启用Zuul
 */
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

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

application.properties中添加配置

zuul.routes.client1.path=/client1/**
zuul.routes.client1.service-id=DEV-CLIENT-1

zuul.routes.client2.path=/client2/**
zuul.routes.client2.service-id=dev-client-2
## 
zuul.routes.client2.strip-prefix= true

其他配套代码

另外启用了 Eureka注册中心、client-1、client-2

2个客户端都向Eureka进行了注册

client-1

application.properties
#向erueka注册
eureka.instance.leaseRenewalIntervalInSeconds= 30
eureka.instance.prefer-ip-address=true
eureka.client.registryFetchIntervalSeconds= 15
## 配置的Eureka服务地址 以下地址为内网
eureka.client.serviceUrl.defaultZone=http://10.60.110.8:8199/eureka-server/eureka/,http://10.60.110\
  .9:8199/eureka-server/eureka/,http://10.60.110.10:8199/eureka-server/eureka/

server.servlet.context-path=
# 如果没有contextPath的话这些不用配置
eureka.instance.home-page-url-path=${server.servlet.context-path}
eureka.instance.health-check-url-path=${server.servlet.context-path}/actuator/health
eureka.instance.status-page-url-path=${server.servlet.context-path}/actuator/info
eureka.instance.metadata-map.management.context-path=${server.servlet.context-path}/actuator

server.port=8182

spring.application.name=DEV-CLIENT-1

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

IndexController.java

/**
 * @author HouZm
 * @date 2018/11/1 10:39
 */
@RestController
@RequestMapping("/")
public class IndexController {

    @RequestMapping("")
    public String index(){
        return "我是Client1 ";
    }
}

client-2

application.properties

#向erueka注册
eureka.instance.leaseRenewalIntervalInSeconds= 30
eureka.instance.prefer-ip-address=true
eureka.client.registryFetchIntervalSeconds= 15
eureka.client.serviceUrl.defaultZone=http://10.60.110.8:8199/eureka-server/eureka/,http://10.60.110\
  .9:8199/eureka-server/eureka/,http://10.60.110.10:8199/eureka-server/eureka/

server.servlet.context-path=
# 如果没有contextPath的话这些不用配置
eureka.instance.home-page-url-path=${server.servlet.context-path}
eureka.instance.health-check-url-path=${server.servlet.context-path}/actuator/health
eureka.instance.status-page-url-path=${server.servlet.context-path}/actuator/info
eureka.instance.metadata-map.management.context-path=${server.servlet.context-path}/actuator

server.port=8183

spring.application.name=DEV-CLIENT-2
## 启用actuator 端口,默认只公开了/health 和 /info 端点
## 公开除 env 端点之外的所有(已启用的) web 端点:
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

IndexController.java

/**
 * @author HouZm
 * @date 2018/11/1 10:39
 */
@RestController
@RequestMapping("/")
public class IndexController {

    @RequestMapping("")
    public String index(){
        return "我是Client2 ";
    }
}

测试访问

启动3个服务

访问 http://localhost:8184/client2,出现

我是Client2

访问:http://localhost:8184/client1

我是Client1

证明Zuul路由已成功

代码地址:代码地址

上一篇下一篇

猜你喜欢

热点阅读