ゲートウェイを介して分散マイクロサービスのspringcloud + zuul + swagger2

同社の技術インフラのニーズのためには、私はZK +ダボ+ springboot springcloudシリーズのアーキテクチャに接触したしました。まず、新しいものにさらされ、私は心がすべての後に、ある学びたい、スキルはもっと体よりも、[ゴーヤの顔を] springcloudされていない新技術の使用に言及することはありません。この記事では、全体の構成のキーポイントに焦点を当てて、その他の詳細は、私のロビンを見つけてください。

トピックへ:目的のzuulゲートウェイ、限り私は今、いくつかの外部サービスを作ることであるとして、ドメイン名要求は要求を取得した後に、各サービスにゲートウェイに転送されていることを確認します。ユーレカにサービス名で登録された2つ以上のマイクロサービスを、調製の最初のステップ

 1.user-serive

     controller1でのユーザー・サービス・サービス:API / V1 /ユーザー/ userAddress

                                       コントローラ2:API / V1 /ユーザー/ userCompany

 2.orderサービス:上記のように

 3.zuulサービス(ゲートウェイ・サービス)

 第二段階でzuulサービスで転送ポリシーを設定します:

  あなたはインターネットを検索することができ、話すことはありません無視し、機能モジュールを作成Zuul。主に一部の構成application.yml場所ので、私はそれを貼り付けます。

zuul:
  routes:
    order-service:
      path: /**/order/** 此处一定要这样写,不要写成/order/**;否则在进行转发的时候,请求不通;会在转发的时候,前面需要手动加上order,再连接api/v1才能请求;
    user-service:
      path: /**/user/**

 あなたがよく見て欲しい場合は、要求、初めて要求、次のような理由でアウトzuul時間を転送する過程で:(、あなたは下zuul機構に取得することができます)

zuul用途レイジーローディング機構は、最初の訪問は、いくつかのクラスをロードすることになりますので、これはですが、デフォルトの時間が比較的短い持っていたので、あなたは、彼らはいくつかの時間がかかるこれらのクラスをロードし、負荷に起動しないときに残業になります。

ソリューションは、中application.ymlに設定されています。

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
             timeoutInMilliseconds: 5000

  要求アドレスの順序がある場合は、この時点では、それがオーダーサービスに要求を転送します。ユーザアドレスが正常に前進ユーザーのサービス要求に転送されます場合。

 

-------------------------------------------------- ------

次は闊歩協会コードについて投稿しました

闊歩する方法を追加します。

1.のpom.xmlはzuulサービスを追加しました:

<!-- swagger2 依赖 -->
<dependency>
   <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.1</version> </dependency>

2.新しい闊歩の設定クラスを作成します。

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private DiscoveryClientRouteLocator discoveryClientRouteLocator;
    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo()) // .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                .title("对接接口文档")
                .description("相关后台接口")
                .contact("程序猿")
                .version("1.0")
                .build();
    }

 3.新規ドキュメントの設定クラス

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider{

    private final RouteLocator routeLocator;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${spring.application.name}")
    private String applicationName;

    public DocumentationConfig(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }
   // 自动获取系统配置的路由资源集合
   @Override
   public List<SwaggerResource> get() {
       List<SwaggerResource> resources = new ArrayList<>(); // 排除自身,将其他的服务添加进去 discoveryClient.getServices().stream().filter(s -> !s.equals(applicationName)).forEach(name -> { Optional<ServiceInstance> instanceOptional = discoveryClient.getInstances(name).stream().findFirst(); if (instanceOptional.isPresent() && instanceOptional.get().getMetadata().containsKey("context-path")) { String contexPath = instanceOptional.get().getMetadata().get("context-path"); resources.add(swaggerResource(name, "/" + name + contexPath + "/v2/api-docs", "2.0")); } else { resources.add(swaggerResource(name, "/" + name + "/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; } } 

-------------------------------------------------- ------

最後に、全体的な効果は次のとおりです。

 

注意:

何か間違った場所下さい批判があれば、この記事では、共有への純粋に個人的です。

収集のうち解きの部分は、参考のために、私のオンラインコレクションです 

おすすめ

転載: www.cnblogs.com/sunnyguo/p/11826656.html