将Eureka Server作为一个服务注册到其它Eureka Server,这样多个Eureka Server之间就能够互相发现对方,同步服务,实现Eureka Server集群(高可用)。
Eureka服务(eureka_service)
修改application.yml配置文件
导入坐标
- 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">
<parent>
<artifactId>springcloud_parent</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka_service</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
配置文件
- application.yml
server:
# 端口设置 有传参使用传参值;没有使用默认10086
port: ${port:10086}
spring:
application:
name: eureka_server
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
# 端口设置 有传参使用传参值;没有使用默认 http://127.0.0.1:10086/eureka
defaultZone: ${defaultZone:http://127.0.0.1:10086/eureka/}
10086端口 子节点配置
-Dport=10086 -DdefaultZone=http://127.0.0.1:10087/eureka/ (图片错误,以此为准)
10087端口 子节点配置
-Dport=10087 -DdefaultZone=http://127.0.0.1:10086/eureka/ (图片错误,以此为准)
创建启动类
package li.chen.com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @ClassName: EurekaServerApplication
* @Description 启动引导类
* @EnableEurekaServer 声明当前类为Eureka服务
**/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
测试
启动
EurekaServerApplication
启动
EurekaServerApplication10087
访问路径 http://127.0.0.1:10086/ 或 http://127.0.0.1:10086/
服务注册(user_service)
仅修改application.yml配置文件
配置文件
- application.yml
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2
username: root
password: root
application:
name: user_service
mybatis:
type-aliases-package: li.chen.com.user.pojo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10086/eureka
springboot启动类
- UserApplication
package li.chen.com.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("li.chen.com.user.mapper")
@EnableDiscoveryClient //开启Eureka客户端发现功能
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
测试
启动UserApplication
访问 http://127.0.0.1:10086/ 或 http://127.0.0.1:10087/ 都显示user_Service服务
注意:EurekaServerApplication要一直启动