欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringCloud學(xué)習(xí)(2)-創(chuàng)新互聯(lián)

先知

在這里插入圖片描述

桃江網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)

什么叫服務(wù)與注冊中心?
服務(wù)注冊中心是服務(wù)實現(xiàn)服務(wù)化管理的核心組件,類似于目錄服務(wù)的作用,主要用來存儲服務(wù)信息,譬如提供者 url 串、路由信息等。服務(wù)注冊中心是微服務(wù)架構(gòu)中最基礎(chǔ)的設(shè)施之一。注冊中心可以說是微服務(wù)架構(gòu)中的“通訊錄”,它記錄了服務(wù)和服務(wù)地址的映射關(guān)系。在分布式架構(gòu)中,服務(wù)會注冊到這里,當(dāng)服務(wù)需要調(diào)用其它服務(wù)時,就到這里找到服務(wù)的地址,進行調(diào)用。

使用Eureka
概念:

? Eureka 是 Netflix 公司開源的一個服務(wù)注冊與發(fā)現(xiàn)的組件 。

? Eureka 和其他 Netflix 公司的服務(wù)組件(例如負(fù)載均衡、熔斷器、網(wǎng)關(guān)等) 一起,被 Spring Cloud 社區(qū)整合為Spring-Cloud-Netflix 模塊。

? Eureka 包含兩個組件:Eureka Server (注冊中心) 和 Eureka Client (服務(wù)提供者、服務(wù)消費者)。

操作:

架構(gòu)圖一覽

在這里插入圖片描述

創(chuàng)建Serve端 新建項目

在這里插入圖片描述
在這里插入圖片描述

配置文件

在這里插入圖片描述

application.yaml

server:
  port: 8080

# Eureka配置
eureka:
  instance:
    ## Eureka實例的名稱
    hostname: localhostA
  client:
    # false表示自己端就是注冊中心,職責(zé)就是維護服務(wù)實例,并不需要去檢查服務(wù)
    fetch-registry: false
    # false表示不向注冊中心注冊自己
    register-with-eureka: false
    # 設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(wù)都需要依賴這個地址
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

啟動類配置
// 表示當(dāng)前類為服務(wù)端Eureka服務(wù)端
@EnableEurekaServer
@SpringBootApplication
public class CloudA1Application {public static void main(String[] args) {SpringApplication.run(CloudA1Application.class, args);
    }

}
啟動測試一下

訪問自己的
localhost:端口號
在這里插入圖片描述
一切正常再繼續(xù)

Eureka Client包括兩個服務(wù)模塊:Service Provider(服務(wù)提供方)和Service Consumer(服務(wù)消費方)。

創(chuàng)建Client端的服務(wù)提供端 新建項目

新增依賴
org.projectlomboklomboktruecom.alibabadruid1.1.16com.baomidoumybatis-plus-boot-starter3.4.2mysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-web
配置文件

在這里插入圖片描述

application.yaml

server:
  port: 8081
# Eureka配置
eureka:
   client:
       # 表示將自己注冊進Eureka Server默認(rèn)為true
     register-with-eureka: true
       # 是否從Eureka Server抓去已有的注冊信息,默認(rèn)是true
     fetch-registry: true
       # 設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(wù)都需要依賴這個地址
     service-url:
       defaultZone: http://localhost:8080/eureka
#數(shù)據(jù)庫配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: xxx
    type: com.alibaba.druid.pool.DruidDataSource
#  當(dāng)前服務(wù)注冊在Eureka Server的名稱
  application:
    name: server-provider1


#MP配置
mybatis-plus:
  #  配置外部xml映射
  configuration:
    #    開啟SQL日志輸出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #    開啟駝峰映射
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*.xml
啟動類配置
// 表示當(dāng)前類客戶端Eureka
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Application {public static void main(String[] args) {SpringApplication.run(CloudB1Application.class, args);
    }

}
編寫控制器類
package com.learn.cloudb1.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {@GetMapping("/get1")
    public String hello(){return "hello world!";
    }
}
啟動測試

訪問自己的 localhost:端口號(就是服務(wù)注冊中心)
可以發(fā)現(xiàn)多了些東西,一個報錯和剛剛注冊好的服務(wù)提供者

在這里插入圖片描述

創(chuàng)建Client端的服務(wù)消費端

創(chuàng)建方法和創(chuàng)建Client端的服務(wù)服務(wù)端一樣

配置文件
server:
  port: 8083

spring:
  application:
    name: service-customer1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka
