1、前言java
本文爲spring cloud 微服務框架專題的第一篇,主要講解如何快速搭建Eureka 註冊中心 。 本文理論很少,主要是傻瓜式的環境搭建,適合新手快速入門。git
爲了更好的懂得原理,你們能夠下載《spring cloud 和docker微服務架構實戰》pdf得書籍 連接: https://pan.baidu.com/s/1LLSqy0QGOhFei-5XJ2HVSA 密碼: d2x7github
若是這個連接失效了,你們能夠聯繫個人郵箱,我會很快回復並把pdf發送給您, 郵箱地址 xinyudai_ifox@163.comweb
本教程代碼地址爲 https://github.com/daixiaoyu/springcloud-example-feign,你們能夠下載下來運行spring
代碼說明:爲了力求真實開發實戰,沒有將註冊中心,微服務,網關三個項目合在一個module中,而是拆分了,因此引入到idea中時請開三個idea分別引入docker
2、準備環境瀏覽器
maven(將maven配置到環境變量中,便於後期打包)、 若是須要源碼請安裝git、jdk 架構
3、註冊中心搭建mvc
一、搭建Eurake 註冊中心app
<!-- 常規的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>
server: port: 9527 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:9527/eureka/
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")); }
若是出現圖上,則註冊中心已經搭建完畢,專題二 將講解 微服務在實際開發中的使用方式 以及服務暴露方式,稍後更新