服務(wù)治理:Spring Cloud Eureka

服務(wù)治理

主要作用是實(shí)現(xiàn)各個(gè)微服務(wù)實(shí)例的自動(dòng)化注冊(cè)與發(fā)現(xiàn)。

服務(wù)注冊(cè)

在服務(wù)治理框架中,通常會(huì)構(gòu)建一個(gè)注冊(cè)中心,每個(gè)服務(wù)單元向注冊(cè)中心登記自己提供的服務(wù),將主機(jī)與端口號(hào)、版本號(hào)、通信協(xié)議等一些附加信息告知注冊(cè)中心,注冊(cè)中心按照服務(wù)名分類組織服務(wù)清單。

服務(wù)發(fā)現(xiàn)

由于在服務(wù)治理框架下運(yùn)作,服務(wù)間的調(diào)用不再通過指定具體的實(shí)例地址來發(fā)現(xiàn),而是通過向服務(wù)名發(fā)起請(qǐng)求調(diào)用實(shí)現(xiàn)。所以,服務(wù)調(diào)用在調(diào)用服務(wù)提供方接口的時(shí)候,并不知道具體的服務(wù)實(shí)例位置。因此,調(diào)用方需要向服務(wù)注冊(cè)中心咨詢服務(wù),并獲取所有服務(wù)的實(shí)例清單,以實(shí)現(xiàn)對(duì)具體服務(wù)實(shí)例的訪問

Netfix Eureka

Eureka服務(wù)端,我們也稱為服務(wù)注冊(cè)中心,支持高可用配置,它依托于強(qiáng)一致性提供良好的服務(wù)實(shí)例可用性,可以應(yīng)對(duì)多種不同的故障場(chǎng)景。
Eureka客戶端,主要處理服務(wù)的注冊(cè)與發(fā)現(xiàn)??蛻舳朔?wù)通過注解和參數(shù)配置的方式,嵌入在客戶端應(yīng)用程序的代碼中,在應(yīng)用程序運(yùn)行時(shí),Eureka客戶端向注冊(cè)中心注冊(cè)自身的提供的服務(wù)并周期性地發(fā)送心跳來更新它的服務(wù)租約。

搭建服務(wù)注冊(cè)中心

在pom.xml中引入必要的依賴內(nèi)容

<dependencies>
  <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-s 七 arter-eureka-server</artifactid>
  </dependency>
</dependencies>
<dependencyManagemen七>
  <dependencies>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-dependencies</artifactid>
      <version>Brixton.SRS</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagemen七>

通過@EnableEurekaServer注解啟動(dòng)一個(gè)注冊(cè)服務(wù)中心提供給其他應(yīng)用進(jìn)行對(duì)話.

@EnableEurekaServer
@SpringBoo七Application
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class) .web(七rue).run (args);
    }
}

在默認(rèn)設(shè)置下,該服務(wù)注冊(cè)中心將自己作為客戶端來嘗試注冊(cè)他自己,所以我們需要禁用它的客戶端注冊(cè)行為,需要在application.properties中增加以下配置:

server. port=l 111
eureka.instance.hostname = localhost
#是否設(shè)置為注冊(cè)中心
eureka.client.register-with-eureka=false
eureka.client.fetch-registry = false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${serve
r.port}/eureka/

注冊(cè)服務(wù)提供者

新建一個(gè)新的微服務(wù)@EnableEurekaServer
@SpringBoo七Application
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class) .web(七rue).run (args);
}
}應(yīng)用,接著,修改pom.xml文件,增加Spring Cloud Eureka模塊的依賴

<dependencies>
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boo七-starter-web</artifactid>
  </dependency>
  <dependency>
    <group工d>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-test</artifactid>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-eureka</artifactid>
  </dependency>
</dependencies>

<dependencyManagemen七>
  <dependencies>
    <dependency>
      <group工d>org.springframework.cloud</groupid>
      <artifactId>spring-cloud-dependencies</artifac七Id>
      <version>Brixton.SRS</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

