测试中使用到的代码到在这里https://download.csdn.net/download/zhou920786312/10853300
Eureka概念和使用场景
可以看这边文章https://blog.csdn.net/xiao__jia__jia/article/details/81141556
Eureka服务器
将微服务注册到服务中心去
多个客户端配置就可以做到集群
@SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_App
{
public static void main(String[] args)
{
SpringApplication.run(EurekaServer7001_App.class, args);
}
}
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称,对应host文件的127.0.0.1
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
Eureka客户端
eureka:
client: #客户端注册进eureka服务列表内
service-url:
#eureka服务器地址
defaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
#设置在eureka服务器satus中显示的名称
instance-id: microservicecloud-dept8001
prefer-ip-address: true #访问路径可以显示IP地址
info: #eureka 中微服务的介绍
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中,相当于这个微服务是eureka的客户端
@EnableDiscoveryClient //发现在eureka中的微服务信息
public class DeptProvider8001_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptProvider8001_App.class, args);
}
}