這篇文章將為大家詳細(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)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行未央網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開發(fā)工具,它為基于JVM的云應(yīng)用開發(fā)中的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競(jìng)選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡單的開發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開源產(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ù);
本例子為你提供了最簡單的一個(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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.caicongyang</groupId> <artifactId>spingCloudEurekaServer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR6</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.caicongyang</groupId> <artifactId>spingCloudConfServer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR6</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- sping cloud 注冊(cè)服務(wù) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <defaultGoal>compile</defaultGoal> </build> </project>
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.caicongyang</groupId> <artifactId>springCloudApp</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR6</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> <java.encoding>UTF-8</java.encoding> <springfox.swagger.version>2.2.2</springfox.swagger.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- sping cloud 監(jiān)控 http://localhost:8080/health --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- sping cloud 注冊(cè)服務(wù) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- sping cloud 路由 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.swagger.version}</version> </dependency> </dependencies> <build> <finalName>spingcould</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${java.encoding}</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> </build> </project>
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è)簡單的應(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.訪問http://localhost:9999/eureka/ app是否已經(jīng)注冊(cè)上來
2.訪問 http://localhost:8080/swagger-ui.html 是否正常訪問,如果正常訪問說明爭(zhēng)取讀取到config配置中心的swagger.ui.enable配置項(xiàng)
關(guān)于如何使用SpringCloud搭建一個(gè)微服務(wù)框架就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前文章:如何使用SpringCloud搭建一個(gè)微服務(wù)框架
文章鏈接:http://bm7419.com/article22/pciscc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)、網(wǎng)站排名、自適應(yīng)網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)