spring cloud 專題一 (spring cloud 入門搭建 之 Eureka註冊中心搭建)

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

  •       代碼在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 看看啓動結果,點擊控制檯中打印的連接 ,瀏覽器進入註冊中心

 

       

 

      若是出現圖上,則註冊中心已經搭建完畢,專題二 將講解 微服務在實際開發中的使用方式 以及服務暴露方式,稍後更新

相關文章
相關標籤/搜索