springcloud框架的簡單搭建(服務註冊中心)

發現不少招聘都須要會springcloud,因此最近在學習springcloud。

必需要從零開始用IDEA搭建一下這個springcloud框架,本文是採用Eureka做爲服務註冊與發現的組件。java

  1. 先用IDEA建立一個MAVEN項目。
    新建項目
    下一步須要填寫GroupId和ArtifactId,這裏按照本身的習慣去填寫就行了。
    clipboard.png
    下一步選擇項目位置和項目名稱點擊完成就能夠了。
    clipboard.png
  2. 在該MAVEN項目下新建一個模塊,這裏就須要新建的是springboot了。
    clipboard.png
    這裏選擇的是spring initializr進行springboot的建立。
    clipboard.png
    仍是要對Group和Artifact進行設置。
    clipboard.png
    點擊下一步以後,咱們須要選擇Cloud Discovery下的Eureka Server,點擊下一步而後點擊完成就能夠了。
    clipboard.png
  3. 如今咱們須要對服務中心eureka-server進行一些相關的配置了。
    打開它的pom.xml文件,咱們看到是這樣的:web

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.zhouxiaoxi</groupId>
        <artifactId>eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    而後咱們須要啓動一個服務註冊中心,這須要在springboot工程的啓動application類上加一個註解@EnableEurekaServer:spring

    package com.zhouxiaoxi.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }

    先將配置文件改成yml,而後進行相關的配置,以下:apache

    #端口號
    server:
      port: 8080
    
    eureka:
      instance:
        hostname: localhost
      client:
        #服務註冊中心也會將本身做爲客戶端來嘗試註冊本身,爲true(默認)時自動生效。
        registerWithEureka: false
        #檢索服務選項,當設置爲True(默認值)時,會進行服務檢索,註冊中心不負責檢索服務。
        fetchRegistry: false
        #是一個默認的註冊中心地址。配置該選項後,能夠在服務中心進行註冊。
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
    spring:
      application:
        name: eurka-server
  4. 配置完成以後,啓動項目,而後瀏覽器地址欄輸入http://localhost:8080而後出現如下頁面,表示註冊中心搭建完畢。

    clipboard.png

  5. 接下來咱們就須要建立一個服務提供者 (eureka-client)
    仍是和上面同樣新建一個模塊,此次咱們命名爲eureka-client,而後選擇依賴的時候有點區別:
    clipboard.png
    點擊下一步知道完成就能夠了。
  6. 覈對一下pom.xml文件信息:瀏覽器

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.zhouxiaoxi</groupId>
        <artifactId>eureka-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-client</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
  7. 咱們這邊也須要對啓動類進行相關的註解添加:
    由於咱們這邊用的是eureka搭建的服務中心,因此咱們這邊仍是用@EnableEurekaClient註解就能夠了springboot

    package com.zhouxiaoxi.eurekaclient;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    //@EnableDiscoveryClient註解是基於spring-cloud-commons依賴,至關於一個公共的服務發現;
    //@EnableEurekaClient註解是基於spring-cloud-netflix依賴,只能爲eureka做用;
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    
    }
  8. 須要對配置文件進行相關的配置:app

    server:
      port: 8081
    
    spring:
      application:
        name: eureka-client
    
    eureka:
      client:
        serviceUrl:
          #註明本身的服務註冊中心的地址
          defaultZone: http://localhost:8080/eureka/
  9. 新建一個controller,而後寫一個hello方法:框架

    clipboard.png

    package com.zhouxiaoxi.eurekaclient.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Value("${server.port}")
        String port;
    
        @RequestMapping("/hello")
        public String home(@RequestParam(value = "name") String name) {
            return "hello " + name + " ,i am from port:" + port;
        }
    
    }
  10. 啓動該工程以後,打開剛纔的瀏覽器頁面刷新後會發現一個服務已經註冊在服務中了,服務名爲EUREKA-CLIENT ,端口爲8081:maven

    clipboard.png

    這時打開 http://localhost:8081/hello?name=world,你會在瀏覽器上看到 :spring-boot

    hello world ,i am from port:8081

    至此,咱們的一個較爲完整的項目就搭建起來了。

  11. 不過咱們還能夠運行多個服務,這裏有個小技巧了就:
    先打開IDEA右上角的編輯配置,以下圖:
    clipboard.png
    而後找到咱們剛纔的工程,將容許並行運行給勾選上,應用並肯定就能夠了:
    clipboard.png
    找到該工程下的配置文件,將端口號進行修改後啓動,就能夠並行啓動多個服務了:
    啓動幾個咱們就會在註冊中心的頁面上看到幾個服務。
    clipboard.png

    恩,先這樣吧,後續再更新。。。

相關文章
相關標籤/搜索