<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      Fork me on GitHub

      SpringCloud系列使用Eureka進行服務治理

      1. 什么是微服務?

      “微服務”一詞來自國外的一篇博文,網站:https://martinfowler.com/articles/microservices.html
      在這里插入圖片描述
      如果您不能看懂英文文檔,可以跳轉到搜簡體中文的文檔
      在這里插入圖片描述
      這是國人翻譯的文檔,可以學習參考:
      在這里插入圖片描述

      引用官方文檔解釋:

      簡單來說,微服務架構風格[1]是一種將一個單一應用程序開發為一組小型服務的方法,每個服務運行在自己的進程中,服務間通信采用輕量級通信機制(通常用HTTP資源API)。這些服務圍繞業務能力構建并且可通過全自動部署機制獨立部署。這些服務共用一個最小型的集中式的管理,服務可用不同的語言開發,使用不同的數據存儲技術。

      2. 什么是Spring Cloud?

      • Spring Cloud是一個分布式的整體解決方案的框架。基于Spring Boot開發。Spring Cloud 為開發者提供了在分布式系統(配置管理,服務發現,負載,網關,消息總線,集群管理,安全管理,分布式鎖,分布式事務等等)中快速構建的工具,使用Spring Cloud的開發者可以快速的啟動服務或構建應用、同時能夠快速和云平臺資源進行對接。

      3. 什么是Spring Cloud Eureka?

      • Spring Cloud Eureka 是 Spring Cloud Netflix 微服務套件的一部分,基于 Netflix Eureka 做了二次封裝,主要負責實現微服務架構中的服務治理功能。

      • Spring Cloud Eureka 是一個基于 REST 的服務,并且提供了基于 Java 的客戶端組件,能夠非常方便地將服務注冊到 Spring Cloud Eureka 中進行統一管理。

      4. Eureka服務注冊中心

      Eureka server:創建服務注冊中心

      環境準備:

      • JDK 1.8
      • SpringBoot2.2.3
      • SpringCloud(Hoxton.SR6)
      • Maven 3.2+
      • 開發工具
        • IntelliJ IDEA
        • smartGit

      創建一個SpringBoot Initialize項目,詳情可以參考我之前博客:SpringBoot系列之快速創建項目教程

      在這里插入圖片描述
      pom,加上spring-cloud-starter-netflix-eureka-server

      <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
              </dependency>
      

      @EnableEurekaServer配置Eureka服務端:

      package com.example.springcloud.server;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
      
      @SpringBootApplication
      @EnableEurekaServer
      public class SpringcloudEurekaServerApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
          }
      
      }
      
      

      eureka服務端配置:

      spring:
        application:
          name: eurka-server
      server:
        port: 8761
      eureka:
        instance:
          hostname: localhost
        client:
          register-with-eureka: false
          fetch-registry: false
          service-url:
            defaultZone: http://localhost:8761/eureka/
      

      在這里插入圖片描述
      啟動項目,訪問:http://localhost:8761

      在這里插入圖片描述

      5. Eureka服務提供者

      在Eureka中,服務提供者和服務消費者是Eureka client提供的,使用注解@EnableEurekaClient標明

      新建SpringBoot Initializer項目
      在這里插入圖片描述

      server:
        port: 8081
      spring:
        application:
          name: eureka-service-provider
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka/
          register-with-eureka: true
          fetch-registry: true
          healthcheck:
            enabled: false
        instance:
          status-page-url-path: http://localhost:8761/actuator/info
          health-check-url-path: http://localhost:8761/actuator//health
          prefer-ip-address: true
          instance-id: eureka-service-provider8081
      
      

      在這里插入圖片描述
      寫個例子,以github用戶為例:

      package com.example.springcloud.provider.bean;
      
      import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
      import lombok.Data;
      
      import java.io.Serializable;
      
      /**
       * <pre>
       *  Github User
       * </pre>
       *
       * <pre>
       * @author mazq
       * 修改記錄
       *    修改后版本:     修改人:  修改日期: 2020/07/27 17:38  修改內容:
       * </pre>
       */
      @JsonIgnoreProperties(ignoreUnknown = true)
      @Data
      public class User implements Serializable {
      
          private String name;
          private String blog;
      
          @Override
          public String toString() {
              return "User{" +
                      "name='" + name + '\'' +
                      ", blog='" + blog + '\'' +
                      '}';
          }
      }
      

      讀取github用戶信息:

      package com.example.springcloud.provider.service;
      
      import com.example.springcloud.provider.bean.User;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.boot.web.client.RestTemplateBuilder;
      import org.springframework.stereotype.Service;
      import org.springframework.web.client.RestTemplate;
      
      /**
       * <pre>
       *  UserService
       * </pre>
       *
       * <pre>
       * @author mazq
       * 修改記錄
       *    修改后版本:     修改人:  修改日期: 2020/07/27 17:42  修改內容:
       * </pre>
       */
      @Service
      @Slf4j
      public class UserService {
      
          private final RestTemplate restTemplate;
      
          public UserService(RestTemplateBuilder restTemplateBuilder) {
              this.restTemplate = restTemplateBuilder.build();
          }
      
      
          public User findUser(String user) throws InterruptedException {
              log.info("username[{}]" , user);
              String url = String.format("https://api.github.com/users/%s", user);
              User results = restTemplate.getForObject(url, User.class);
              return results;
          }
      }
      
      

      @EnableEurekaClient指定eureka client:

      package com.example.springcloud.provider;
      
      import com.example.springcloud.provider.bean.User;
      import com.example.springcloud.provider.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.ResponseBody;
      import org.springframework.web.bind.annotation.RestController;
      
      @SpringBootApplication
      @EnableEurekaClient
      @RestController
      public class SpringcloudServiceProviderApplication {
      
          @Autowired
          UserService userService;
      
          public static void main(String[] args) {
              SpringApplication.run(SpringcloudServiceProviderApplication.class, args);
          }
      
          @GetMapping({"/api/users/{username}"})
          @ResponseBody
          public User findUser(@PathVariable("username")String username) throws InterruptedException{
              User user=  userService.findUser(username);
              return user;
          }
      }
      
      

      部署后,注冊信息發布到eureka server:
      在這里插入圖片描述
      服務注冊信息打印到控制臺
      在這里插入圖片描述
      訪問接口:http://localhost:8081/api/users/mojombo,能訪問就是注冊成功

      提示:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
      在這里插入圖片描述

      SpringCloud警告(Eureka):EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
      警告!Eureka可能存在維護了錯誤的實例列表(當它們沒有啟動的時候,Eureka卻把它當成啟動的了);Renews值小于Threshold值,因此剩下未過期的都是安全的。

      方法:參考博客http://www.rzrgm.cn/gudi/p/8645370.html,可以關了eureka的自我保護模式eureka.server.enableSelfPreservation=false

      eureka也引用SpringBoot actuator監控管理,SpringBoot actuator可以參考我之前博客: SpringBoot系列之actuator監控管理極速入門與實踐
      在這里插入圖片描述
      具體可以進行配置,配置成eureka server的ip,加上actuator

      eureka:
        instance:
          status-page-url-path: http://localhost:8761/actuator/info
          health-check-url-path: http://localhost:8761/actuator/health
      
      

      顯示ip和實例id配置:
      在這里插入圖片描述
      在這里插入圖片描述

      6. Eureka服務消費者

      Spring cloud有兩種最常用的服務調用方式,一種是ribbon+restTemplate,另一種是feign,所以本博客以學習為目的,簡單介紹一下ribbon+restTemplate的方式,之后有時間再介紹feign的方式

      ribbon+resttemplate方式,Spring Cloud Hoxton.SR6版本不需要引入spring-cloud-starter-netflix-ribbon,已經默認集成
      在這里插入圖片描述
      引入web既可,主要是想調restTemplate

       <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      

      yaml配置:

      server:
        port: 8082
      spring:
        application:
          name: eureka-service-consumer
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka/
          fetch-registry: true
          register-with-eureka: false
          healthcheck:
            enabled: false
        instance:
          status-page-url-path: http://localhost:8761/actuator/info
          health-check-url-path: http://localhost:8761/actuator//health
          prefer-ip-address: true
          instance-id: eureka-service-consumer8082
      
      

      在這里插入圖片描述
      關鍵點,使用SpringCloud的@LoadBalanced,才能調http://EUREKA-SERVICE-PROVIDER/api/users/? 接口的數據,瀏覽器是不能直接調的

      package com.example.springcloud.consumer;
      
      import com.example.springcloud.consumer.bean.User;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.RestTemplate;
      
      @SpringBootApplication
      @EnableEurekaClient
      @RestController
      public class SpringcloudServiceConsumerApplication {
      
          @Bean
          @LoadBalanced
          public RestTemplate restTemplate(){
              return new RestTemplate();
          }
      
          public static void main(String[] args) {
              SpringApplication.run(SpringcloudServiceConsumerApplication.class, args);
          }
      
          @GetMapping("/findUser/{username}")
          public User index(@PathVariable("username")String username){
              return restTemplate().getForObject("http://EUREKA-SERVICE-PROVIDER/api/users/"+username,User.class);
          }
      }
      
      
      

      附錄:

      ok,本博客參考官方教程進行實踐,僅僅作為入門的學習參考資料,詳情可以參考Spring Cloud官方文檔https://cloud.spring.io/spring-cloud-netflix/reference/html/#registering-with-eureka

      代碼例子下載:code download

      優質學習資料參考:

      posted @ 2020-07-27 21:21  smileNicky  閱讀(578)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产成人亚洲日韩欧美| 国产超高清麻豆精品传媒麻豆精品| 国产成人高清亚洲综合| 久久精品国产亚洲成人av| 亚洲AV日韩AV综合在线观看| 亚洲欧洲日韩精品在线| 青青草成人免费自拍视频| 翘臀少妇被扒开屁股日出水爆乳 | 亚洲午夜福利AV一区二区无码| 亚洲a成人片在线观看| 国产成人无码区免费内射一片色欲| 日韩人妻精品中文字幕专区| 国产乱码精品一品二品 | 麻豆一区二区三区蜜桃免费| 黑人玩弄人妻中文在线| 久青草视频在线免费观看| 91亚洲国产成人久久精品| 小婕子伦流澡到高潮h| 在线成人| AV极品无码专区亚洲AV| 奇米四色7777中文字幕| 91精品91久久久久久| 疯狂做受XXXX高潮国产| 久久国产欧美日韩精品图片| 日韩亚洲国产中文永久| 亚洲AV国产福利精品在现观看| 少妇人妻精品一区二区| 日本真人做爰免费的视频| 国产精品白嫩初高生免费视频| 四川丰满少妇无套内谢| 91亚洲国产成人精品性色| 无码人妻aⅴ一区二区三区69岛| 一区二区三区午夜福利院| 国产精品天天看天天狠| 国产va在线观看免费| 中文字幕乱码在线播放| 久久这里有精品国产电影网| 国产午夜影视大全免费观看| 国产一区二区视频在线看| h动态图男女啪啪27报gif| 成年午夜无码av片在线观看|