這篇文章將為大家詳細(xì)講解有關(guān)如何使用SpringCloud搭建一個(gè)微服務(wù)框架,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)是專業(yè)的未央網(wǎng)站建設(shè)公司,未央接單;提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行未央網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開(kāi)發(fā)工具,它為基于JVM的云應(yīng)用開(kāi)發(fā)中的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競(jìng)選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開(kāi)發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開(kāi)源產(chǎn)品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項(xiàng)目。
1.微服務(wù)
微服務(wù)主要包含服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn),服務(wù)路由,服務(wù)配置,服務(wù)熔斷,服務(wù)降級(jí)等一系列的服務(wù),而Spring Cloud為我們提供了個(gè)一整套的服務(wù);
本例子為你提供了最簡(jiǎn)單的一個(gè)服務(wù)發(fā)現(xiàn)例子,包含服務(wù)注冊(cè)發(fā)現(xiàn)spingCloudEurekaServer、服務(wù)配置中心spingCloudConfServer、以及一個(gè)app應(yīng)用springCloudApp
2.服務(wù)注冊(cè)與發(fā)現(xiàn)
spingCloudEurekaServer
pom.xml
4.0.0 com.caicongyang spingCloudEurekaServer 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-maven-plugin
Application.java
package com.caicongyang.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Spring could EurekaServer程序主入口 * * @author Administrator * */ @SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
application.yml (可用properties替代)
server: port: 9999 eureka: instance: hostname: 127.0.0.1 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.服務(wù)配置(全局配置中心)
pom.xml
4.0.0 com.caicongyang spingCloudConfServer 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin compile
application.java
package com.caiconyang.conf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * Spring could conf程序主入口 * @author Administrator * */ @SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
application.properties
server.port=8888 ## App配置文件所在git地址 spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springCloudConfigRepo.git spring.cloud.config.server.git.searchPaths=repo spring.application.name=spingCloudConfServer
4.App
pom.xml
4.0.0 com.caicongyang springCloudApp 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 UTF-8 1.7 UTF-8 2.2.2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-hystrix org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 ${springfox.swagger.version} io.springfox springfox-swagger-ui ${springfox.swagger.version} spingcould org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.encoding} true
Application.java
package com.caicongyang.springCloudApp.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * Spring could web程序主入口 * @author Administrator * */ @Configuration//配置控制 @EnableAutoConfiguration//啟用自動(dòng)配置 @ComponentScan(value={"com.caicongyang.springCloudApp"})//組件掃描 @EnableDiscoveryClient public class Application { public static void main(String[] args) { //第一個(gè)簡(jiǎn)單的應(yīng)用, SpringApplication.run(Application.class,args); } }
SwaggerConfig.java
package com.caicongyang.springCloudApp.conf; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * * @author caicongyang1 * @version id: SwaggerConfig, v 0.1 16/4/22 下午4:12 caicongyang1 Exp $$ */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Value("${swagger.ui.enable}") //該配置項(xiàng)在配置中心管理 private boolean environmentSpecificBooleanFlag; @Bean public Docket docketFactory() { return new Docket(DocumentationType.SWAGGER_2).apiInfo( new ApiInfo("接口文檔", "SpingCloud web接口列表", "1.0", "", "", "", "")).enable(environmentSpecificBooleanFlag); } }
application.properties
server.port=8080 spring.cloud.config.uri=http://127.0.0.1:8888 spring.cloud.config.name=springCloudApp spring.cloud.config.profile=${config.profile:dev} #service discovery url eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/ #service name spring.application.name=springCloudApp
5.測(cè)試與驗(yàn)證
順序啟動(dòng)服務(wù)注冊(cè)發(fā)現(xiàn)spingCloudEurekaServer、服務(wù)配置中心spingCloudConfServer、以及一個(gè)app應(yīng)用springCloudApp
測(cè)試與驗(yàn)證
1.訪問(wèn)http://localhost:9999/eureka/ app是否已經(jīng)注冊(cè)上來(lái)
2.訪問(wèn) http://localhost:8080/swagger-ui.html 是否正常訪問(wèn),如果正常訪問(wèn)說(shuō)明爭(zhēng)取讀取到config配置中心的swagger.ui.enable配置項(xiàng)
關(guān)于如何使用SpringCloud搭建一個(gè)微服務(wù)框架就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。