一、前言
本文為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 看看啟動結果,點擊控制台中打印的鏈接 ,瀏覽器進入注冊中心
如果出現圖上,則注冊中心已經搭建完畢,專題二 將講解 微服務在實際開發中的使用方式 以及服務暴露方式,稍后更新