先附上大神博客:史上最简单的 SpringCloud 教程
在学习使用springcloud之前需要先了解学习springboot,因为springboot是springcloud实现快速开发的基础,我的理解就是springcloud的各个服务就是一个个的springboot项目。所以要学springcloud先学springboot。
使用springcloud开发分布式系统肯定需要搭建服务注册中心,尝试搭建一个入门级的使用eureka搭建服务注册中心。
1.新建一个springboot项目eureka-server,依赖选择 eureka server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2.在application.properties配置文件中配置eureka相关配置
##注册中心服务端口
server.port=1111
##在默认设置下,eureka的服务注册中心也会将自己作为客户端注册自己,可以在application.properties配置文件中禁用这个功能
eureka.instance.hostname=localhost
##false表示不向注册中心注册自己
eureka.client.register-with-eureka=false
##注册中心的任务是维护服务,不需要检测服务,设为false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
3.在springboot的启动类Application.java类上加上注解@EnableEurekaServer启动服务注册功能
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
eureka server 已经整合了界面,启动项目后url访问:http://localhost:1111 就可以查看到eureka注册中心状况