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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud构建微服务

發布時間:2024/4/14 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud构建微服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Spring Cloud構建微服務 博客分類: 微服務 spring boot 架構

首先了解下項目結構

請忽略config-service,這里先不說這個

pom.xml配置

<!--這里只寫出比較重要的配置,模組依賴的配置這里就不寫了,不明白的可以先看看maven相關知識--> <dependencies><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Brixton.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>

discovery-service

discovery-service目錄結構如下,主要就是一個Java類,外加兩個yml配置文件

DiscoveryApplication.java 內容如下 注解SpringBootApplication是@Configuration @EnableAutoConfiguration @ComponentScan三個注解的集合 注解EnableEurekaServer表明這是一個Eureka Server,用于服務的注冊

@SpringBootApplication @EnableEurekaServer public class DiscoveryApplication {public static void main(String[] args) {SpringApplication.run(DiscoveryApplication.class,args);} }

bootstrap.yml配置如下,這里只配置了服務的名字叫 discovery

spring:application:name: discovery application.yml配置如下 server.port配置了tomcat的啟動端口,eureka實例的名字,以及eureka其他配置。因為我們這里是eureka服務端, register-with-eureka配置為false,這個配置表示是否將其本身注冊到eureka server以被其他發現 fetch-registry配置為false,這個配置表示是否需要從eureka server中抓取eureka上的注冊信息 defaultZone 默認地址為 http://localhost:8761/eureak server:port: 8761eureka:instance:hostname: discoveryclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://discovery:${server.port}/eureka/spring.cloud.config.discovery.enabled: true

category-service

category-service 目錄結構大致如下,這里我們主要說明下CategoryController.java、CategoryApplication.java、 application.yml、bootstrap.yml的內容。

CategoryController.java內容如下,主要就是對外提供服務,通過訪問地址 /v1/category @RestController @RequestMapping("/v1") public class CategoryController {private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);@AutowiredCategoryService categoryService;@RequestMapping("/category")public List<Category> getCategory() {LOGGER.info("哇塞~鳥人哎哎哎哎");return categoryService.getCategory();} } CategoryApplication.java主要內容如下,最重要的就是@EnableEurekaClient表示這是個eureka client,這個應用 將會被注冊到eureka server,這個應用的端口是什么?注冊到哪個eureka server?帶著這些疑問,待會我們看application.yml @SpringBootApplication @EnableEurekaClient public class CategoryApplication {public static void main(String[] args) {SpringApplication.run(CategoryApplication.class,args);} } application.yml如下,server.port配置的端口為0表明啟動這個項目之后,會自動為其分配一個可用端口,如果我們把這個應用 打成一個可執行jar包,在不重新指定端口的情況下,只會有一個程序能正常工作哦。 defaultZone 指明了該服務注冊的地址,服務將會被注冊到這個地址上 ribbon.eureka.enabled 默認情況即為true,這里不配置也無所謂 server:port: 0eureka:client:serviceUrl:defaultZone: http://discovery:8761/eureka/ instance:preferIpAddress: trueribbon:eureka:enabled: true bootstrap.yml配置如下,這里配置了服務的名字叫catelog-service,其他啥都沒有哦 spring:application:name: catalog-service

gateway-service

gateway-service項目結構如下,gateway主要是為了統一暴露接口而生,服務眾多的情況下,對前端來講,我不需要記住那么多的域名 地址來調用API,我需要記住的只是gateway的地址就行。

Application.java內容如下,最主要的其實就是@EnableSidecar,這個東西他提供了一個jsp頁面。通過這個頁面我們可以知道 gateway以及其他在eureka server上注冊的服務的健康狀況,并且這個注解包含了@EnableZuulProxy,所以呢,它也支持軟 負載均衡,如果啟動多個服務,通過gateway來調用這個接口,多次調用我們會發現,請求會落在不同的服務上 @SpringBootApplication @EnableSidecar public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} } application.yml如下,里面主要多了一個endpoints配置和sidecar的端口配置,endpoints是為了監控系統才有的配置, 具體可以參看AbstractEndpoint.java的實現類。sidecar的端口這里配置的是80,其他端口也是可以的 bootstrap.yml就不提了,因為里面只有服務的名字,和上面的類似 server:port: 10000endpoints:restart:enabled: trueshutdown:enabled: truehealth:sensitive: falseeureka:instance:hostname: gatewayclient:registerWithEureka: truefetchRegistry: trueserviceUrl:defaultZone: http://discovery:8761/eureka/ sidecar:port: 80

如何查看服務的健康狀況?

答: 就本例來說,我們訪問地址http://gateway:10000/來了解gateway的情況,如果要知道catelog-service的情況我們就訪問http://gateway:10000/hosts/catalog-service地址。hosts后面跟我們的服務的名字即可

spring boot如何創建一個可執行的jar包

答:在服務端的pom文件中增加插件spring-boot-maven-plugin。參考文檔http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

如何通過gateway來訪問其他服務

答:舉例,如果我們要通過gateway來訪問catalog-service服務,那么我在瀏覽器里面輸入http://gateway:10000/catalog-service/v1/category這里的/v1/category是由catalog-servicel來決定的,catalog-service則是服務的名字

PS: 一些截圖?

參考文檔

?

【1】http://jinnianshilongnian.iteye.com/blog/1902886
【2】http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

?

?

http://my.oschina.net/openforce/blog/680941

轉載于:https://my.oschina.net/xiaominmin/blog/1599067

總結

以上是生活随笔為你收集整理的Spring Cloud构建微服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。