宋子宪博客

Szx

Apollo整合zuul服务网关
在pom文件引入maven依赖 <dependency> <groupId...
扫描右侧二维码阅读全文
17
2019/05

Apollo整合zuul服务网关

在pom文件引入maven依赖

    <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>

    <!-- spring-boot整合 Apollo-->
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.0.0</version>
    </dependency>

    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-core</artifactId>
        <version>1.0.0</version>
    </dependency>

Image 2.png

新建一个项目
3.png

设置项目名称
Image 7.png

阿波罗文本配置

server.port = 80
spring.application.name = app-shop-zuul
eureka.client.service-url.defaultZone = http://192.168.78.130:8080/eureka
zuul.routes.api-a.path = /api-weixin/**
zuul.routes.api-a.serviceId = app-shop-weixin
zuul.routes.api-b.path = /api-member/**
zuul.routes.api-b.serviceId = app-shop-member
appid=app-shop-zuul-document = [\n{\n        "name": "app-shop-member",\n        "location": "/app-shop-member/v2/api-docs",\n        "version": "2.0"\n    },\n    {\n        "name": "app-shou-weixin",\n        "location": "/app-shop-weixin/v2/api-docs",\n        "version": "2.0"\n    }\n]

Image 8.png

发布
Image 9.png

在本地项目中创建application.properties文件,app.id是服务名称,apollo.meta是注册中心地址

app.id=app-shop-zuul
apollo.meta=http://192.168.78.130:8080

Image 10.png

配置启动类

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@EnableApolloConfig
public class AppGateWay {

    //
    // @ApolloConfig
    // private Config config;
    @Value("${appid=app-shop-zuul-document}")
    private String document;

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

    // 添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            return resources();
        }

        private List<SwaggerResource> resources() {
            // 从阿波罗平台获取配置文件
            // String swaDocJson =
            // config.getProperty("mayikt.zuul.swagger.document", null);
            JSONArray docJsonArray = JSONArray.parseArray(document);
            List resources = new ArrayList<>();
            // 遍历集合数组
            for (Object object : docJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                String name = jsonObject.getString("name");
                String location = jsonObject.getString("location");
                String version = jsonObject.getString("version");
                resources.add(swaggerResource(name, location, version));
            }
            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;
        }

    }

}

Image 11.png

在浏览器输入网关服务名称+/swagger-ui.html
Image 12.png

Last modification:August 16th, 2019 at 10:49 pm
如果觉得这篇技术文章对您有帮助,可以请博主喝一杯饮料

Leave a Comment