Alibaba网关服务

title: Alibaba网关服务
date: 2019-08-21 09:18:55
categories: 项目实战
tags:

  • gateway
  • spring cloud alibaba
    cover: https://www.github.com/OneJane/blog/raw/master/小书匠/1566388361268.png
基于spring cloud alibaba实战整合开发
ht-micro-record-service-gateway
【Alibaba网关服务】com.htdc ht-micro-record-dependencies 1.0.0-SNAPSHOT ../ht-micro-record-dependencies/pom.xml ht-micro-record-service-gatewayjarht-micro-record-service-gateway http://www.htdatacloud.com/ 2019-Now org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba fastjson com.google.guava guava org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery com.google.guava guava com.alibaba fastjson org.springframework.cloud spring-cloud-starter-alibaba-sentinel org.springframework.cloud spring-cloud-starter-openfeign com.google.code.findbugs jsr305 org.hdrhistogram HdrHistogram org.springframework.cloud spring-cloud-starter-gateway javax.servlet javax.servlet-api com.spotify docker-maven-plugin 0.4.13 192.168.2.7:5000/${project.artifactId}:${project.version} onejane-jdk1.8 ["java", "-jar","/${project.build.finalName}.jar"] / ${project.build.directory} ${project.build.finalName}.jar http://192.168.2.7:2375 org.springframework.boot spring-boot-maven-plugin com.ht.micro.record.service.gateway.GatewayServiceApplication

bootstrap.properties
spring.application.name=ht-micro-record-service-gateway-config spring.main.allow-bean-definition-overriding=true spring.cloud.nacos.config.file-extension=yaml spring.cloud.nacos.config.server-addr=192.168.2.7:8848,192.168.2.7:8849,192.168.2.7:8850

nacos配置
spring: application: name: ht-micro-record-service-gateway jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 default-property-inclusion: non_null cloud: nacos: discovery: server-addr: 192.168.2.7:8848,192.168.2.7:8849,192.168.2.7:8850 sentinel: transport: port: 8721 dashboard: 192.168.2.7:190 gateway: discovery: locator: enabled: true routes: http://localhost:9000/api/user/user/20 访问user服务 - id: HT-MICRO-RECORD-SERVICE-USER uri: lb://ht-micro-record-service-user predicates: - Path=/api/user/** filters: - StripPrefix=2server: port: 9000feign: sentinel: enabled: truemanagement: endpoints: web: exposure: include: "*"logging: level: org.springframework.cloud.gateway: debug

GatewayServiceApplication.java
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class GatewayServiceApplication {// ----------------------------- 解决跨域 Begin -----------------------------private static final String ALL = "*"; private static final String MAX_AGE = "18000L"; @Bean public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties); }@Bean public ServerCodecConfigurer serverCodecConfigurer() { return new DefaultServerCodecConfigurer(); }@Bean public WebFilter corsFilter() { return (ServerWebExchange ctx, WebFilterChain chain) -> { ServerHttpRequest request = ctx.getRequest(); if (!CorsUtils.isCorsRequest(request)) { return chain.filter(ctx); } HttpHeaders requestHeaders = request.getHeaders(); ServerHttpResponse response = ctx.getResponse(); HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod(); HttpHeaders headers = response.getHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin()); headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders()); if (requestMethod != null) { headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name()); } headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL); headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE); if (request.getMethod() == HttpMethod.OPTIONS) { response.setStatusCode(HttpStatus.OK); return Mono.empty(); } return chain.filter(ctx); }; }// ----------------------------- 解决跨域 End -----------------------------public static void main(String[] args) { SpringApplication.run(GatewayServiceApplication.class, args); } }

    推荐阅读