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

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

    • 分享

      Java springboot B2B2C o2o多用戶商城 springcloud架構(gòu):服務(wù)消費(fèi)(基礎(chǔ))

       昵稱61506847 2019-02-14

      使用LoadBalancerClient

      在Spring Cloud Commons中提供了大量的與服務(wù)治理相關(guān)的抽象接口,包括DiscoveryClient、這里我們即將介紹的LoadBalancerClient等。對于這些接口的定義我們在上一篇介紹服務(wù)注冊與發(fā)現(xiàn)時已經(jīng)說過,Spring Cloud做這一層抽象,很好的解耦了服務(wù)治理體系,使得我們可以輕易的替換不同的服務(wù)治理設(shè)施。

      LoadBalancerClient接口的命名中,我們就知道這是一個負(fù)載均衡客戶端的抽象定義,下面我們就看看如何使用Spring Cloud提供的負(fù)載均衡器客戶端接口來實(shí)現(xiàn)服務(wù)的消費(fèi)。

      下面的例子,我們將利用上一篇中構(gòu)建的eureka-server作為服務(wù)注冊中心、eureka-client作為服務(wù)提供者作為基礎(chǔ)。

       

      • 我們先來創(chuàng)建一個服務(wù)消費(fèi)者工程,命名為:eureka-consumer。并在pom.xml中引入依賴(這里省略了parent和dependencyManagement的配置):
      <dependencies>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <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>
      • 配置application.properties,指定eureka注冊中心的地址:
      spring.application.name=eureka-consumer
      server.port=2101

      eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
      • 創(chuàng)建應(yīng)用主類。初始化RestTemplate,用來真正發(fā)起REST請求。@EnableDiscoveryClient注解用來將當(dāng)前應(yīng)用加入到服務(wù)治理體系中。
      @EnableDiscoveryClient
      @SpringBootApplication
      public class Application {

      @Bean
      public RestTemplate restTemplate() {
      return new RestTemplate();
      }

      public static void main(String[] args) {
      new SpringApplicationBuilder(Application.class).web(true).run(args);
      }
      }
      • 創(chuàng)建一個接口用來消費(fèi)eureka-client提供的接口:
      @RestController
      public class DcController {

      @Autowired
      LoadBalancerClient loadBalancerClient;
      @Autowired
      RestTemplate restTemplate;

      @GetMapping("/consumer")
      public String dc() {
      ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
      String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
      System.out.println(url);
      return restTemplate.getForObject(url, String.class);
      }
      }

      可以看到這里,我們注入了LoadBalancerClientRestTemplate,并在/consumer接口的實(shí)現(xiàn)中,先通過loadBalancerClientchoose函數(shù)來負(fù)載均衡的選出一個eureka-client的服務(wù)實(shí)例,這個服務(wù)實(shí)例的基本信息存儲在ServiceInstance中,然后通過這些對象中的信息拼接出訪問/dc接口的詳細(xì)地址,最后再利用RestTemplate對象實(shí)現(xiàn)對服務(wù)提供者接口的調(diào)用。

      在完成了上面你的代碼編寫之后,讀者可以將eureka-server、eureka-client、eureka-consumer都啟動起來,然后訪問http://localhost:2101/consumer ,來跟蹤觀察eureka-consumer服務(wù)是如何消費(fèi)eureka-client服務(wù)的/dc接口的。

      consul版的示例,可查看git倉庫中的consul-client和consul-consumer

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多