使用eureka的步骤
搭建Eureka server
创建工程子模块
导入坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置application.yml
server:
port: 9000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
registerWithEureka: 是否将自己注册到Eureka服务中,本身就是所有无需注册
fetchRegistry : 是否从Eureka中获取注册信息
serviceUrlEureka: 客户端与Eureka服务端进行交互的地址
配置启动类(在启动类上添加注解)
@EnableEurekaServer //激活Eureka Server端配置
启动。打开浏览器访问http://localhost8761即可进入EurekaServer内置的管理控制台
将服务提供者注册到Eureka Server上
引入Eureka Client的坐标
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
修改application.yml添加Eureka Server的信息
eureka:
client:
serviceUrl: # eureka server的路径
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true #使用ip注册
修改启动类,添加服务发现的支持(可选)
添加注解:@EnableDiscoveryClient
或@EnableEurekaClient
从Spring Cloud Edgware版本开始, @EnableDiscoveryClient 或 @EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
服务消费者通过注册中心获取服务列表,并调用
原文:https://www.cnblogs.com/friend-c/p/15089990.html