Springcloud-Alibaba 〖三〗微服務Eureka集羣搭建,負載均衡,服務註冊發現

Springcloud-Alibaba 〖三〗微服務Eureka集羣搭建,負載均衡,服務註冊發現

PS: github倉庫倉庫地址項目都放到裏面了

本篇章的第三章

六. cloud-eureka-server7001模塊構建

6.1 Eureka系統架構

在這裏插入圖片描述

6.2 創建Eureka模塊

在這裏插入圖片描述

6.3 pom文件裏添加依賴

<?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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.aiguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>
    <dependencies>
        <!--eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--引入自己定義的api通用包,可以使用payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

6.4 application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服務端的實例名稱
  client:
    #false表示不向註冊中心註冊自己
    register-with-eureka: false
    #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址
      defaultZ4 one: http://${eureka.instance.hostname}:${server.port}/eureka/


6.5 主啓動類

package com.atguigu.springcloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

6.6 打開 http://localhost:7001/ 進入UI界面

在這裏插入圖片描述
此時還沒有服務註冊進來

七. 服務註冊

7.1 支付微服務8001入駐進eurekaServer

7.1.1 增加新依賴

<!--eureka-client-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

7.1.2 application.yml 裏面添加Eureka配置

eureka:
  client:
    #表示是否將自己註冊進EurekaServer默認爲true
    register-with-eureka: true
    #是否從EurekaServer抓取已有的註冊信息,默認爲true。單節點無所謂,集羣必須設置爲true才能配合ribbon使用負載均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

7.1.3 主啓動添加 @EnableEurekaClient

在這裏插入圖片描述

7.1.4 啓動測試

在這裏插入圖片描述
可以看到8001服務註冊進來了

7.2 訂單微服務80入駐進eurekaServer

7.2.1 增加新依賴

<!--eureka-client-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

7.2.2 application.yml 裏面添加Eureka配置

spring:
  application:
    name: cloud-order-server

eureka:
  client:
    #表示是否將自己註冊進EurekaServer默認爲true
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

7.2.3 主啓動添加 @EnableEurekaClient

在這裏插入圖片描述

7.2.4 啓動測試

在這裏插入圖片描述
服務已經註冊進來

八. Eureka集羣搭建

8.1 cloud-eureka-server7002模塊構建

8.1.1 創建module cloud-eureka-server7002

在這裏插入圖片描述

8.1.2 pom文件添加依賴

與7001項目依賴一樣

<?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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.aiguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7002</artifactId>

    <dependencies>
        <!--eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--引入自己定義的api通用包,可以使用payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

8.1.3 添加主啓動類

在這裏插入圖片描述

8.1.4 添加 application.yml 時先修改本機端口映射

在這裏插入圖片描述
在HOSTS文件底添加端口映射

127.0.0.1   eureka7001.com
127.0.0.1   eureka7002.com

修改不了文件沒權限的可以參照以下鏈接去修改

解決問題

8.2 修改7001和7002的application.yml

8.2.1 修改7001 application.yml,直接粘貼

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com  #eureka服務端的實例名稱
  client:
    #false表示不向註冊中心註冊自己
    register-with-eureka: false
    #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址
      defaultZone: http://eureka7002.com:7002/eureka/

8.2.2 修改7002 application.yml,直接粘貼

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #eureka服務端的實例名稱
  client:
    #false表示不向註冊中心註冊自己
    register-with-eureka: false
    #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址
      defaultZone: http://eureka7001.com:7001/eureka/

8.3 測試集羣

在7002裏能看見7001既守望7001端口,就算配置成功
在這裏插入圖片描述

九. 將兩個微服務發佈到Eureka集羣配置中

9.1 8001項目 application.yml

在這裏插入圖片描述
添加如下

 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

9.2 80項目 application.yml

不同的地點,添加相同的配置

 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

9.3 測試

服務都已註冊進來
在這裏插入圖片描述


加粗樣式
測試單個服務也成功
在這裏插入圖片描述

十. 創建module cloud-provider-payment8002

這個模塊跟8001模塊一樣的只不過端口不一樣,所以前期創建模塊我就省略步驟了,大家copy即可,我把步驟貼出來

10.1 改POM

10.2 建 application.yml 文件

10.3 主啓動

10.4 業務類

10.5 修改 Controller

8001 Controller
在這裏插入圖片描述
8002 Controller.
在這裏插入圖片描述

10.6 開啓負載均衡

10.6.1 首先到80服務下去修改寫死的對外暴露的服務url爲Eureka註冊中心的名字

在這裏插入圖片描述
在這裏插入圖片描述

10.6.2 在我們80項目的restTemplate加入spring容器的bean中增加一個負載均衡的註解 @LoadBalanced

在這裏插入圖片描述

10.6.3 測試

負載均衡測試成功

  • 8002端口
    在這裏插入圖片描述
  • 8001端口
    在這裏插入圖片描述

製作不易,轉載請標註,別白嫖啊~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章