日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringCloud的五大核心组件李俊老师

發(fā)布時(shí)間:2024/3/12 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud的五大核心组件李俊老师 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 樂優(yōu)項(xiàng)目SpringCloud筆記總結(jié)
    • 注冊(cè)中心Eureka,ly-registry服務(wù)端
    • Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用
    • Ribbon負(fù)載均衡
    • Hystrix熔斷器
    • zuul網(wǎng)關(guān)

樂優(yōu)項(xiàng)目SpringCloud筆記總結(jié)

ly-item 商品的的mapper模塊,里面有datasource

ly-cart: 購(gòu)物車模塊

ly-registry: 注冊(cè)中心服務(wù)端,其他的模塊都是客戶端

ly-api-gateway: 路由網(wǎng)關(guān)

ly-feign:遠(yuǎn)程調(diào)用

注冊(cè)中心Eureka,ly-registry服務(wù)端

lyregistry這個(gè)Module作為服務(wù)端

服務(wù)端可以進(jìn)行配置

server:port: 10086spring:application:name: ly-registryeureka:client:fetch-registry: falseregister-with-eureka: falseservice-url:defaultZone: http://localhost:${server.port}/eurekaserver:enable-self-preservation: false #關(guān)閉自我保護(hù)eviction-interval-timer-in-ms: 5000 #每隔5秒進(jìn)行一次服務(wù)列表清理 package com.yunhe;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class LyRegistry {public static void main(String[] args) {SpringApplication.run(LyRegistry.class,args);} }

使用ly-user作為客戶端進(jìn)行相關(guān)的測(cè)試:

package com.yunhe;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //開啟feign功能 public class LyUserApplication {public static void main(String[] args) {SpringApplication.run(LyUserApplication.class,args);} }

加入相關(guān)的客戶端實(shí)現(xiàn)的依賴:

<!--eureka client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用 ly-item--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0</version></dependency>

客戶端的配置

server:port: 10010spring:application:name: item-servicedatasource:url: jdbc:mysql://localhost:3306/leyou?characterEncoding=UTF-8&useSSL=falseusername: rootpassword: roothouzhiconghikari:maximum-pool-size: 30minimum-idle: 10eureka:client:service-url:defaultZone: http://127.0.0.1:10086/eurekainstance:lease-renewal-interval-in-seconds: 5 #每隔6秒發(fā)送一次心跳lease-expiration-duration-in-seconds: 10 #10秒不發(fā)送就過(guò)期prefer-ip-address: trueip-address: 127.0.0.1instance-id: ${spring.application.name}:${server.port}# 對(duì)Ribbon進(jìn)行配置 item-service: #feign里面負(fù)載均衡配置ribbon:ConnectTimeout: 250 # Ribbon的連接超時(shí)時(shí)間ReadTimeout: 3000 # Ribbon的數(shù)據(jù)讀取超時(shí)時(shí)間OkToRetryOnAllOperations: true # 是否對(duì)所有操作都進(jìn)行重試MaxAutoRetriesNextServer: 1 # 切換實(shí)例的重試次數(shù)MaxAutoRetries: 1 # 對(duì)當(dāng)前實(shí)例的重試次數(shù)feign:hystrix:enabled: true # 開啟Feign的熔斷功能,默認(rèn)為關(guān)閉

Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用

ly-user這個(gè)模塊調(diào)用ly-item

  • 在ly-user的pom.xml文件加入feign的依賴

  • 在ly-user的LyApplication 啟動(dòng)類上加入**@EnableFeignClients**注解

  • 在client客戶端下面創(chuàng)建一個(gè)itemClient接口

  • @RequestMapping("brand")
  • 加上這個(gè)前端要用的請(qǐng)求的路徑。

  • 加入Service這個(gè)接口中的定義的相關(guān)的方法。
  • 前面加上**@FeignClient(name=“item-service”)** (item-service是這個(gè)ly-item這個(gè)模塊里面的spring.application.name=item-service)
  • 由于還沒有進(jìn)行優(yōu)化,我們可以先把pojo類全部復(fù)制粘貼過(guò)來(lái)
  • 建立一個(gè)BrandController進(jìn)行前端的測(cè)試輸入這個(gè)測(cè)試類的請(qǐng)求就好
  • itemClient:

    package com.yunhe.client;import com.yunhe.pojo.Brand; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*;import java.util.List;//注意:類上面不要寫@RequestMapping @FeignClient(name = "item-service",fallback = ItemClientFallback.class) public interface ItemClient {@GetMapping("brand")public List<Brand> listAll();} @RestController public class UserController {@Autowiredprivate ItemClient itemClient;@GetMappingpublic List<Brand> queryAllBrands(){return itemClient.listAll();} }

    Ribbon負(fù)載均衡

  • 可以知道feignEureka,zuul中已經(jīng)自帶了ribbon和Hystrix,所以不用引入依賴.
  • Hystrix熔斷器

    feign里面自帶熔斷器,但是默認(rèn)是關(guān)閉狀態(tài)的,需要手動(dòng)開啟

    使ly-user模塊調(diào)用ly-item模塊,需要feign和hystrix,進(jìn)行hystrix步驟:

  • yml文件加上:
  • feign:hystrix:enabled: true #開啟熔斷器
  • 創(chuàng)建client包然后進(jìn)行創(chuàng)建下面的類: //注意:類上面不要寫@RequestMapping ,這里的@GetMapping(“brand”)和ly-item里面的controller測(cè)試的請(qǐng)求一樣。
  • package com.yunhe.client;import com.yunhe.pojo.Brand; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*;import java.util.List;//注意:類上面不要寫@RequestMapping @FeignClient(name = "item-service",fallback = ItemClientFallback.class) public interface ItemClient {@GetMapping("brand")public List<Brand> listAll();}
  • 創(chuàng)建 ItemClientFallback實(shí)現(xiàn)類: ly-item的服務(wù)斷了不會(huì)影響ItemClientFallback方法的執(zhí)行
  • package com.yunhe.client;import com.yunhe.client.ItemClient; import com.yunhe.pojo.Brand; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Component public class ItemClientFallback implements ItemClient {@Override@GetMapping("brand")public List<Brand> listAll() {System.out.println("服務(wù)斷了,嗚嗚嗚嗚。。。");return null;} }

    zuul網(wǎng)關(guān)

  • 創(chuàng)建一個(gè)單獨(dú)的模塊ly-api-gateway,所有的端口號(hào)會(huì)經(jīng)過(guò)zuul處理。
  • 當(dāng)然其他的所有模塊都是注冊(cè)中心的客戶端,需要加上**@EnableDiscoveryClient,在主配置文件上面**
  • **引入網(wǎng)關(guān)的依賴,**也需要引入eureka客戶端的依賴
  • <!--zuul網(wǎng)關(guān)--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId><version>2.0.0.RELEASE</version></dependency><!--eureka client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
  • 啟動(dòng)類上面 加上**@EnableZuulProxy** 網(wǎng)關(guān)代理注解

  • 在ly-api-gateway的application.yml文件中配置網(wǎng)關(guān),注意一下:routes 下面的 item-service是配置文件中的spring.application.name 的名字

  • zuul:prefix: /api #添加路由前綴retryable: trueroutes:item-service: /item/** #商品服務(wù)user-service: /user/** #用戶微服務(wù)cart-service: /cart/** #購(gòu)物車服務(wù)
  • 網(wǎng)關(guān)的所有的配置文件信息
  • server:port: 10000spring:application:name: api-gatewayeureka:client:service-url:defaultZone: http://127.0.0.1:10086/eurekainstance:lease-renewal-interval-in-seconds: 5 #每隔6秒發(fā)送一次心跳lease-expiration-duration-in-seconds: 10 #10秒不發(fā)送就過(guò)期prefer-ip-address: trueip-address: 127.0.0.1instance-id: ${spring.application.name}:${server.port} zuul:prefix: /api # 添加路由前綴retryable: true #當(dāng)網(wǎng)關(guān)沒有連接上來(lái)的時(shí)候是否可以重試routes:item-service: /item/** #商品微服務(wù) /item下的所有的請(qǐng)求user-service: /user/** #用戶微服務(wù)cart-service: /cart/** #購(gòu)物車微服務(wù)ribbon:ConnectTimeout: 60000 # 連接超時(shí)時(shí)間(ms)ReadTimeout: 60000 # 通信超時(shí)時(shí)間(ms)OkToRetryOnAllOperations: true # 是否對(duì)所有操作重試MaxAutoRetriesNextServer: 1 # 同一服務(wù)不同實(shí)例的重試次數(shù)MaxAutoRetries: 1 # 同一實(shí)例的重試次數(shù) hystrix:command:default:execution:isolation:thread:timeoutInMillisecond: 10000 # 熔斷超時(shí)時(shí)長(zhǎng):10000ms
  • 通過(guò)網(wǎng)關(guān)進(jìn)行訪問ly-item里面的增刪改查的操作。

    http://localhost:10000/api/item/brand 和 不用網(wǎng)關(guān)訪問的效果一樣 http://localhost:10010/brand

  • zuul自帶hystrix和Ribbon功能,需要我們通過(guò)配置文件開啟

  • 網(wǎng)關(guān)可以進(jìn)行過(guò)濾的配置

  • package com.yunhe.filter;import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.exception.ZuulException; import org.springframework.stereotype.Component; //注意一下需要加上這個(gè)@Component組件 @Component public class LoginFilter extends ZuulFilter{/*** - pre:請(qǐng)求在被路由之前執(zhí)行* - routing:在路由請(qǐng)求時(shí)調(diào)用* - post:在routing和errror過(guò)濾器之后調(diào)用* - error:處理請(qǐng)求時(shí)發(fā)生錯(cuò)誤調(diào)用* @return*/@Overridepublic String filterType() {return "pre";}//數(shù)字越小優(yōu)先級(jí)越高@Overridepublic int filterOrder() {return 1;}//是否進(jìn)行過(guò)濾 true 過(guò)濾 false 不進(jìn)行過(guò)濾@Overridepublic boolean shouldFilter() {return true;}//run() 過(guò)濾的方法@Overridepublic Object run() throws ZuulException {System.out.println("zuul filter執(zhí)行了。。。。。");return null;} }

    總結(jié)

    以上是生活随笔為你收集整理的SpringCloud的五大核心组件李俊老师的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。