1.搭建EurekaServer
(1)引入eureka-server依赖
<dependencies> <!--eureka服务端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
(2)添加@EnableEurekaServer注解
(3)在application.yml中配置eureka地址
server: port: 10086 #服务端口 spring: application: name: eurekaserver #服务名称 eureka: client: service-url: #eureka的地址信息 defaultZone: http://127.0.0.1:10086/eureka/
2.服务注册
(1)引入eureka-client依赖
<!--eureka客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
(2)在application.yml中配置euraka地址
spring: application: name: userservice #user服务的服务名称 eureka: client: service-url: #eureka的地址信息 defaultZone: http://127.0.0.1:10086/eureka/
3.服务发现
(1)引入eureka-client依赖
<!--eureka客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
(2)在application.yml中配置eureka地址
(3)给RestTemplate添加@LoadBalanced注解
(4)用服务提供者的服务