啟動類
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Customer1Application {public static void main(String[] args) {SpringApplication.run(CloudB1Customer1Application.class, args);
    }
}
控制器
package com.learn.cloudb1_customer1.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/getMes")
public class GetMesController {//從Euraka Server中獲取服務(wù)提供方的服務(wù)地址信息
    @Autowired
    private DiscoveryClient ds;
    @GetMapping()
    public String getMes(){//服務(wù)提供者的名字
         //服務(wù)發(fā)現(xiàn)
        ListinstanceList = ds.getInstances("server-provider1");
        int port=0;
        String host="";
        //打印服務(wù)機器的信息
        for (ServiceInstance instance : instanceList) {//服務(wù)主機端口號
            port = instance.getPort();
            System.out.println("服務(wù)主機端口號:"+ port);
            //服務(wù)主機名字
            host = instance.getHost();
            System.out.println("服務(wù)主機名字:"+host);
        }
        return host+"++++"+port;
    }
}
啟動測試

發(fā)現(xiàn)又多了一個
在這里插入圖片描述

點擊這里再輸入自己定義的接口發(fā)現(xiàn)也可正常訪問
在這里插入圖片描述

在這里插入圖片描述

高可用

準(zhǔn)備兩個Eureka Server

分別進行配置,相互注冊

Eureka Client 分別注冊到這兩個 Eureka Server中

創(chuàng)建eureka-server1

server:
  port: 8761


eureka:
  instance:
    hostname: eureka-server1 # 主機名
  client:
    service-url:
      defaultZone: http://eureka-server2:8762/eureka
    register-with-eureka: true # 是否將自己的路徑 注冊到eureka上。eureka server 不需要的,eureka provider client 需要
    fetch-registry: true # 是否需要從eureka中抓取路徑。eureka server 不需要的,eureka consumer client 需要


spring:
  application:
    name: eureka-server-ha

創(chuàng)建eureka-server2

server:
  port: 8762


eureka:
  instance:
    hostname: eureka-server2 # 主機名
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka

    register-with-eureka: true # 是否將自己的路徑 注冊到eureka上。eureka server 不需要的,eureka provider client 需要
    fetch-registry: true # 是否需要從eureka中抓取路徑。eureka server 不需要的,eureka consumer client 需要
spring:
  application:
    name: eureka-server-ha

修改provider

server:
  port: 8001


eureka:
  instance:
    hostname: localhost # 主機名
    prefer-ip-address: true # 將當(dāng)前實例的ip注冊到eureka server 中。默認(rèn)是false 注冊主機名
    ip-address: 127.0.0.1 # 設(shè)置當(dāng)前實例的ip
    instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 設(shè)置web控制臺顯示的 實例id
    lease-renewal-interval-in-seconds: 3 # 每隔3 秒發(fā)一次心跳包
    lease-expiration-duration-in-seconds: 9 # 如果9秒沒有發(fā)心跳包,服務(wù)器呀,你把我干掉吧~
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
  application:
    name: eureka-provider # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑

修改consumer

server:
  port: 9000


eureka:
  instance:
    hostname: localhost # 主機名
  client:
    service-url:
      defaultZone:  http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka  # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
  application:
    name: eureka-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑

高可用測試:停掉一個eureka,依然可以訪問consumer。

Eureka配置詳情

實例信息配置
eureka:
instance:
hostname: localhost # 主機名
prefer-ip-address: # 是否將自己的ip注冊到eureka中,默認(rèn)false 注冊 主機名
ip-address: # 設(shè)置當(dāng)前實例ip
instance-id: # 修改instance-id顯示
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server發(fā)送心跳的時間間隔
lease-expiration-duration-in-seconds: 90 # 如果90秒內(nèi)eureka server沒有收到eureka client的心跳包,則剔除該服務(wù)
客戶端特性配置
eureka:
client:
service-url:
# eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
defaultZone:
register-with-eureka: # 是否將自己的路徑 注冊到eureka上。
fetch-registry: # 是否需要從eureka中抓取數(shù)據(jù)。
注冊中心端配置
eureka:
server: #是否開啟自我保護機制,默認(rèn)true
enable-self-preservation:
eviction-interval-timer-in-ms: 120 2月#清理間隔(單位毫秒,默認(rèn)是60*1000)
instance:
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server發(fā)送心跳的時間間隔
lease-expiration-duration-in-seconds: 90 # 如果90秒內(nèi)eureka server沒有收到eureka clien
儀表盤配置
eureka:
dashboard:
enabled: true # 是否啟用eureka web控制臺
path: / # 設(shè)置eureka web控制臺默認(rèn)訪問路徑

OK!入門結(jié)束

當(dāng)然常用的做法是將多個模塊/項目建立在一個maven父工程中,這樣更容易管理與使用

其他更詳細(xì)的適合入門的文章
https://www.cnblogs.com/h–d/p/12635204.html
https://blog.csdn.net/jc_hook/article/details/122413858

報錯解決方法
https://blog.csdn.net/hadues/article/details/105023709

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當(dāng)前題目:SpringCloud學(xué)習(xí)(2)-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article40/decpho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站網(wǎng)站收錄、手機網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)