up:: Eureka的架构和作用
说明:
这篇博客正式开发Eureka,从代码角度实现Eureka
引入Eureka
引入依赖
添加模块的方法前面已细致讲过,请及时参考:新建多模块项目
<?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>spring-cloud-course-practice</artifactId>
<groupId>com.imooc</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Eureka-server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<name>course-eureka-server</name>
<description>Spring Cloud Eureka</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!--spring的maven项目必备的一个插件-->
<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>
不需要mysql链接,所以去除mysql等依赖
说明:这里Eureka需要依赖于springCloud版本,对于这样多模块的,应该在根目录添加依赖:::
<!--标识SPring Cloud版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件
说明: 关于这两个配置项还是比较模糊,先学习下去,有时间会来看看。。。
#由于该应用为注册中心,所以设置为false。表示不想注册中心注册自己
eureka.client.register-with-eureka=false
#注册中心的职责就是维护服务实例,并不需要去检索服务,因此也设置为false。
eureka.client.fetch-registry=false
加上注解
package com.imooc.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 描述: Eureka服务端
*/
@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作用;