Java B2B2C o2o多使用者商城 springcloud架構 (一)服務的註冊與發現(Eureka)

happywawa發表於2019-04-03

一、spring cloud簡介

spring cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件匯流排、全域性鎖、決策競選、分散式會話等等。它執行環境簡單,可以在開發人員的電腦上跑。另外說明spring cloud是基於springboot的,所以需要開發中對springboot有一定的瞭解,如果不瞭解的話可以看這篇文章:2小時學會springboot。另外對於“微服務架構” 不瞭解的話,可以通過搜尋引擎搜尋“微服務架構”瞭解下。

二、建立一個服務提供者 (eureka client)

當client向server註冊時,它會提供一些後設資料,例如主機和埠,URL,主頁等。Eureka server 從每個client例項接收心跳訊息。 如果心跳超時,則通常將該例項從註冊server中刪除。

建立過程同server類似,建立完pom.xml如下:

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
<?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>
<groupId>com.forezp</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-hi</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>Dalston.RC1</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>

  通過註解@EnableEurekaClient 表明自己是一個eurekaclient.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}

  僅僅@EnableEurekaClient是不夠的,還需要在配置檔案中註明自己的服務註冊中心的地址,application.yml配置檔案如下:

1
2
3
4
5
6
7
8
9
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi

  

需要指明spring.application.name,這個很重要,這在以後的服務與服務之間相互呼叫一般都是根據這個name 。
啟動工程,開啟http://localhost:8761 ,即eureka server 的網址:

Paste_Image.png

你會發現一個服務已經註冊在服務中了,服務名為SERVICE-HI ,埠為7862

這時開啟 http://localhost:8762/hi?name=forezp ,你會在瀏覽器上看到 :

1
hi forezp,i am from port:8762

  架構程式碼如下:

"分散式b2b <wbr

需要JAVASpring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:一零三八七七四六二六


相關文章