springcloud全面搭建微服務

new 一個 empty project出來

添加父module manager,然後刪除src文件夾

在這裏插入圖片描述

<!--手動指定 pom -->
<packaging>pom</packaging>

在pom中定義公共依賴,並將各個子模塊聚合進來

<groupId>org.djh</groupId>
<artifactId>manager</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>../good_service</module>
    <module>../public</module>
    <module>../good_service1</module>
    <module>../eureka</module>
    <module>../eureka1</module>
    <module>../feign</module>
    <module>../zuul</module>
    <module>../config</module>
</modules>
<!--手動指定 pom -->
<packaging>pom</packaging>

添加一個 公共module 存放的是像entity、common和utility等

在這裏插入圖片描述

添加一個商品微服務module:good_service

就是 mapper \rightarrow service \rightarrow controller 的一條龍服務,接口restful風格最後能可以測試出來數據
在這裏插入圖片描述

copy上面的商品微服務module:good_service1,用於集羣

整個複製下來,把pom的artifactId改了

<artifactId>good_service1</artifactId>

端口號改了

到此目錄結構如下

  • manager :聲明和整合
  • public:存放公共部分
  • good_service:用於good服務集羣
  • good_service1:用於good服務集羣

整合eureka

假如出現了消費這個服務的,那麼服務服務之間是直接交互的,要進行遠程調用就需要知道服務端的ip地址和端口在這裏插入圖片描述

而eureka註冊和管理微服務的,註冊中心幫助我們管理這些服務,
在這裏插入圖片描述

微服務會實時上報自己的狀態,註冊中心統一管理這些微服務的狀態,將存在問題的服務踢出服務列表,客戶端獲取到可用的服務進行調用。

創建兩個springboot的module項目,用於做上圖的服務中心

① 服務中心(服務端)

  1. 既然是服務端就需要在pom.xml依賴eureka-server
<dependencies>
    <!-- 導入Eureka-server 服務端依賴 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
  1. 進行集羣配置,registerWithEureka不需要自己註冊因爲自己就是服務端false,fetchRegistry也不需要被發現
server:
  port: 6001
eureka:
  instance:
    hostname: eureka6001.com # eureka服務端的實例名稱
  client:
    registerWithEureka: false # 服務註冊,false表示不將自已註冊到Eureka服務中
    fetchRegistry: false # 服務發現,false表示自己不從Eureka服務中獲取註冊信息
    serviceUrl:    # Eureka客戶端與Eureka服務端的交互地址,集羣版配置對方的地址,單機版配置自己(如果不配置則默認本機8761端口)
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集羣版
      defaultZone: http://eureka6002.com:6002/eureka/
  server:
     enable-self-preservation: false # 禁用自我保護機制
  1. 啓動類
    @EnableEurekaClient
@EnableEurekaServer //標識一個Eureka Server 服務註冊中心
@SpringBootApplication
public class eureka {
    public static void main(String[] args) {
        SpringApplication.run(eureka.class, args);
    }
}

另一臺變端口和defaultZone

② 兩個商品微服務客戶端

  1. 既然是商品服務端就需要在pom.xml依賴eureka-client
<dependencies>
    <!-- 導入Eureka客戶端的依賴,將 微服務提供者 註冊進 Eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
  1. 因爲是微服務提供者,所以這裏寫明registerWithEureka註冊和被fetchRegistry發現爲true,然後defaultZone爲註冊的地方,即兩臺erueka服務端
eureka:
  client:
    registerWithEureka: true # 服務註冊開關
    fetchRegistry: true # 服務發現開關
    serviceUrl: # 客戶端(服務提供者)註冊到哪一個Eureka Server服務註冊中心,多個用逗號分隔
      defaultZone: http://localhost:6001/eureka/,http://localhost:6002/eureka/
  instance:
    instanceId: ${spring.application.name}:${server.port} # 指定實例ID,就不會顯示主機名了
    preferIpAddress: true
spring:
  application:
    name: microservice-good#這個很重要,這在以後的服務與服務之間相互調用一般都是根據這個name

這時候就可以 查看重啓所有springboot項目,然後輸入localhost:6001 || /6002
在這裏插入圖片描述

整合feign

使用feign達到負載均衡,可以讓我們面向接口編程
Spring Cloud 對 Feign 進行了封裝,Feign 默認集成了 Ribbon 實現了客戶端負載均衡調用。
在這裏插入圖片描述

1、在消費者微服務中使用 Ribbon 實現負載均衡,Ribbon 先從EurekaServer中獲取服務列表。
2、Ribbon 根據負載均衡的算法(默認輪訓算法)去調用微服務。

現在我們將消費者實現出來

  1. 添加Feign依賴,並且這也是eureka-client
<dependencies>
   <!-- 導入Eureka客戶端的依賴,將 微服務提供者 註冊進 Eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>
  1. 配置文件,registerWithEureka不需要註冊,可以去發現服務
