乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      springcloud(第三篇)springcloud eureka 服務(wù)注冊與發(fā)現(xiàn)

       WindySky 2017-07-17

      spring cloud eureka

      eureka 用以服務(wù)發(fā)現(xiàn)、服務(wù)注冊,比較流行的有consul

      簡介

      eureka為netflix開源軟件,分為三個部分:

      • eureka服務(wù):用以提供服務(wù)注冊、發(fā)現(xiàn),已一個war的形式提供 http://search./#search%7Cga%7C1%7Ceureka-server
        或者編譯源碼,將war拷貝進tomcat即可提供服務(wù)

      • eureka-server: 相對client端的服務(wù)端,為客戶端提供服務(wù),通常情況下為一個
        集群

      • eureka-client:客戶端,通過向eureka服務(wù)發(fā)現(xiàn)注冊的可用的eureka-server,向后端發(fā)送請求

      spring cloud eureka

      spring cloud eureka 分為兩部分

      • @EnableEurekaClient: 該注解表明應(yīng)用既作為eureka實例又為eureka client 可以發(fā)現(xiàn)注冊的服務(wù)
      • @EnableEurekaServer: 該注解表明應(yīng)用為eureka服務(wù),有可以聯(lián)合多個服務(wù)作為集群,對外提供服務(wù)注冊以及發(fā)現(xiàn)功能

      client

      pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven./POM/4.0.0"
               xmlns:xsi="http://www./2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>springcloud</artifactId>
              <groupId>com.lkl.springcloud</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>eureka</artifactId>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-netflix</artifactId>
                      <version>1.0.7.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka</artifactId>
              </dependency>
      
              <!--表示為web工程-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      
              <!--暴露各種指標-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
      
          </dependencies>
      </project>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44

      啟動應(yīng)用 Application.Java

      package com.lkl.springcloud.eureka;
      
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.boot.builder.SpringApplicationBuilder;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * Created by liaokailin on 16/4/30.
       */
      @SpringBootApplication
      @EnableEurekaClient
      @RestController
      @EnableAutoConfiguration
      public class Application {
          @RequestMapping("/")
          public String home() {
              return "Hello world";
          }
      
          public static void main(String[] args) {
              new SpringApplicationBuilder(Application.class).web(true).run(args);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29

      創(chuàng)建配置application.properties

      server.port=9090
      spring.application.name=eureka.client
      #eureka.client.serviceUrl.defaultZone=http://120.76.145.187:8080/eureka-server-1.4.6/v2/
      eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/
      eureka.instance.appname=eureka.client.01
      #eureka.client.registerWithEureka=true
      #eureka.client.fetchRegistry=true
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      eureka.client.serviceUrl.defaultZone 配置eureka服務(wù)地址

      server

      pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven./POM/4.0.0"
               xmlns:xsi="http://www./2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>springcloud</artifactId>
              <groupId>com.lkl.springcloud</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>eureka-server</artifactId>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-netflix</artifactId>
                      <version>1.0.7.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka</artifactId>
              </dependency>
      
              <!--表示為web工程-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      
              <!--暴露各種指標-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka-server</artifactId>
              </dependency>
      
      
          </dependencies>
      
      </project>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51

      啟動應(yīng)用Application.java

      package com.lkl.springcloud.eureka;
      
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.boot.builder.SpringApplicationBuilder;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
      
      /**
       * Created by liaokailin on 16/4/30.
       */
      @EnableEurekaServer
      @SpringBootApplication
      public class Application {
      
          public static void main(String[] args) {
              new SpringApplicationBuilder(Application.class).web(true).run(args);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19

      配置application.yml

      server:
        port: 7070
      spring:
        application:
          name: server-01
      eureka:
        client:
          serviceUrl:
            defaultZone: http://localhost:7070/eureka/
        instance:
          metadataMap:
            instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

      spring.application.name表示應(yīng)用名稱,集群名稱需要一致
      defaultZone表示向自身注冊,例子中有三個server節(jié)點構(gòu)成集群,其余兩個兩個節(jié)點也向該端口注冊
      instanceId 表示eureka instance 標識,需要唯一,如果不配置,多個節(jié)點最終只會有一個生效

      同樣的配置第二個節(jié)點,第三個節(jié)點

      啟動所有服務(wù)端應(yīng)用
      訪問 http://localhost:7070 可以查看eureka注冊服務(wù)信息

      訪問 http://localhost:7070/eureka/apps 可以查看metadata

      服務(wù)發(fā)現(xiàn)

      在客戶端創(chuàng)建Component

      DiscoveryService.java

      package com.lkl.springcloud.eureka;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.CommandLineRunner;
      import org.springframework.cloud.client.ServiceInstance;
      import org.springframework.cloud.client.discovery.DiscoveryClient;
      import org.springframework.stereotype.Component;
      import org.springframework.util.CollectionUtils;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import java.util.List;
      
      /**
       * Created by liaokailin on 16/4/30.
       */
      @Component
      @RestController
      public class DiscoveryService {
      
          @Autowired
          private DiscoveryClient discoveryClient;
      
          @RequestMapping("/discovery")
          public String doDiscoveryService(){
              StringBuilder buf = new StringBuilder();
              List<String> serviceIds = discoveryClient.getServices();
              if(!CollectionUtils.isEmpty(serviceIds)){
                  for(String s : serviceIds){
                      System.out.println("serviceId:" + s);
                      List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(s);
                      if(!CollectionUtils.isEmpty(serviceInstances)){
                          for(ServiceInstance si:serviceInstances){
                              buf.append("["+si.getServiceId() +" host=" +si.getHost()+" port="+si.getPort()+" uri="+si.getUri()+"]");
                          }
                      }else{
                          buf.append("no service.");
                      }
                  }
              }
      
      
              return buf.toString();
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47

      訪問 http://localhost:9090/discovery 實現(xiàn)服務(wù)發(fā)現(xiàn)。

      ok ~ it’s work ! more about is here

      轉(zhuǎn)載請注明
      http://blog.csdn.net/liaokailin/article/details/51314001

      歡迎關(guān)注,您的肯定是對我最大的支持

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多