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>
新建一个项目
设置项目名称
阿波罗文本配置
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]
发布
在本地项目中创建application.properties文件,app.id
是服务名称,apollo.meta
是注册中心地址
app.id=app-shop-zuul
apollo.meta=http://192.168.78.130:8080
配置启动类
@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;
}
}
}
在浏览器输入网关服务名称+/swagger-ui.html
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »