新增用例

This commit is contained in:
罗祥
2019-01-14 01:02:37 +08:00
parent 2245a70877
commit 443b1b4b37
43 changed files with 1353 additions and 12 deletions

View File

@ -1,3 +1,194 @@
2019-01-11 11:16:14.320 INFO 13496 --- [nio-8010-exec-4] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVER/DESKTOP-8JGSFLJ:server:8010 with status UP (replication=true)
2019-01-11 11:16:15.084 INFO 13496 --- [ Thread-41] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVER/DESKTOP-8JGSFLJ:server:8020 with status UP (replication=true)
2019-01-11 11:16:15.085 INFO 13496 --- [ Thread-41] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVER/DESKTOP-8JGSFLJ:server:8030 with status UP (replication=true)
# eureka 高可用注册中心的搭建
## 一、项目结构
eureka-server为服务注册中心负责服务的管理
eureka-client 为eureka客户端
![spring-cloud-eureka](D:\spring-samples-for-all\pictures\spring-cloud-eureka-cluster.png)
## 二、三步搭建eureka 高可用注册中心
这里我们以单机伪集群的方式搭建,让三个单机注册中心互相注册,实现注册中心的高可用。配置示意图如下:
![eureka-server-client](D:\spring-samples-for-all\pictures\eureka-server-client.png)
#### 2.1 引入eureka服务端依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
```
#### 2.2 创建三份配置文件,分别代表不同注册中心的配置
![eureka-application](D:\spring-samples-for-all\pictures\eureka-application.png)
application-01.yml:
```yaml
spring:
application:
name: server
server:
port: 8010
eureka:
server:
# 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时剔除
enable-self-preservation: false
instance:
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://localhost:8020/eureka/,http://192.168.200.228:8030/eureka/
```
application-02.yml
```yaml
spring:
application:
name: server
server:
port: 8020
eureka:
server:
# 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时剔除
enable-self-preservation: false
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://127.0.0.1:8010/eureka/,http://192.168.200.228:8030/eureka/
```
application-03.yml
```yaml
spring:
application:
name: server
server:
port: 8030
eureka:
server:
# 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时从列表中剔除
enable-self-preservation: false
instance:
hostname: 192.168.200.228
client:
serviceUrl:
defaultZone: http://127.0.0.1:8010/eureka/,http://localhost:8020/eureka/
```
需要注意的是Eureka互相注册要求各个Eureka实例的eureka.instance.hostname不同如果相同则会被Eureka标记为unavailable-replicas不可用副本
#### 2.3 启动类上增加注解@EnableEurekaServer激活eureka服务端自动配置
```java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
```
## 三、三步搭建eureka 客户端
#### 3.1 引入eureka客户端依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
```
#### 3.2 eureka 客户端配置,指定注册中心地址
```yaml
server:
port: 8040
# 指定服务命名
spring:
application:
name: eureka-client
# 指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8010/eureka/,http://localhost:8020/eureka/,http://192.168.200.228:8030/eureka/
```
#### 3.3 启动类上增加注解@EnableDiscoveryClient激活eureka客户端自动配置
```java
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
```
## 4.启动项目
### 4.1 这里我们可以采用命令行方式指定配置,分别启动三个注册中心
![eureka-active](D:\spring-samples-for-all\pictures\eureka-active.png)
### 4.2 高可用集群搭建成功的判定
这里需要主要的是仅仅status中出现其他注册中心时并不一定是搭建成功的**一定是当注册中心的DS Replicas 和 available replicas中显示其余的注册中心时候**,才代表搭建成功。
#### **4.2.1 点击下面注册中心的可用实例列表中的地址,访问链接分以下几个情况:**
1. hostname和prefer-ip-address都没有配置则访问 主机名:服务名:端口号,
```
http://desktop-8jgsflj:8761/info
```
2. 配置了hostname而没有配置prefer-ip-address则访问 hostname:服务名:端口号,
http://server:8761/info
3. 如果配置了prefer-ip-address则访问 ipAddress:服务名:端口号,
http://192.168.200.228:8761/info
8010 注册中心:
![eureka-8010](D:\spring-samples-for-all\pictures\eureka-8010.png)
8020 注册中心:
![eureka-8020](D:\spring-samples-for-all\pictures\eureka-8020.png)
8030 注册中心:
![eureka-8030](D:\spring-samples-for-all\pictures\eureka-8030.png)
### 4.3 prefer-ip-address 参数说明
在有的配置示例中配置了prefer-ip-address为true。
```properties
eureka.instance.prefer-ip-address=true
```
在多机器独立部署的情况下是没有问题的配置prefer-ip-address为ture代表发现服务时候优先按照ip去搜寻对于多集群而言可以保证尽快准确搜索到服务。而对于单机部署来说ip地址都是相同的这会导致其余注册中心出现在unavailable-replicas(不可用副本)中。所以单机部署时候不建议开启这个参数默认值为false多机部署时候可以开启。

View File

@ -0,0 +1,98 @@
# eureka 服务的注册与发现
## 一、项目结构
eureka-server为服务注册中心负责服务的管理
eureka-client 为eureka客户端
![spring-cloud-eureka](D:\spring-samples-for-all\pictures\spring-cloud-eureka.png)
## 二、三步搭建eureka 服务注册中心
#### 2.1 引入eureka服务端依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
```
#### 2.2 eureka 服务端配置
```yaml
server:
port: 8010
eureka:
instance:
hostname: localhost
client:
# 设置为false,代表不向注册中心注册自己
register-with-eureka: false
# 注册中心主要用于维护服务并不需要检索服务所以设置为false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
```
#### 2.3 启动类上增加注解@EnableEurekaServer激活eureka服务端自动配置
```java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
```
## 三、三步搭建eureka 客户端
#### 3.1 引入eureka客户端依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
```
#### 3.2 eureka 客户端配置
```yaml
server:
port: 8020
# 指定服务命名
spring:
application:
name: eureka-client
# 指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
```
#### 3.3 启动类上增加注解@EnableDiscoveryClient激活eureka客户端自动配置
```java
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
```
## 4.启动项目
#### 4.1 进入注册中心控制台,查看服务注册情况
![eureka](D:\spring-samples-for-all\pictures\eureka.png)