[bigdata-111] spring-cloud-07 api网关服务
1. api网关 后面有很多服务,对外用api网管统一提供访问。如果一个用户访问api网关,那么,api网关就会将这些调用转发到后面的诸多服务,然后获取服务结果再返回。而后端的服务都是配置出来的。 2. 参考文献 https://github.com/dyc87112/SpringCloudBook/tree/master/api-gateway 3. 源码目录结构 . 4. 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> <groupId>com.brian.demo</groupId> <artifactId>apigateway</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>apigateway</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <repositories> <repository> <id>my-nexus-central</id> <name>my local nexus</name> <url>http://localhost:8081/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 5. App.java package com.brian.demo.apigateway; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper; import org.springframework.context.annotation.Bean; @EnableZuulProxy @SpringCloudApplication public class App { @Bean public PatternServiceRouteMapper serviceRouteMapper() { return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}"); } public static void main(String[] args) { new SpringApplicationBuilder(App.class).web(true).run(args); } } 6. application.properites spring.application.name=api-gateway server.port=5555 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1111/eureka/ zuul.routes.api-a.path=/api/a/** zuul.routes.api-a.serviceId=hello-service zuul.AccessFilter.pre.disable=true 7. 编译打包 mvn clean package 8. 运行 java -jar target/apigateway-0.0.1-SNAPSHOT.jar 9. 查看 根据配置,如果访问http://localhost:5555/api/a/*,那么,api网关会将这个路由转发到HELLO-SERVICE这里,因此,访问http://localhost:5555/api/a/hello,能看到一个字符串。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |