学习笔记:微服务-5.spring zuul

spring cloud zuul 是网页api网关,就是说微服务有很多的服务,每个服务都有个自己的网址,采用zuul就可以从一个zuul网址对外服务,会自动跳转到设置的对应微服务上

上经典图片

1. 新建一个spring boot 项目 命名为Eureda-client-zuul

2.pom.xml


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot </groupId>
  7. <artifactId>spring-boot-starter-parent </artifactId>
  8. <version>2.1.1.RELEASE </version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linbin </groupId>
  12. <artifactId>Eureda-Client-zuul </artifactId>
  13. <version>0.0.1-SNAPSHOT </version>
  14. <name>Eureda-Client-zuul </name>
  15. <description>Demo project for Spring Boot </description>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  19. <java.version>1.8 </java.version>
  20. <spring-cloud.version>Greenwich.M3 </spring-cloud.version> <!-- Finchley.M8 -->
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot </groupId>
  25. <artifactId>spring-boot-starter-web </artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud </groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud </groupId>
  33. <artifactId>spring-cloud-starter-netflix-zuul </artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot </groupId>
  37. <artifactId>spring-boot-starter-test </artifactId>
  38. <scope>test </scope>
  39. </dependency>
  40. </dependencies>
  41. <dependencyManagement>
  42. <dependencies>
  43. <dependency>
  44. <groupId>org.springframework.cloud </groupId>
  45. <artifactId>spring-cloud-dependencies </artifactId>
  46. <version>${spring-cloud.version} </version>
  47. <type>pom </type>
  48. <scope>import </scope>
  49. </dependency>
  50. </dependencies>
  51. </dependencyManagement>
  52. <repositories>
  53. <repository>
  54. <id>spring-milestones </id>
  55. <name>Spring Milestones </name>
  56. <url>https://repo.spring.io/milestone </url>
  57. <snapshots>
  58. <enabled>false </enabled>
  59. </snapshots>
  60. </repository>
  61. </repositories>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot </groupId>
  66. <artifactId>spring-boot-maven-plugin </artifactId>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

3. application.properties

server.port=8000
eureka.client.serviceUrl.defaultZone=http://admin:123@centos7:8888/eureka/
spring.application.name=Euredaclientzuul
zuul.routes.euredaclient1.path=/c1/**
zuul.routes.euredaclient1.serviceId=Euredaclient1
zuul.routes.springdemo.path=/sd/**
zuul.routes.springdemo.serviceId=SpringDemo

4. 启动类前加 @EnableZuulProxy

@EnableZuulProxy
@SpringBootApplication
public class EuredaClientZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuredaClientZuulApplication.class, args);
    }
}

5.运行项目

http://centos7:8000/c1/hello    会自动跳转到Euredaclient1微服务地址即: http://centos7:8081/hello 

http://centos7:8000/sd/hello  会自动跳转到SpringDemo微服务地址即: http://centos7:8082/hello

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值