SpringCloud微服務架構實戰-微服務商城 (2) 註冊中心搭建

1、服務治理

服務治理是微服務架構的最核心和基礎模塊,用於實現各個微服務間的自動化註冊和發現。服務治理解決了微服務系統架構中微服務實例配置維護困難問題。服務治理框架圍繞服務註冊和服務發現機制來完成對微服務應用實例的自動化管理

2、 服務註冊

構建註冊中心,微服務單元向註冊中心登記自己提供的服務,將主機與端口號、版本號、通訊協議告知註冊中心,註冊中心按照服務名分類組織服務清單。

3、服務發現

在服務治理架構體系下,服務間的調用不再通過指定服務的實例地址,而是通過服務名來調用。在調用服務時,服務調用方並不知道服務的實例地址。服務調用方需要向註冊中心諮詢服務,獲取服務清單,用於對具體服務實例的訪問。

4、 Spring Cloud  Eureka

Spring Cloud使用Netflix Eureka來實現服務的註冊和發現,它即包括服務端組件,又包含了客戶端組件。

Eureka Server  又稱服務註冊中心,支持高可用配置,依託強一致性提供良好的服務實例可用性。Eureka  Client 主要處理服務的註冊和發現,通過定期向服務註冊中心發送心跳刷新自己的服務租約,同時又能從註冊中心查詢當前註冊的服務信息緩存本地並週期性刷新服務註冊信息。

Eureka Client 主要處理服務的註冊和發現,Client向註冊中心註冊自身提供的服務並週期性發送心跳到註冊中心更新服務租約,同時還能從主持中心查詢當前註冊的服務信息並緩存到本地

核心要素:服務註冊中心、服務提供者、服務消費者

實現機制:

服務註冊中心互相註冊形成高可用集羣,失效剔除(定時任務,定時剔除沒有續約的服務實例)

服務提供者:註冊、續約(定時心跳)、下線,服務同步(註冊到一個註冊中心,會將請求轉發給集羣其他註冊重新)

服務消費者:從註冊中心拉取服務並緩存清單

5、Eurreka 搭建

環境:org.springframework.boot 2.1.7.RELEASE   Spring Cloud.version Greenwich.SR2

5.1 Eureka Server 搭建

引入Spring Boot、Spring Cloud、Eureka Server依賴

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.newbie.parent</groupId>
    <artifactId>newbie-parent</artifactId>
    <version>1.0.0</version>
  </parent>
  <artifactId>eureka-server</artifactId>
  <name>eureka-server</name>
  <packaging>jar</packaging>
  <description>Demo project for Spring Boot</description>
  <!--管理依賴jar包-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <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>
  <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-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

通過@EnableEurekaServer註解開啓Eureka服務註冊中心

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

配置註冊中心參數

server.port=1111


#實例url指向 主機ip+端口號      不加的話url指向的依然是主機名+端口號
eureka.instance.hostname=localhost
#設置是否向註冊中心註冊
eureka.client.register-with-eureka=true
#是否向註冊中心發送心跳查詢服務
eureka.client.fetch-registry=true
#eureka默認服務地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

5.2 Eureka Client搭建

引入Spring Boot、Spring Cloud、Eureka Client依賴

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.newbie.parent</groupId>
    <artifactId>newbie-parent</artifactId>
    <version>1.0.0</version>
  </parent>
  <artifactId>hello-consumer</artifactId>
  <packaging>jar</packaging>
  <name>hello-consumer</name>
  <description>Demo project for Spring Boot</description>

  <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>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <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>

通過@EnableDiscoveryClient激活Eureka DiscoveryClient實現,以便能輸出服務信息

@SpringBootApplication
@EnableDiscoveryClient
public class HelloConsumerApplication {

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

}

添加客戶名、註冊中心地址

spring.application.name=hello-consumer

eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

編寫測試Controller,測試輸出服務信息

...
...
@Autowired
  private DiscoveryClient client;

  @RequestMapping(value = "/hello", method = RequestMethod.GET)
  public String home() {
    List<ServiceInstance> instanceList = client.getInstances("qt-eurekaservice");
        for (ServiceInstance instance : instanceList) {
            LOG.info("/hello,host:" + instance.getHost() + ",serviceId:" + instance.getServiceId());

        }
    return "hello world!";
  }
...
...
...

6、高可用註冊中心實現

Eureka 服務治理體系下,應用即是服務提供方,也是服務消費方,Eureka Server通過向其他註冊中心註冊自己,這樣就形成了一組互相組成自己的服務註冊中心,以實現服務清單的同步,達到高可用的效果。

創建註冊中心集羣peer1和peer2

application-peer1.properties

spring.application.name=eureka-server
server.port=1111

eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/

application-peer2.properties

spring.application.name=eureka-server
server.port=1112

eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

多節點本地啓動

mvn spring-boot:run -Dspring-boot.run.profiles=peer1
mvn spring-boot:run -Dspring-boot.run.profiles=peer2

7、服務治理之服務發現與消費搭建

引入Spring Boot、Spring Cloud、Eureka Client、Ribbon依賴

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.newbie.parent</groupId>
    <artifactId>newbie-parent</artifactId>
    <version>1.0.0</version>
  </parent>
  <artifactId>ribbon-consumer</artifactId>
  <name>ribbon-consumer</name>
  <description>Demo project for Spring Boot</description>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <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>

通過註解通過@EnableDiscoveryClient激活Eureka DiscoveryClient實現,以便獲得服務發現能力,同時創建RestTemplate Spring Bean 實例,同時使用@LoadBalanced註解開啓客戶端負載均衡。

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {

  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }

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

}

編寫Controller,實現對目標服務的調用

@RestController
public class ConsumerController {

  @Autowired
  private RestTemplate restTemplate;

  @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
  public String toHelloConsumer() {
    return restTemplate.getForEntity("http://HELLO-CONSUMER/hello", String.class)
        .getBody();
  }

}

本地啓動應用進行服務發現與消費調試

啓動eureka server 

mvn spring-boot:run -Dspring.profiles.active=peer1 -Ddebug

啓動eureka 服務提供者

mvn spring-boot:run  -Dspring-boot.run.jvmArguments="-Dserver.port=8081"
mvn spring-boot:run  -Dspring-boot.run.jvmArguments="-Dserver.port=8082"

啓動ribbon

mvn spring-boot:run  -Dspring-boot.run.jvmArguments="-Dserver.port=8083"

訪問/ribbon-consumer,驗證ribbon客戶端的負載均衡

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