pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在启动类上面加入注解
@SpringBootApplication
@EnableDiscoveryClient
public class ExampleClientApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleClientApplication.class, args);
}
}
@EnableDiscoveryClient 含义为开启一个客户端
提供服务接口
@RestController
public class HelloCloudController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String helloCloud(){
return "hello SpringCloud !";
}
}
客户端一般就是提供各种各样的服务接口,这里我们简单的写了一个返回字符串信息的接口,通过get请求能够访问它。
将客户端注册到eureka
配置文件中添加:
server:
port: 8771
eureka:
instance:
hostname: localhost
#instance-id
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
defaultZone 指的就是注册的地址,如果有多个节点,则把每个节点的配置加入到defaultZone,以逗号隔开。
同时instance指的是本机的host,instance-id是一个有意思的配置,可以改变你的实例在eureka上显示的地址。
最后
客户端没有太多复杂的配置,开箱即用还是强大啊…