spring cloud 专题一 (spring cloud 入门搭建 之 Eureka注册中心搭建)
一、前言
本文为spring cloud 微服务框架专题的第一篇,主要讲解如何快速搭建Eureka 注册中心 。 本文理论不多,主要是傻瓜式的环境搭建,适合新手快速入门。
为了更好的懂得原理,大家可以下载《spring cloud 和docker微服务架构实战》pdf得书籍 链接: https://pan.baidu.com/s/1LLSqy0QGOhFei-5XJ2HVSA 密码: d2x7
如果这个链接失效了,大家可以联系我的邮箱,我会很快回复并把pdf发送给您, 邮箱地址 xinyudai_ifox@163.com
本教程代码地址为 https://github.com/daixiaoyu/springcloud-example-feign,大家可以下载下来运行
代码说明:为了力求真实开发实战,没有将注册中心,微服务,网关三个项目合在一个module中,而是拆分了,所以引入到idea中时请开三个idea分别引入
二、准备环境
maven(将maven配置到环境变量中,便于后期打包)、 如果需要源码请安装git、jdk
三、注册中心搭建
1、搭建Eurake 注册中心
- 代码在springcloud-example-feign下的EurekaServer项目中,请导入Idea
- pom文件引入包如下
<!-- 常规的spring boot 引入 -->
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <start-class>com.dai.cloud.TestStart</start-class> </properties>
<!-- 引入spring cloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies>
<!-- 引入spring mvc 因为这是一个server 并为我们提供了注册中心页面,需要web模块 大家可以理解为就是一个网站 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!--引入eureka 注册中心模块 这是spring cloud 微服务框架集成的,后期我会开设源码分析专题 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> - 编写配置文件,全在application.yml中,这里的原理我暂时不讲,本文是为了协助pdf书籍快速搭建,原理可以参看pdf书籍
server: port: 9527 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:9527/eureka/
- 搭建启动类,为了稳定和后期的接入,大家请用我的包名在src.main.java下建立包 com.dai.cloud 并在包中建立TestStart.java类
package com.dai.cloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import java.net.InetAddress; @SpringBootApplication @EnableEurekaServer //表明这是Eureka注册中心 @EnableAutoConfiguration public class TestStart { private static final Logger logger = LoggerFactory.getLogger(TestStart.class); public static void main(String[] args)throws Exception{ SpringApplication application = new SpringApplication(TestStart.class); final ApplicationContext applicationContext = application.run(args); Environment environment = applicationContext.getEnvironment(); logger.info("\n---------------------------------\n\t" +"Application '{}' is running! Access URLS: \n\t "+ "Local: \t\thttp://localhost:{}\n\t" +"External:\thttp://{}:{}\n---------------------------------", environment.getProperty("spring.application.name"), environment.getProperty("server.port"), InetAddress.getLocalHost().getHostAddress(),environment.getProperty("server.port")); }
- 好了,大功告成,运行TestStart.java 看看启动结果,点击控制台中打印的链接 ,浏览器进入注册中心
如果出现图上,则注册中心已经搭建完毕,专题二 将讲解 微服务在实际开发中的使用方式 以及服务暴露方式,稍后更新