在idea環(huán)境下構(gòu)建springCloud項目
springCloud是基于springboot搭建的微服務(wù)。它利用Spring Boot的開發(fā)便利性巧妙地簡化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開發(fā),如服務(wù)發(fā)現(xiàn)注冊、配置中心、消息總線、負(fù)載均衡、斷路器、數(shù)據(jù)監(jiān)控等,都可以用Spring Boot的開發(fā)風(fēng)格做到一鍵啟動和部署。
spring cloud官方文檔:http://projects.spring.io/spring-cloud/
spring cloud 中文網(wǎng) : https://springcloud.cc/
最終搭建后的工程源代碼:https://github.com/onpwerb/SpringCloud
一、新建maven工程
根據(jù)spring cloud官方文檔,在pom.xml導(dǎo)入如下代碼
<!-- spring cloud 配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
二、建立注冊中心
新建名稱為 discovery 的 module
1.在該module下的pom.xml導(dǎo)入如下配置:
<!-- @EnableEurekaServer --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <!--<version>1.1.6.RELEASE</version>--> </dependency> </dependencies>
2.在src/main/java目錄下新建discovery文件夾,然后新建一個application
package discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class DiscoveryApplicaion { public static void main(String[] args) { SpringApplication.run(DiscoveryApplicaion.class, args); } }
3.在該module下的src/main/resources文件夾下,新建文件application.yml,配置注冊中心eureka的相關(guān)服務(wù)
server: port: 8081 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
三、構(gòu)建一個服務(wù)A
新建一個名為service的module
1.在src/main/java目錄下新建service文件夾,然后新建一個application
package service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ServiceApplication { @GetMapping("/service") public String service(){ return "service"; } public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } }
2.在該module下的src/main/resources文件夾下,新建文件application.yml
spring: application: name: service.service eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8082
四、構(gòu)建第二個服務(wù)B
新建一個名為service2的module
1.在src/main/java目錄下新建service2文件夾,然后新建一個application
package service2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class Service2Application { @RequestMapping("/service2") public String service2(){ return "service2"; } public static void main(String[] args) { SpringApplication.run(Service2Application.class, args); } }
2.在該module下的src/main/resources文件夾下,新建文件application.yml
spring: application: name: service2 eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8083
五、配置網(wǎng)關(guān)
新建名稱為 gateway 的 module
1.在該module下的pom.xml導(dǎo)入如下配置:
package gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
2.在src/main/java目錄下新建gateway文件夾,然后新建一個application
eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ spring: application: name: gateway server: port: 8084 zuul: routes: service: /service/** service2: /service2/**
3.在該module下的src/main/resources文件夾下,新建文件application.yml
六、啟動服務(wù)
先啟動discovery模塊,再啟動其他模塊
在瀏覽器依次輸入:
http://localhost:8081/
http://localhost:8082/service
http://localhost:8083/service2
http://localhost:8084/service/service
http://localhost:8084/service2/service2
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
查看SpringBoot和JDK版本對應(yīng)關(guān)系的方法
在進(jìn)行一些自主學(xué)習(xí)的時候,發(fā)現(xiàn)使用maven方式創(chuàng)建的SpringBoot項目啟動失敗,最終發(fā)現(xiàn)是SpringBoot版本和JDK版本不對應(yīng)導(dǎo)致的,所以本文就給大家介紹了如何查看SpringBoot和JDK版本的對應(yīng)關(guān)系,需要的朋友可以參考下2024-03-03Maven 版本管理與 flatten-maven-plugin 插件的使用解析
這篇文章主要介紹了Maven 版本管理與 flatten-maven-plugin 插件的使用解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07JDBC的基本操作與Statement和PreparedStateMent使用區(qū)別分析
這篇文章主要介紹了JDBC的基本操作與Statement和PreparedStateMent使用區(qū)別,Java Database Connectivity,它是代表一組獨立于任何數(shù)據(jù)庫管理系統(tǒng)(DBMS)的API,聲明在java.sql與javax.sql包中,是SUN(現(xiàn)在Oracle)提供的一組接口規(guī)范2023-04-04Jetty啟動項目中引用json-lib相關(guān)類庫報錯ClassNotFound的解決方案
今天小編就為大家分享一篇關(guān)于Jetty啟動項目中引用json-lib相關(guān)類庫報錯ClassNotFound的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階)
本篇文章主要介紹了詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Java中連接Mongodb進(jìn)行增刪改查的操作詳解
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,由C++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲解決方案,本文給大家介紹了Java中連接Mongodb進(jìn)行操作,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06