使用Idea簡單快速搭建springcloud項目的圖文教程
前言:
開發(fā)工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服務(wù)系統(tǒng),實現(xiàn)服務(wù)之間的調(diào)用。
需要搭建一個父工程springcloud-test,一個服務(wù)注冊中心eureka-server,兩個微服務(wù)cloud-client,cloud-provider。
兩個微服務(wù)均注冊到服務(wù)注冊中心。
一.搭建父項目
2.
3.
(1)刪掉src目錄
(2)定義pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.chen.test</groupId> <artifactId>springcloud-test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> </project>
二.搭建eureka-server注冊中心
1.
2.
3.
4.
5.
6.
(1)定義pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--引入父工程依賴--> <parent> <groupId>com.chen.test</groupId> <artifactId>springcloud-test</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.chen.test</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>2020.0.0</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
(2)刪掉test文件夾(自己設(shè)置,可有可無)
(3)啟動加注解@EnableEurekaServer(開啟eureka服務(wù))
package com.chen.test.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
(4)定義配置文件
配置文件改為application.yml(可有可無,看自己喜好)
server: port: 8080 spring: application: #應(yīng)用名稱(在注冊中顯示的) name: eureka-server eureka: client: #此客戶端是否獲取eureka服務(wù)器注冊表上的注冊信息,默認為true fetch-registry: false #實例是否在eureka服務(wù)器上注冊自己的信息以供其他服務(wù)發(fā)現(xiàn),默認為true,即自己注冊自己。 register-with-eureka: true #與Eureka注冊服務(wù)中心的通信zone和url地址 serviceUrl: #http://localhost:8080/eureka/eureka defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #服務(wù)注冊中心實例的主機名 instance: hostname: 127.0.0.1 prefer-ip-address: true instance-id: 127.0.0.1:8080 server: #設(shè)為false,關(guān)閉自我保護,即Eureka server在云心光器件會去統(tǒng)計心跳失敗比例在15分鐘之內(nèi)是否低于85%,如果低于85%,EurekaServer #會將這些事例保護起來,讓這些事例不會過期,但是在保護器內(nèi)如果剛哈這個服務(wù)提供者非正常下線了,此時服務(wù)消費者會拿到一個無效的服務(wù) #實例,此時調(diào)用會失敗,對于這個問題需要服務(wù)消費者端有一些容錯機制,如重試、斷路器等; enable-self-preservation: false #掃描失效服務(wù)的間隔時間(單位是毫秒,摩恩是60*1000),即60s eviction-interval-timer-in-ms: 10000
(5)測試
三.搭建提供者服務(wù)
1.
2.
3.
4.
5.
6.
(1)定義pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--引入父工程依賴--> <parent> <groupId>com.chen.test</groupId> <artifactId>springcloud-test</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.chen.test</groupId> <artifactId>cloud-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-provider</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>2020.0.0</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
(2)刪除test文件加(可有可無)
(3)修改配置文件為application.yml(可有可無)
spring: application: name: cloud-provider server: port: 8081 eureka: client: #此客戶端是否獲取eureka服務(wù)器注冊表上的注冊信息,默認為true fetch-registry: false #實例是否在eureka服務(wù)器上注冊自己的信息以供其他服務(wù)發(fā)現(xiàn),默認為true,即自己注冊自己。 register-with-eureka: true service-url: #defaultZone 這個是不會提示的,此處需要自己寫 #實際上屬性應(yīng)該是service-url,這個屬性是個map(key-value)格式;當(dāng)key是defaultZone的時候才能被解析;所以這里沒有提示, #但是自己還需要寫一個defaultZone; defaultZone: http://localhost:8080/eureka #服務(wù)注冊中心實例的主機名 instance: hostname: 127.0.0.1 prefer-ip-address: true instance-id: 127.0.0.1:8081
(4)啟動類加注解@EnableEurekaClient
package com.chen.test.cloudprovider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class CloudProviderApplication { public static void main(String[] args) { SpringApplication.run(CloudProviderApplication.class, args); } }
(5)測試
四.搭建消費者服務(wù)
1.
2.
3.
4.
5.
(1)定義pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--引入父工程依賴--> <parent> <groupId>com.chen.test</groupId> <artifactId>springcloud-test</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.chen.demo</groupId> <artifactId>cloud-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-client</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>2020.0.0</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
(2)刪除test文件夾(可有可無)
(3)修改配置文件為application.yml(可有可無)
server: #定義端口號 port: 8082 spring: application: #定義應(yīng)用名稱,即服務(wù)名稱 name: cloud-client eureka: client: service-url: defaultZone: http://localhost:8080/eureka #服務(wù)注冊中心實例的主機名 instance: hostname: 127.0.0.1 prefer-ip-address: true instance-id: 127.0.0.1:8082
(4)啟動類加注解@EnableEurekaClient
package com.chen.demo.cloudclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class CloudClientApplication { public static void main(String[] args) { SpringApplication.run(CloudClientApplication.class, args); } }
(5)測試
(6)在父pom中加上
<modules> <module>eureka-server</module> <module>cloud-provider</module> <module>cloud-client</module> </modules>
五.實現(xiàn)服務(wù)之間的調(diào)用
1.在cloud-provider中創(chuàng)建controller包和service包
package com.chen.test.cloudprovider.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.chen.test.cloudprovider.service.HelloService; @RestController @RequestMapping("/hello") public class HelloController { @Autowired private HelloService helloService; @GetMapping("/getHello") public String getHello(){ return helloService.getHello(); } }
package com.chen.test.cloudprovider.service; public interface HelloService { String getHello(); }
package com.chen.test.cloudprovider.service.impl; import com.chen.test.cloudprovider.service.HelloService; import org.springframework.stereotype.Service; @Service public class HelloServiceImpl implements HelloService { @Override public String getHello() { return "你好兄弟"; } }
(2)測試
(3)在cloud-client中創(chuàng)建controller包和service包
package com.chen.demo.cloudclient.controller; import com.chen.demo.cloudclient.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/Hello") public class HelloClient { @Autowired private HelloService helloService; @GetMapping("/getClient") public String getClient(){ return helloService.getProduct(); } }
package com.chen.demo.cloudclient.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; //name 為product項目中application.yml配置文件中的application.name; //path 為product項目中application.yml配置文件中的context.path; @FeignClient(name = "cloud-provider",path ="/hello" ) //@Componet注解最好加上,不加idea會顯示有錯誤,但是不影響系統(tǒng)運行; @Component public interface HelloService { @RequestMapping(value = "getHello") String getProduct(); }
特別注意
在啟動類加上注解@EnableFeignClients
package com.chen.demo.cloudclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class CloudClientApplication { public static void main(String[] args) { SpringApplication.run(CloudClientApplication.class, args); } }
(4)測試
到此這篇關(guān)于使用Idea簡單快速搭建springcloud項目的圖文教程的文章就介紹到這了,更多相關(guān)idea搭建springcloud項目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
連續(xù)調(diào)用多個外部系統(tǒng)寫接口保證數(shù)據(jù)一致性的思路
今天小編就為大家分享一篇關(guān)于連續(xù)調(diào)用多個外部系統(tǒng)寫接口保證數(shù)據(jù)一致性的思路,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12詳解IDEA 啟動tomcat 端口占用原因以及解決方法( 使用debug模式)
這篇文章主要介紹了詳解IDEA 啟動tomcat 端口占用原因以及解決方法( 使用debug模式) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼
本篇文章主要介紹了SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10解決Springboot項目中很多頁面出現(xiàn)Whitelabel Error Page(404)的問題
最近在接手的前后端項目中發(fā)現(xiàn)其默認路徑不是主機+端口(如:http://localhost:3453/)的形式,很多頁面的訪問是加了一個層級,只要訪問頁面就會出現(xiàn)Whitelabel Error Page(404),所以本文給大家提供了解決方案,需要的朋友可以參考下2024-02-02java DataInputStream和DataOutputStream詳解及實例代碼
這篇文章主要介紹了java DataInputStream和DataOutputStream詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01