java1234开源博客系统
博客信息

Zuul 路由配置

发布时间:『 2018-10-23 23:42』  博客类别:SpringCloud  阅读(4782) 评论(0)

Zuul 路由配置


我们新建一个module  microservice-zuul-3001

这里我们的zuul也注册到eureka服务里,端口3001;

我们修改下Hosts,专门为zuul搞个本地域名映射 

hosts文件 加下:

127.0.0.1  zuul.java1234.com


然后pom.xml要加上:

<!-- zuul路由网关 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zuul</artifactId>

</dependency>


完整pom.xml

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

  <modelVersion>4.0.0</modelVersion>

  <parent>

    <groupId>com.java1234.springcloud</groupId>

    <artifactId>microservice</artifactId>

    <version>0.0.1-SNAPSHOT</version>

  </parent>

  <artifactId>microservice-zuul-3001</artifactId>

  

  <dependencies>

  

    <dependency>

          <groupId>com.java1234.springcloud</groupId>

          <artifactId>microservice-common</artifactId>

         <version>${project.version}</version>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <!-- zuul路由网关 -->

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

<!-- actuator监控 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<!-- hystrix容错 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

    

     <!-- 修改后立即生效,热部署 -->

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>springloaded</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

    </dependency>

    

  

  </dependencies>

</project>



搞一个 application.yml 

server:

  port: 3001

  context-path: /


spring:

  application:

    name: microservice-zuul


eureka:

  instance:

    instance-id: microservice-zuul:3001 #客户端实例名称

    prefer-ip-address: true #显示IP

  client: 

    service-url: 

      defaultZone: http://eureka2001.java1234.com:2001/eureka/,http://eureka2002.java1234.com:2002/eureka/,http://eureka2003.java1234.com:2003/eureka/ # 集群


info: 

   groupId: $project.groupId$

   artifactId: $project.artifactId$

   version: $project.version$

   负责人: 王五

   联系电话: 110



建一个主启动类:ZuulApplication_3001

加下@EnableZuulProxy注解

package com.java1234;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableZuulProxy
public class ZuulApplication_3001 {

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


我们测试下:

启动三个eureka 然后再启动下一个1001服务,以及 zuul网关服务;

1.jpg


这里有两个服务;


我们直接请求:http://localhost:1001/student/list 能获取到数据;

我们用 http://zuul.java1234.com:3001/microservice-student/student/list  域名+端口+服务名称+请求地址 也能请求到数据;


说明我们的路由基本配置OK


关键字:   Zuul     路由配置  
关注Java1234微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有