springboot+springcloud微服務入門


MicroService實現技術:

  用springBoot來創建單個服務,用SpringCloud來管理這些微服務。

  ##SpringCloud的五大神獸

  #1.注冊/服務發現——Netflix Eureka
    管理服務器地址和ip的

  #2.客服端負載均衡——Netflix Ribbon\Feign
    服務請求的分配

  #3.斷路器——Netflix Hystrix
    對有故障的服務進行處理

  #4.服務網關——Netflix Zuul
    微服務的統一入口。

  #5.分布式配置——Spring Cloud Config
    對微服務的配置文件做同一管理

SpringCloud入門步奏

  1.注冊中心
    用於管理微服務的地址
    1.1當微服務可以解決注冊的注冊表(IP、端口、服務名稱)

    1.2從每個微服務注冊微服務地址清單

    1.3使用心跳機制微服務合同:每N秒發送一個請求到注冊表,告訴注冊表,

      我還活着,如果微服務掛,有心跳合同失敗,

      注冊表微服務地址列表,其他服務將更新地址列表

  2.用戶管理微服務
  3.訂單管理微服務

 

1.注冊中心配置

  1.1先創建個普通的maven項目父級模塊,

    在pom.xml里配置公用的jar包管理

    

<properties>
        <!--編碼-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!--
   1.管理SpringBoot的jar包
   -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <!--
    2.管理spring-cloud的jar包
    -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!--
        測試
    -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

 1.2.創建注冊中心 --項目名:eureka-server-1000

 在父級模塊中創建個spring-boot項目,也是maven項目,

  但是要選中quickstart

  

 

 

 

  1.3在eureka-server-1000模塊中的pom.xml添加依賴

  

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-1000</artifactId>

    <name>eureka-server-1000</name>



    <dependencies>
        <!--
            注冊/服務發現的依賴
            有了它,才能獲取到所有微服務的ip,端口,服務名
            有了它,其他的微服務才能獲取到所有微服務的ip,端口,服務名清單
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--
            springboot-web的環境依賴
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

 

  1.4再建個resources配置文件夾,

  在配置文件夾中建個application.yml文件

  在yml文件中添加

  

server:
  port: 1000 #端口號
eureka:
  instance:
    hostname: localhost #主機
  client:
    registerWithEureka: false #禁止注冊中心向自己注冊
    fetchRegistry: false #禁止注冊中心拉取注冊地址清單
    serviceUrl: #微服務向注冊中心注冊的地址
      defaultZone: http://localhost:1000/eureka/

 

  1.5在java中建個配置類添加

/**
 * @EnableEurekaServer
 * 啟動注冊中心,默認關閉
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }

}

  啟動main方法可以看見頁面

  在網頁上輸入http://localhost:1000/顯示如下頁面說明配置成功

 

 

 

2..用戶管理微服務

  模塊名:order-server-2000

  和創建注冊模塊一樣但配置不一樣

 

  2.1在pom.xml中添加依賴

  

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-server-2000</artifactId>

    <name>order-server-2000</name>



    <dependencies>
        <!--
            注冊的微服務控制器
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--
            springboot的web環境配置
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

  2.2在resources配置文件夾中新建個application.yml

  在yml中添加配置

  

eureka:
  client:
    serviceUrl: #指定注冊中心的地址
      defaultZone: http://localhost:1000/eureka/
  instance: #是否顯示ip地址
    prefer-ip-address: true
server: #端口號
  port: 2000
spring:
  application: #服務名
    name: orders-server
  cloud:
    discovery:
      client:
        simple:
          local:
            service-id: orders-server:2000 #顯示的id

 2.3在java文件夾中建個

OrdersServerApplication.java
添加如下代碼
@SpringBootApplication
@RestController
public class OrdersServerApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    public static void main(String[] args) {
        SpringApplication.run(OrdersServerApplication.class);
    }

}

啟動main方法可以看見頁面

  在網頁上輸入http://localhost:2000/頁面顯示

  Hello world

  說明配置成功

  再重啟動注冊模塊的main方法
  在網頁上輸入http://localhost:1000/頁面顯示如下就說明微服務配置成功
  

 

  3.訂單管理微服務 

項目名:user-server-3000

和創建注冊模塊一樣但配置不一樣

 

3.1在pom.xml中添加依賴

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-server-3000</artifactId>

    <name>user-server-3000</name>



    <dependencies>
        <!--
            注冊的微服務控制器
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--
            springboot的web環境配置
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

3.2在resources配置文件夾中新建個application.yml

  在yml中添加配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/
  instance:
    prefer-ip-address: true
server:
  port: 3000
spring:
  application:
    name: user-server
  cloud:
    discovery:
      client:
        simple:
          local:
            service-id: user-server:3000

 3.3在java文件夾中建個

OrdersServerApplication.java
添加如下代碼
 
        
/**
 * @EnableEurekaClient
 * 啟動微服務;
 * 默認開啟
 */
@SpringBootApplication
@RestController
@EnableEurekaClient
public class UserServerApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world2";
    }

    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication.class);
    }

}
 
        

啟動main方法可以看見頁面

  在網頁上輸入http://localhost:3000/頁面顯示

  Hello world2

  說明配置成功

  再重啟動注冊模塊的main方法
  在網頁上輸入http://localhost:1000/頁面顯示如下就說明微服務配置成功

 

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM