up:: SpringCloud项目初始化
开发EurekaServer
创建包和配置文件
说明: 为了确保类的唯一性,我们一般需要在前面加上一些名,比如机构或者组织
引入依赖
<?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">
<parent>
<artifactId>cloud-mall-practice</artifactId>
<groupId>com.imooc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-mall-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>
cloud-mall-eureka-server
</name>
<description>Spring Cloud Eureka Server</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
配置文件
spring.application.name=eureka-server
server.port=8000
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
说明: 配置项前面开发说过,请点击回去查看开发EurekaServer
这里补充两句:
eureka.client.registerWithEureka : 表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。 eureka.client.fetchRegistry : 表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
单体与集群:
注册中心集群化 为了提供高可用性,说白了就是保证机器挂了还是可以服务注册的能力,一般都不会将注册中心部署到一台机器上,而是采取多台机器部署,集群化管理。 注册中心的集群化,最简单的做法就是,将注册中心相互注册,但是不开启检索服务的能力(fetch-registry: false),这也是Eureka的特性使得的,因为Eureka天然就是集服务端与客户端为一体的。
fetch-registry: 检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务。
register-with-eureka: 服务注册中心也会将自己作为客户端来尝试注册自己,为true(默认)时自动生效
eureka.client.serviceUrl.defaultZone 是一个默认的注册中心地址。配置该选项后,可以在服务中心进行注册。
创建启动类
package com.imooc.cloud.mall.practice.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 描述: Eureka Server的启动类,提供服务注册与发现
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
说明:没啥值得注意的,前面已经学习过了
注解说明:
@EnableEurekaClient与@EnableDiscoveryClient的区别:
@EnableDiscoveryClient注解是基于spring-cloud-commons依赖,相当于一个公共的服务发现;
@EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用;
测试
开发完一个模块或者一项功能最好测试一下,防止问题积累太多而无法排错。。。
我们为了更好了解配置项,将下面配置项开启。。
重新运行: