微服务电商项目--搭建zull网关

 说明:本人书写该篇博客原因主要有两个:一、方便本人查阅,二、为全小白且想学微服务的朋友进行查阅。以下内容主要来源于余胜军,本人在他基础上将步骤进行细化,使小白也能看懂,请大家在转载的时候也引入余胜军的链接

1.1. 搭建zull网关

1.1.1. 建立one-shop-basics-springcloud-zuul服务

1.1.1. Pom文件

  
  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!-- swagger-spring-boot -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>
    </dependencies>

1.1.1. Yml文件

###服务启动端口号
server:
  port: 8088
###服务名称(服务注册到eureka名称)  
spring:
    application:
        name: app-one-zuul
###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://127.0.0.1:8100/eureka


### 配置网关反向代理    
zuul:
  routes:
    api-a:
     ### 以 /api-weixin/访问转发到会员服务
      path: /api-weixin/**
      serviceId: app-one-weixin
    api-b:
        ### 以 /api-member/访问转发到订单服务
      path: /api-member/**
      serviceId: app-one-member

  

1.1.1. 启动及测试类

package com.one;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.spring4all.swagger.EnableSwagger2Doc;

import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

/**
 * 微服务网关入口
 * @author 陈远波
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateWay {

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

    // 添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList<>();
            // app-itmayiedu-order
            //网关使用服务别名获取远程的swaggerApi
            resources.add(swaggerResource("app-one-member", "/app-one-member/v2/api-docs", "2.0"));
            resources.add(swaggerResource("app-one-weixin", "/app-one-weixin/v2/api-docs", "2.0"));
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }

    }

}

1.1.1. 启动后的效果

猜你喜欢

转载自www.cnblogs.com/chenyuanbo/p/12112774.html