• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • 【微服務】Springcloud學習筆記(一) —— Eureka

    SpringCloud基本特性

    • 分布式配置中心

    • 服務注冊/發現(Eureka)

    • 智能路由(Zuul)

    • 服務間的調用

    • 客戶端負載均衡(Ribbon)

    • 斷路器(Hystrix)

    • 分布式消息管理


    demo結構

    這里寫圖片描述

    服務發現:Eureka客戶端

    Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others

    Eureka是Netflix服務發現的一種服務和客戶端。這種服務是可以被高可用性配置的和部署,并且在注冊的服務當中,每個服務的狀態可以互相復制給彼此

    開啟Eureka

    @EnableEurekaServer //注冊服務器中心
    @SpringBootApplication
    public class SpringcloudDemoApplication {
     public static void main(String[] args) {
       SpringApplication.run(SpringcloudDemoApplication.class, args);
     }
    }

    application.properties

    #SpringCloud - Eureka Configuration
    server.port=1111
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    # "defaultZone" is a magic string fallback value that provides the service URL for any client that doesn’t express a preference
    # "defaultZone"是一個神奇的字符串回退值,它提供了服務的URL給任何客戶端,而這不意味優先級
    eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

    訪問

    http://localhost:1111/

    這里寫圖片描述


    注冊到Eureka

    Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.

    Eureka通過一個服務從各個實例接收心跳信息。如果心跳接收失敗超過配置的時間,實例將會正常從注冊里面移除。

    @EnableEurekaServer //注冊服務器中心
    @EnableDiscoveryClient //**Eureka中的DiscoveryClient實現,才能實現Controller中對服務信息的輸出。
    @SpringBootApplication
    public class SpringcloudServiceApplication {
    
        public static void main(String[] args) {
        SpringApplication.run(SpringcloudServiceApplication.class, args);
        }
    }

    @RestController
    public class ComputeController {
    
        private final Logger logger = Logger.getLogger(getClass());
    
        /**通過DiscoveryClient對象,在日志中打印出服務實例的相關內容*/
        @Autowired
        private DiscoveryClient client;
    
        @RequestMapping(value = "/add" ,method = RequestMethod.GET)
        public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
            ServiceInstance instance = client.getLocalServiceInstance();
            Integer r = a + b;
            logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
            return r;
        }
    
    }

    spring.application.name=compute-service
    server.port=2222
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

    訪問

    http://localhost:2222/add?a=1&b=2

    這里寫圖片描述這里寫圖片描述


    • @EnableEurekaClient
      • 聲明Eureka注冊服務(etc.實例的行為由eureka.instance.*的配置項來決定,但是你最好確保你的spring.application.name有個默認值)
    • @EnableDiscoveryClient
      • 定位發現Eureka客戶端

    eureka 客戶端官方例子

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    @EnableEurekaClient
    @RestController
    public class Application {
    
        @RequestMapping("/")
        public String home() {
            return "Hello world";
        }
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(true).run(args);
        }
    
    }

    更多配置請查看 EurekaInstanceConfigBeanEurekaClientConfigBean


    Eureka服務的身份驗證

    HTTP basic authentication will be automatically added to your eureka client if one of the eureka.client.serviceUrl.defaultZone URLs has credentials embedded in it

    如果其中一個eureka.client.serviceUrl.defaultZone的url已經把憑證嵌入到它里面,那么HTTP基本的身份驗證將會被自動添加到你的eureka客戶端

    For more complex needs you can create a @Bean of type DiscoveryClientOptionalArgs and inject ClientFilter instances into it, all of which will be applied to the calls from the client to the server

    對于更復雜的需求,您可以創建一個帶“@Bean”注解的“DiscoveryClientOptionalArgs”類型并且為它注入“ClientFilter”實例

    **由于Eureka的限制,Eureka不支持單節點身份驗證。

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

    #security.basic.enabled=true
    #fail
    #user.name=admin    
    #user.password=123

    這里寫圖片描述


    健康指標和狀態頁面

    Springboot監控
    Springboot-admin

    官網

    eureka.instance.statusPageUrlPath=${management.context-path}/info
    eureka.instance.healthCheckUrlPath=${management.context-path}/health


            <!-- Springboot監控 -->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-server</artifactId>
                 <version>1.4.2</version>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-server-ui</artifactId>
                <version>1.4.2</version>
            </dependency>

    @EnableAdminServer //開啟服務監控

    spring.boot.admin.url=http://localhost:${server.port}
    spring.jackson.serialization.indent_output=true
    endpoints.health.sensitive=false

    
    

    http://localhost:1111/metrics
    http://localhost:1111/health

    springcloud中文文檔

    版權聲明:本文為qq_20597479原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/qq_20597479/article/details/71123995

    智能推薦

    SpringCloud學習筆記(二)Eureka服務注冊

    上節學習了Eureka進行服務治理未寫完,這次將服務進行注冊到Eureka中,在上次的架子上繼續前進。   yyc-registry 注冊中心詳見:SpringCloud學習筆記-Eureka服務治理   首先新建一個yyc-test模塊,進行服務提供,使注冊到Eureka的注冊中心, 在pom文件中我們引入yyc父類,并引入web模塊   記得,在父類yy...

    SpringCloud學習筆記——服務注冊中心——Eureka

    SpringCloud學習筆記——服務注冊中心——Eureka 參考:尚硅谷2020最新版SpringCloud(H版&alibaba)——周陽 1. Eureka 單機版 創建子模塊,子模塊名稱 cloud-eureka-server7001 pom依賴 創建application.yml 創建主啟動類EurekaM...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    猜你喜歡

    requests實現全自動PPT模板

    http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...

    Linux C系統編程-線程互斥鎖(四)

    互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...

    統計學習方法 - 樸素貝葉斯

    引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...

    styled-components —— React 中的 CSS 最佳實踐

    https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...

    精品国产乱码久久久久久蜜桃不卡