初识SpringCloud|第一篇|Eureka

​ SpringCloud是微服务的一站式解决方案,为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。

​ 关于微服务一直只是观看过一些资料有一些了解,还没真正动手过,观看和学习了CSDN大牛方志朋的SpringCloud教程博客之后才有的这一次Demo。本次SpringBoot的版本为2.0.3.RELEASE,SpringCloud的版本为 Finchley.RELEASE。

主工程

​ 使用Maven作为主工程,在pom文件中引入相对应的依赖。其他module工程全部继承此父类pom。

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
<?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.kn</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>springcloud</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>

<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>Finchley.RELEASE</spring-cloud.version>
</properties>

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

</project>

###服务注册中心

​ 右键工程,新建module,直接使用spring initializr创建工程,依赖选中Spring Cloud Discovery中的 Eureka server即可,如图。

img

​ 将pom文件修改成继承父pom,并加入spring-cloud-starter-netflix-eureka-server依赖。

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
<?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.kn</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>

<parent>
<groupId>com.kn</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

</project>

​ 在SpringBoot启动类Application上添加一个注解@EnableEurekaServer,声明这是一个服务注册中心

1
2
3
4
5
6
7
8
9
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}

}

​ Eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下Erureka server也是一个Eureka client ,必须要指定一个 server。Eureka server的配置文件appication.yml如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#${eureka.instance.hostname} 设备的主机名
spring:
application:
name: eurka-server

​ 通过声明eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

以下为两个配置属性的解释(引用自CSDN博文https://blog.csdn.net/qq_38289534/article/details/82146939

  • registerWithEureka:是否要注册到其他Server上。如果我的Server上其实开放了一些Http接口供调用,那么就需要注册,这样其他的Client才能发现我的服务,才能通过RPC调用我提供的Http接口。如果我的Server没有提供对外Http接口,那么这个参数可以设置为false。
  • fetchRegistry:是否需要拉取服务信息。和是否注册一样,如果我的Server需要调用其他的Client的Http接口,那么就需要获取相应的服务发现信息,这样才能正常的调用。同时这个参数还有一个重要的作用,就是决定Server在初始化时是否立即全量同步其他节点的服务信息!!!Server初始化时会先初始化其内置的Client。若配置了fetchRegistry=true,那么Client在初始化时会从其他Server全量拉取服务信息,放进Client容器中。Server在初始化时会尝试同步Client容器里的服务信息,如果fetchRegistry=false,服务信息不存在,只能被动的等其他Server节点以增量的形式同步过来(Client在执行注册和心跳时对应的注册Server节点会广播此事件,同步给其他的Server节点。当其他Server节点还没有此服务信息时,改为注册此服务信息)。当然正常的通过心跳来同步,最多也仅需要30S而已,是否需要设置此参数就看各自的需求了。

启动程序,通过localhost:8761查看eureka server的界面如下:

img

服务提供者

​ client创建过程和server类似,相关配置有所改动。当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

​ pom文件配置添加了spring-cloud-starter-netflix-eureka-client和web相关依赖,如下:

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
<?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.kn</groupId>
<artifactId>eureka_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-client</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.kn</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

​ 通过在Application启动类上添加@EnableEurekaClient 注解表明是一个eureka server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}

@Value("${server.port}")
String port;

@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "kn") String name) {
return "hi " + name + " ,i am from port:" + port;
}

}

​ 在application.yml添加相应的配置。

1
2
3
4
5
6
7
8
9
10
server:
port: 8762

spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

​ 需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。启动client工程,打开http://localhost:8761 ,即eureka server 的网址:

img

​ 我们可以看到一个服务已经注册在服务中了,服务名为eureka-client。这时候在地址栏输入http://localhost:8762/hi?name=kn,会出现

1
hi kn ,i am from port:8762

结尾

​ 这一篇进行的比较顺利,没有遇到什么问题。最后附上大佬原版的博客链接:https://blog.csdn.net/forezp/article/details/81040925

0%