服務治理 SpringCloud Eureka

服務治理:在傳統rpc遠程調用中,服務與服務依賴關係,管理比較複雜,因此須要使用服務治理,管理服務與服務之間依賴關係,能夠實現服務調用、負載均衡、容錯等,實現服務發現與註冊。java

 

服務註冊與發現:在服務註冊與發現中,有一個註冊中心,當服務器啓動的時候,會把當前本身服務器的信息好比服務地址通信等以別名方式註冊到註冊中心上。另外一方(服務消費者|服務提供者),以該別名的方式去註冊中心上獲取到實際的服務通信地址,而後再實現本地rpc調用遠程。web

 

服務提供者:指提供服務接口spring

服務消費者:指調用接口進行使用緩存

 

EurekaEureka服務端和Eureka客戶端兩個組件組成,Eureka服務端用做服務註冊中心。支持集羣部署。Eureka客戶端是一個java客戶端,用來處理服務註冊與發現。在應用啓動時,Eureka客戶端向服務端註冊本身的服務信息,同時將服務端信息緩存到本地。客戶端會和服務端週期性的進行心跳交互,以更新服務租約和服務信息。服務器

 

自我保護機制:默認狀況下EurekaClient定時向EurekaServer端發送心跳包。若是EurekaServer在必定的時間內沒有收到EurekaClient發送心跳包,便會直接從服務註冊列表中剔除該服務(默認90秒內)。可是在短期丟失了大量的服務實例心跳,這時候EurekaServer會開啓自我保護機制,不會去剔除該服務。網絡

 

在自我保護機制,爲何Eureka不會剔除該服務呢?app

爲了防止EurekaClient是能夠正常訪問,但只是EurekaClientEurekaServer網絡訪問不通。防止誤剔除。(建議在本地環境禁止自我保護機制,在生產環境開啓自我保護機制)負載均衡

eureka:
  server:
    ###測試時關閉自我保護機制,保證不可用服務及時踢出
    enable-self-preservation: false
    ###剔除失效服務間隔
    eviction-interval-timer-in-ms: 2000

 

搭建註冊中心spring-boot

pom文件中Maven依賴信息測試

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依賴 -->
  <dependencyManagement>
      <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.M7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <!-- SpringCloud eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>
  
  <!-- 注意:這裏必需要添加,不然各類依賴有問題 -->
  <repositories>
      <repository>
          <id>spring-milestones</id>
          <name>Spring Milestones</name>
          <url>https://repo.spring.io/libs-milestone</url>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
      </repository>
  </repositories>

配置文件(application.yml)

###服務端口號
server:
  port: 8100
eureka:
  instance:
    ###註冊中心ip地址
    hostname: 127.0.0.1
  client:
    service-url:
      ###註冊中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    ###由於本身是註冊中心,是否須要將本身註冊給本身的註冊中心(集羣的時候是須要是爲true)      
    register-with-eureka: false
    ###由於本身是註冊中心,不須要去檢索服務信息
    fetch-registry: false

測試類

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

@SpringBootApplication
@EnableEurekaServer //表示開啓EurekaServer服務,開啓註冊中心
public class AppEureka {
    public static void main(String[] args) {
        SpringApplication.run(AppEureka.class, args);
    }
}

搭建完成!

 

註冊服務提供者

pom文件中Maven依賴信息

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依賴 -->
  <dependencyManagement>
      <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.M7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
  </dependencyManagement>
  
  <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>
  </dependencies>
  
  <!-- 注意:這裏必需要添加,不然各類依賴有問題 -->
  <repositories>
      <repository>
          <id>spring-milestones</id>
          <name>Spring Milestones</name>
          <url>https://repo.spring.io/libs-milestone</url>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
      </repository>
  </repositories>

配置文件(application.yml)

###會員項目的端口號
server:
  port: 8000
###服務別名(服務註冊到註冊中心名稱)
spring:
  application:
    name: app-hclz-member 
eureka:
  client:
    service-url:
      ###當前會員服務註冊到eureka服務地址
      defaultZone: http://localhost:8100/eureka
    ###須要將個人服務註冊到eureka上
    register-with-eureka: true
    ###須要檢索服務
    fetch-registry: true

主類

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

@SpringBootApplication
@EnableEurekaClient //將當前服務註冊到eureka上
public class AppMember {
    public static void main(String[] args) {
        SpringApplication.run(AppMember.class, args);
    }
}

controller

@RestController
public class MemberApiController {

    @RequestMapping("/getMember")
    public String getMember() {
        return "this is member,我是會員服務,springcloud2.0版本";
    }
}

啓動註冊中心後啓動服務提供者

搭建服務提供者註冊到Eureka成功!

 

註冊服務消費者

pom文件中Maven依賴信息與服務提供者一致

配置文件(application.yml)

###訂單服務的端口號
server:
  port: 8001
###服務別名(服務註冊到註冊中心名稱)
spring:
  application:
    name: app-hclz-order 
eureka:
  client:
    service-url:
      ###當前會員服務註冊到eureka服務地址
      defaultZone: http://localhost:8100/eureka
    ###須要將個人服務註冊到eureka上
    register-with-eureka: true
    ###須要檢索服務
    fetch-registry: true

controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {
    
    //RestTemplate是由SpringBoot Web組件提供,默認整合ribbon負載均衡器
    //rest方式底層是採用httpclient技術
    @Autowired
    private RestTemplate restTemplate;

    //訂單服務調用會員服務
    @RequestMapping("/getOrder")
    public String getOrder() {
        String url = "http://app-hclz-member/getMember";
        String result = restTemplate.getForObject(url, String.class);
        System.out.println("訂單服務調用會員服務result:"+result);
        return result;
    }
}

主類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class AppOrder {
    public static void main(String[] args) {
        SpringApplication.run(AppOrder.class, args);
    }
        
    @Bean  //把RestTemplate註冊到SpringBoot容器中,若是使用rest方式以別名方式進行調用,依賴ribbon負載均衡器@LoadBalanced
    @LoadBalanced  //@LoadBalanced開啓以別名方式去Eureka讀取註冊信息,而後本地實現rpc遠程調用
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

啓動註冊中心接着啓動服務提供者而後啓動服務消費者

搭建服務消費者註冊到Eureka成功!

相關文章
相關標籤/搜索