一.简介
上篇写了eureka 注册中心的启动:
https://blog.csdn.net/u010939285/article/details/80426844
这篇主要是如何将服务注册到eureka上
二.服务注册
2.1 创建项目 eurekaClient
添加pom依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2 配置application.properties 属性:
#服务端口
server.port=8088
#注册中心服务的ip地址
eureka.client.serviceUrl.defaultZone= http://localhost:8081/eureka/
#服务的应用名
spring.application.name=testEurekaClient
2.3 启动程序配置
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
@GetMapping("/sayHello")
public String sayHello(){
return "hello word";
}
}
添加@EnableEurekaClient 注解表示将服务注册到注册中心上去,因为spring cloud 内部之间的服务调用是通过spring boot 的restful接口去进行远程调用的,所有这边写了一个sayHello接口,提供给远程调用
2.4 启动
启动上一个的注册中心,启动本服务,注册上去
启动注册中心:没有服务
启动eureka 客户端注册上去
应用名 testEurekaClient 注册上去了