然后,改造請(qǐng)求處理的接口,注入DiscoveryClient對(duì)象,

@RestController
public class HelloController {
  private final Logger logger=Logger.getLogger(getClass());
  @Autowired
  private DiscoveryClient client;
  @RequestMapping(value = "/hello", me七h(yuǎn)od= RequestMethod.GET )
  public String index() {
    Service Instance instance = client.getLocalServiceinstance();
    logger.info("/hello, host:" + instance.getHost());
    return "Hello World";
  }
}

然后在主類中加上@EnableDiscoveryClient注解,激活Eureka中的DiscoveryClient實(shí)現(xiàn)。

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class) .web(七rue).run (args);
    }
}

最后,我們需要在application.properties配置文件中,通過spring.application.name屬性來為服務(wù)命名,再通過eureka.client.serviceUrl.defaultZone屬性指定服務(wù)注冊(cè)中心

spring.application.name = hello-service
eureka.client.serviceUrl.defaultZone = http://localhost:llll/eureka/

高可用注冊(cè)中心

Eureka Server的高可用實(shí)際上就是將自己作為服務(wù)像其他服務(wù)注冊(cè)中心注冊(cè)自己,這樣就形成一組互相注冊(cè)的服務(wù)注冊(cè)中心,以實(shí)現(xiàn)服務(wù)清單的互相同步,達(dá)到高可用的效果。

服務(wù)發(fā)現(xiàn)與消費(fèi)

服務(wù)發(fā)現(xiàn)的任務(wù)由Eureka完成,服務(wù)消費(fèi)的任務(wù)由Ribbon完成。Ribbon是一個(gè)基于HTTP和TCP的客戶端負(fù)載均衡器,它可以在通過客戶端配置的ribbonServerList服務(wù)列表去輪詢?cè)L問以達(dá)到均衡負(fù)載的作用。
在Eureka的服務(wù)治理體系下實(shí)現(xiàn)服務(wù)的發(fā)現(xiàn)與消費(fèi):
-啟動(dòng)兩個(gè)服務(wù)注冊(cè)中心:java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8081,java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8082
-創(chuàng)建Spring Boot的基礎(chǔ)工程來實(shí)現(xiàn)服務(wù)消費(fèi)者,在pom文件內(nèi)添加如下依賴:

<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifac七Id>
    <version>l.3.7.RELEASE</version>
    <relativePath/> <1-- lookup paren七 from repository -->
</parent>
<dependencies>
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifac七Id>
  </dependency>
<!--添加eureka依賴-->
  <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-eureka</artifactid>
   </dependency>
<!--添加ribbon依賴-->
   <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-ribbon</artifactid>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-dependencies</artifactid>
      <version>Brixton.SRS</version>
      <type>pom</type>
      <scope>impor七</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

創(chuàng)建應(yīng)用主類ConsumerApplication,通過@EnableDiscoveryClient注解讓該應(yīng)用為Eureka客戶端應(yīng)用,以獲得服務(wù)發(fā)現(xiàn)的能力,同時(shí)在該主類中創(chuàng)建RestTemplate的Spring Bean實(shí)例,并通過@LoadBalanced注解開啟客戶端負(fù)載均衡。

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
     return new RestTemplate();
  }
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }
}

-創(chuàng)建ConsumerController類并實(shí)現(xiàn)/ribbon-consumer接口。在該接口中,通過在上面創(chuàng)建的RestTemplate來實(shí)現(xiàn)對(duì)HELLO-SERVICE服務(wù)提供的/hello接口進(jìn)行調(diào)用。

@RestController
public class ConsumerController {
  @Autowired
  RestTemplate restTemplate;

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

-在application.properties中配置Eureka服務(wù)注冊(cè)中心的位置,,需要與之前的HELLO-SERVICE的ip一樣,否則將無法發(fā)現(xiàn)該服務(wù)。

spring.application.name=ribbon-consumer
server.port=9000

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

-向htttp://localhost:9000/ribbon-consumer發(fā)起GET請(qǐng)求,成功則返回Hello World。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容