前言
服务注册与发现的服务端-Eureka。
通过项目名称发现服务并将其注册到eureka服务端。
spring cloud netflix 系列技术栈的链接
spring cloud netflix 系列技术栈
配置Eureka服务端(Eureka注册中心)的步骤
- 在pom文件中添加eureka的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 在eureka入口类中添加@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
- 配置application.yml
# 配置应用程序的名称,让被人发现(eureka是通过服务名称来完成服务的注册与发现的)
spring:
application:
name: spring-cloud-netflix-eureka
server:
port: 8761
eureka:
instance:
# eureka服务端服务器地址(IP)
hostname: localhost
client:
# registerWithEureka,fetchRegistry的值为false表明这是eureka服务端,而非eureka客户端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
# 设置eureka服务端的地址(IP:PORT)
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
创建Eureka服务端(Eureka注册中心)的详细步骤
创建spring-cloud-netflix-eureka
- 选择Directory
- 为服务起名称
- 显示结果
创建pom文件
- 创建pom文件
- 名称只能是pom.xml
- pom.xml中的内容
- <?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>
<!-- 加载统一的依赖管理 -->
<parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
</parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-eureka</artifactId>
<packaging>jar</packaging>
<name>spring-cloud-netflix-eureka</name>
<url>https://blog.csdn.net/weixin_41756573</url>
<inceptionYear>2019-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Test Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Test End -->
<!-- Spring Cloud Eureka Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud Eureka End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定Eureka服务端的入口类 -->
<mainClass>com.baiyang.spring.cloud.netflix.eureka.EurekaApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
-
pom中配置入口类的原因
因为在正式环境中是把项目打包为一个jar文件,jar文件的运行时通过java -jar jar名称 去运行的, java -jar 命令需要知道程序的入口类才能运行项目,必须指定程序的入口类。 -
将pom文件交由IntelliJ IDEA托管
-
托管结果
安装maven项目的目录结构完善项目的目录结构
- 创建src/main/java目录
- 将src/main/java配置为Sources Root目录
- 创建Resources目录
- 将resources目录配置为Resources Root目录
创建程序启动类
- 创建包
域名反转+项目名称
- 创建项目启动类
配置SDK(jdk)
创建项目启动类后会弹出配置SDK的选项
启动类中的内容
- 添加application.yml
将项目配置为Eureka服务端
- 在pom文件中添加eureka的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 在eureka入口类中添加@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
- 配置application.yml
# 配置应用程序的名称,让被人发现(eureka是通过服务名称来完成服务的注册与发现的)
spring:
application:
name: spring-cloud-netflix-eureka
server:
port: 8761
eureka:
instance:
# eureka服务端服务器地址(IP)
hostname: localhost
client:
# registerWithEureka,fetchRegistry的值为false表明这是eureka服务端,而非eureka客户端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
# 设置eureka服务端的地址(IP:PORT)
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
检测eureka服务端是否配置成功
看到如下界面代表eureka服务端配置成功