返回顶部
扩大
缩小

Heaton

微服务zuul(七)


开始干,新建cloud-zuul-gateway-9527,pom文件如下

    <?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>cloud2018</artifactId>
            <groupId>com.tzy.spring</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-zuul-gateway-9527</artifactId>
        <dependencies>
            <!-- 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>
                <artifactId>cloud-api</artifactId>
                <groupId>com.tzy.spring</groupId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
            <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>
            </dependency>
            <!-- 热部署插件 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
        </dependencies>        
    </project>

>yml文件
server: 
  port: 9527
 
spring: 
  application:
    name: cloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true
 
info:                                                       #点击别名后返回的信息,服务发现后打印的信息
  app.name: lalala
  company.name: www.cnblogs.com/ttzzyy
  build.artifactId: $project.artifactId$
  build.version: $project.version$

>启动类
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
	
	@SpringBootApplication
	@EnableZuulProxy
	public class Zuul_9527_StartSpringCloudApp
	{
		public static void main(String[] args)
		{
			SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
		}
	}

>然后开启7001-3,cloud-provider-dept-8001,和当前9527 [http://localhost:7001/](http://localhost:7001/) ![](https://images2018.cnblogs.com/blog/1235870/201805/1235870-20180523220222838-129539372.png) [http://localhost:8001/dept/get/2](http://localhost:8001/dept/get/2) ![](https://images2018.cnblogs.com/blog/1235870/201805/1235870-20180523220259954-1469545691.png) [http://localhost:9527/cloud-dept/dept/get/1](http://localhost:9527/cloud-dept/dept/get/1) ![](https://images2018.cnblogs.com/blog/1235870/201805/1235870-20180523220432265-979384304.png) >这样就通过zuul路由访问到了微服务

既然要用zuul肯定会遇到路由多个微服务,还有名字统一,zuul自动给我们路由,怎么办?
修改yml文件,加入

zuul:
  #ignored-services: cloud-dept                             #原路径失效(单个)
  prefix: /tzy                                             #调用前路径
  ignored-services: "*"                                   #原路径失效(批处理)
  routes:
    mydept.serviceId: cloud-dept                          #访问cloud-dept这个微服务的全名
    mydept.path: /mydept/**                               #可以理解为别名

[http://localhost:9527/tzy/mydept/dept/get/1](http://localhost:9527/tzy/mydept/dept/get/1) ![](https://images2018.cnblogs.com/blog/1235870/201805/1235870-20180523221453795-582058116.png)

补充,之前我们定义了

修改hosts文件

http://gateway-9527.com:9527/tzy/mydept/dept/get/1

成功了哦

posted on 2018-05-23 22:22  咘雷扎克  阅读(246)  评论(0编辑  收藏  举报

导航