- 通过Spring Initializr快速创建SpringBoot项目
Spring Initializr 在eclipse、idea等开发工具都有插件支持,还可以自行访问 http://start.spring.io/使用。
- 引入Spring boot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
- 引入actuator依赖,提供端点监控服务
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 引入spring cloud的依赖 目前选用Edgware版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 编写实现
源码下载:https://gitee.com/sansan-33/spring-cloud-study.git
模块:san3-provider-user
- 运行结果
rest api:http://localhost:8000/1
{"id":1,"username":"account1","name":"张三","age":20,"balance":100.00}
actuator health : http://localhost:8000/health
{"status":"UP","diskSpace":{"status":"UP","total":106198724608,"free":78122856448,"threshold":10485760},"db":{"status":"UP","database":"H2","hello":1}}
actuator info:http://localhost:8000/info
{"app":{"name":"san3-provider-user","encoding":"UTF-8","java":{"source":"10.0.1","target":"10.0.1"}}}
- 引入eureka服务端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 注册服务
aplication.yml中加入注册配置,如:
eureka:
client:
serviceUrl:
defaultZone: http://[用户名]:[密码]@ip:port/eureka/