1. 程式人生 > 其它 >使用Idea簡單搭建springcloud專案

使用Idea簡單搭建springcloud專案

前言:
開發工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服務系統,實現服務之間的呼叫。
需要搭建一個父工程springcloud-test,一個服務註冊中心eureka-server,兩個微服務cloud-client,cloud-provider。
兩個微服務均註冊到服務註冊中心。
一.搭建父專案
在這裡插入圖片描述
2.
在這裡插入圖片描述
3.
在這裡插入圖片描述

(1)刪掉src目錄
在這裡插入圖片描述
(2)定義pom.xml檔案

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 二.搭建eureka-server註冊中心 1. 在這裡插入圖片描述 2. 在這裡插入圖片描述 3. 在這裡插入圖片描述 4. 在這裡插入圖片描述 5. 在這裡插入圖片描述 6. (1)定義pom.xml檔案 <?xml version="1.0" encoding="UTF-8"?>


4.0.0

<!--引入父工程依賴-->
<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(2)刪掉test資料夾(自己設定,可有可無)
(3)啟動加註解@EnableEurekaServer(開啟eureka服務)

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);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(4)定義配置檔案
配置檔案改為application.yml(可有可無,看自己喜好)

server:
port: 8080

spring:
application:
#應用名稱(在註冊中顯示的)
name: eureka-server
eureka:
client:
#此客戶端是否獲取eureka伺服器登錄檔上的註冊資訊,預設為true
fetch-registry: false
#例項是否在eureka伺服器上註冊自己的資訊以供其他服務發現,預設為true,即自己註冊自己。
register-with-eureka: true
#與Eureka註冊服務中心的通訊zone和url地址
serviceUrl:
#http://localhost:8080/eureka/eureka
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka
#服務註冊中心例項的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8080
server:
#設為false,關閉自我保護,即Eureka server在雲心光器件會去統計心跳失敗比例在15分鐘之內是否低於85%,如果低於85%,EurekaServer
#會將這些事例保護起來,讓這些事例不會過期,但是在保護器內如果剛哈這個服務提供者非正常下線了,此時服務消費者會拿到一個無效的服務
#例項,此時呼叫會失敗,對於這個問題需要服務消費者端有一些容錯機制,如重試、斷路器等;
enable-self-preservation: false
#掃描失效服務的間隔時間(單位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(5)測試
在這裡插入圖片描述
三.搭建提供者服務
1.
在這裡插入圖片描述
2.
在這裡插入圖片描述
3.
在這裡插入圖片描述
4.
在這裡插入圖片描述
5.
在這裡插入圖片描述
6.
(1)定義pom.xml檔案

<?xml version="1.0" encoding="UTF-8"?>


4.0.0


com.chen.test
springcloud-test
1.0-SNAPSHOT

<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(2)刪除test檔案加(可有可無)
(3)修改配置檔案為application.yml(可有可無)

spring:
application:
name: cloud-provider
server:
port: 8081

eureka:
client:
#此客戶端是否獲取eureka伺服器登錄檔上的註冊資訊,預設為true
fetch-registry: false
#例項是否在eureka伺服器上註冊自己的資訊以供其他服務發現,預設為true,即自己註冊自己。
register-with-eureka: true
service-url:
#defaultZone 這個是不會提示的,此處需要自己寫
#實際上屬性應該是service-url,這個屬性是個map(key-value)格式;當key是defaultZone的時候才能被解析;所以這裡沒有提示,
#但是自己還需要寫一個defaultZone;
defaultZone: http://localhost:8080/eureka
#服務註冊中心例項的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8081

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(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);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(5)測試
在這裡插入圖片描述
四.搭建消費者服務
1.
在這裡插入圖片描述
2.
在這裡插入圖片描述
3.
在這裡插入圖片描述
4.
在這裡插入圖片描述
5.
在這裡插入圖片描述
(1)定義pom.xml檔案

<?xml version="1.0" encoding="UTF-8"?>


4.0.0


com.chen.test
springcloud-test
1.0-SNAPSHOT

<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(2)刪除test資料夾(可有可無)
(3)修改配置檔案為application.yml(可有可無)

server:
#定義埠號
port: 8082
spring:
application:
#定義應用名稱,即服務名稱
name: cloud-client
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
#服務註冊中心例項的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8082
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(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);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(5)測試
在這裡插入圖片描述
(6)在父pom中加上

<modules>
    <module>eureka-server</module>
    <module>cloud-provider</module>
    <module>cloud-client</module>
</modules>

1
2
3
4
5
在這裡插入圖片描述

五.實現服務之間的呼叫
1.在cloud-provider中建立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();
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.chen.test.cloudprovider.service;

public interface HelloService {

String getHello();

}

1
2
3
4
5
6
7
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 "你好兄弟";
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(2)測試
在這裡插入圖片描述
(3)在cloud-client中建立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();
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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會顯示有錯誤,但是不影響系統執行;
@Component
public interface HelloService {

@RequestMapping(value = "getHello")
String getProduct();

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
特別注意
在啟動類加上註解@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);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(4)測試
在這裡插入圖片描述