第二の研究SpringCloud:使用領事レジストリ(Greenwich.SR1版)

A、領事のインストールと設定

  https://www.cnblogs.com/yangk1996/p/10742911.html

第二に、サービス登録

  • Mavenのは、導入依存しています
    <親> 
        <groupIdを> com.yk </ groupIdを> 
        <たartifactId>クラウド親</たartifactId> 
        <バージョン> 0.0.1-SNAPSHOT </バージョン> 
    </親> 
    <たartifactId> springcloud-領事-クライアント</たartifactId > 
    <依存性> 
        <依存性> 
            <のgroupId> org.springframework.cloud </のgroupId> 
            <たartifactId>ばねクラウドスタータ領事発見</たartifactId> 
        </依存> 
    </依存関係>
  • サービス検出
@SpringBootApplication(= DataSourceAutoConfiguration.classを除外する)
@EnableDiscoveryClient //注册发现
パブリッククラスConsulApplication { 

    パブリック静的無効メイン(文字列[] args){ 
        SpringApplication.run(ConsulApplication.class、引数)。
    } 

}
  • YMLの設定
春Cloud- = spring.application.name総領事、クライアント
はserver.port = 8080 

サーバーの#領事接続構成
#領事ホストアドレス
spring.cloud.consul.host = http://192.168.100.129 
#領事サービスポート
spring.cloud 8500 = .consul.port 

#ヘルス・調整・パスは、それはあなたが失敗した上で登録されたサービス構成しない場合は、サーバーの領事、ヘルプ・コールバックに渡さ
spring.cloud.consul.discovery.healthCheckPath = /チェック

ホストアドレス#を、これはデフォルトのサービスです機械
spring.cloud.consul.discovery.hostname = 192.168.0.102
  • テストのデモ
@RestController 
パブリッククラスDiscoveryClientController { 
    @Autowired 
    プライベートDiscoveryClient discoveryClient; 

    @value( "spring.application.nameの$ {}")
    プライベートのApplicationName文字列; 

    / ** 
     * @author:yangk 
     * @description:サービス名をすべて取得します
     * @Param :[] 
     * @return:java.util.Listに<java.lang.Stringで> 
     * @date:2019インディアン06越09 Riを10市フェン38 
     * / 
    @GetMapping( "listServices")
    パブリックリストの<string> listServices() { 
        )(discoveryClient.getServicesを返す; 
    } 

    / ** 
     * @author:yangk 
     * @description:すべてのサービスインスタンスの情報を取得 
     * @Param。
     * @return。
     * @Date:2019年06月09日10时41分
     * / 
    @GetMapping( "listInstance")
    公衆リスト<ServiceInstance> listInstance(){ 
        リストの<string>サーバ= listServices()。
        LinkedListは<ServiceInstance> serviceInstances =新しいLinkedListは<>(); 
        servers.forEach(serviceNameを- > { 
            serviceInstances.addAll(discoveryClient.getInstances(serviceNameを)); 
        }); 
        serviceInstancesを返します。
    } 

    / ** 
     * @Author:yangk 
     * @description:获取当前应用的实例
     * @Param:
     * @return:
     * @Date:2019年06月09日10时44分 
     * /
    @GetMapping( "getCurrentServiceInstance")
    公共ServiceInstance getCurrentServiceInstance(){ 
        戻りdiscoveryClient.getInstances(のApplicationName)に.get(0)。
    } 
  / ** 
  * @Author:yangk 
  * @description:コンサル服务器的回调
  * @Param:   
  * @return:   
  * @Date:2019年06月09日12时51分
  * / 
    @GetMapping( "/チェック")
    公衆Stringチェック(){ 
        "OK"を返します。
    } 

}

第三に、スタート

  • 2つのクライアントを起動します

  • 8500クエリ領事UIページ:IPで

  • 表示例

 

 

おすすめ

転載: www.cnblogs.com/yangk1996/p/10993201.html