spring:
  application:
    name: feign
eureka:
  client:
    registerWithEureka: false # 服務註冊開關
    fetchRegistry: true # 服務發現開關
    serviceUrl: # 客戶端(服務提供者)註冊到哪一個Eureka Server服務註冊中心,多個用逗號分隔
      defaultZone: http://localhost:6001/eureka,http://localhost:6002/eureka
  instance:
    instanceId: ${spring.application.name}:${server.port} # 指定實例ID,就不會顯示主機名了
    preferIpAddress: true
ribbon:
  eureka:
    enabled: true

3.代碼使用

@RestController
public class ProductController {

    @Autowired
    ProductClientService service;
    @RequestMapping(value = "/consumer/product/list")
    public List<Product> list() {
        return service.list();
    }

}

@FeignClient(value = "microservice-product") //指定調用的微服務名稱
public interface ProductClientService {
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    List<Product> list();
}

啓動類

//會掃描指定包下面使用@FeignClient標識的接口
@EnableFeignClients(basePackages = "com.takefree.service")
@EnableEurekaClient //將此服務註冊到Eureka 服務註冊中心
@SpringBootApplication
public class Feign {

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

}

整合zuul(不全)

其中路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎,
客戶端請求網關/api/good,通過路由轉發到 good服務
客戶端請求網關/api/order,通過路由轉發到 order 服務
而過濾功能則負責對請求的處理過程進行干預,是實現請求校驗等功能的基礎.

因爲需要註冊進入erueka,依賴netflix-eureka-client

 <dependencies>
        <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-web</artifactId>
        </dependency>

        <!--zuul路由網關依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

整合配置中心Config

在這裏插入圖片描述
創建github倉庫,新建本地文件夾 git clone 一下

然後新建一個UTF8的yml文件,然後推送
也就是 add commit push 一條龍服務

創造一個config服務端module讀取git上的配置文件

服務端

添加config module,web啓動器依賴

<dependencies>
    <!-- Spring Cloud Config配置中心依賴 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

</dependencies>

新建yml

server:
  port: 5001

spring:
  application:
    name: microservice-config
  cloud:
    config:
      server:
        git: # 遠程庫的git地址
          uri: https://github.com/xxx/microservice-cloud-config.git

啓動類上寫

@EnableConfigServer
@SpringBootApplication
public class Config {

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

}

就可以讀取了
在這裏插入圖片描述
在這裏插入圖片描述

客戶端

然後就是把微服務的配置庫順利的讀進去,依賴config

 <!-- Spring Cloud Config 客戶端依賴-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>

在類路徑resources下創建 bootstrap.yml 配application.yml 是用戶級別的配置項bootstrap.yml 是系統級別的配置項,優先級更高Spring Cloud 會創建一個 Bootstrap Context ,Bootstrap Context 會負責從外部資源加載配置屬性並解析配置; Bootstrap 屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。

spring:
  cloud:
    config:
      name: microservice-config-application #github上的配置名稱,注意沒有yml後綴名
      profile: prod #本次訪問的環境配置項
      label: master #遠程庫的分支名
      uri: http://localhost:5001 #Config配置中心地址,通過它獲取microservice-configapplication.yml配置信息

然後可以把原來的application.yml和git中重複的配置刪去了

整合Bus實現自動更新配置文件

在這裏插入圖片描述
安裝RabbitMQ

在安裝RabbitMQ需要有Erlang,然後配置erlang bin目錄的環境變量

然後安裝Rabbit,安裝完了start,訪問localhost:15632
如果不行,切入sbin 下cmd執行

# 開啓RabbitMQ節點
rabbitmqctl start_app
# 開啓RabbitMQ管理模塊的插件,並配置到RabbitMQ節點上
rabbitmq-plugins enable rabbitmq_management
# 關閉RabbitMQ節點
rabbitmqctl stop

然後再start,再訪問localhost:15632默認賬戶密碼全爲guest
在這裏插入圖片描述

<dependency>
	<groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--監聽器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在bootstrap.properties 配置文件新增RabbitMQ配置以及
消息總線配置

spring:
  cloud:
    config:
      name: microservice-config-application #github上的配置名稱,注意沒有yml後綴名
      profile: prod #本次訪問的環境配置項
      label: master #遠程庫的分支名
      uri: http://localhost:5001 #Config配置中心地址,通過它獲取microservice-configapplication.yml配置信息
  # ---------------新增---------------
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
# 暴露觸發消息總線的地址
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

可以測試一下,首先修改GitHub上的文件

工具發送 POST 請求: http://localhost:8081/actuator/bus-refresh

但是更新druid數據源不會刷新(使用默認的會刷新),需要自定義配置類

@Configuration
public class DruidConfig {

    @RefreshScope //刷新配置
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

}

整合熔斷器 hystrix

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