欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法

 更新時(shí)間:2018年05月02日 09:20:15   作者:JAVA開發(fā)老菜鳥  
這篇文章主要介紹了spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊,它主要用來實(shí)現(xiàn)各個(gè)微服務(wù)實(shí)例的自動(dòng)化注冊和發(fā)現(xiàn)。

Spring Cloud Eureka是Spring Cloud Netflix 微服務(wù)套件的一部分,主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能。

本文通過簡單的小例子來分享下如何通過Eureka進(jìn)行服務(wù)治理:

  1. 搭建服務(wù)注冊中心
  2. 注冊服務(wù)提供者
  3. 服務(wù)發(fā)現(xiàn)和消費(fèi)

==========我是華麗的分割線========================

一、搭建服務(wù)注冊中心

先列出完整目錄結(jié)構(gòu):

搭建過程如下:

1.創(chuàng)建maven工程:eureka(具體實(shí)現(xiàn)略)

2.修改pom文件,引入依賴

<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">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sam</groupId>
 <artifactId>eureka</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
 </parent>

 <properties>
  <javaVersion>1.8</javaVersion>
 </properties>
 <!-- 使用dependencyManagement進(jìn)行版本管理 -->
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>

 <dependencies>
  <!-- 引入eureka server依賴 -->
  <dependency>   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
 </dependencies>
</project>

3.創(chuàng)建啟動(dòng)類

/**
 * 
 * @EnableEurekaServer
 * 用來指定該項(xiàng)目為Eureka的服務(wù)注冊中心
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp {

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

4.配置application.properties文件

#設(shè)置tomcat服務(wù)端口號(hào)
server.port=1111
#設(shè)置服務(wù)名稱
spring.application.name=eureka-service

eureka.instance.hostname=localhost
#注冊中心不需要注冊自己
eureka.client.register-with-eureka=false
#注冊中心不需要去發(fā)現(xiàn)服務(wù)
eureka.client.fetch-registry=false
#設(shè)置服務(wù)注冊中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

5.啟動(dòng)服務(wù)并訪問,我們會(huì)看到這樣的畫面:

二、注冊服務(wù)提供者

先列出完整目錄結(jié)構(gòu):

搭建過程如下:

1.創(chuàng)建maven工程:hello-service(具體實(shí)現(xiàn)略)

2.修改pom文件,引入依賴

<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">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sam</groupId>
 <artifactId>hello-service</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
 </parent>

 <properties>
  <javaVersion>1.8</javaVersion>
 </properties>

 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>

 <dependencies>
  <!-- 引入eureka 客戶端依賴 -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
 </dependencies>
</project>

3.創(chuàng)建啟動(dòng)類

/***
 * 
 * @EnableDiscoveryClient
 * 讓服務(wù)使用eureka服務(wù)器
 * 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApp {
 public static void main(String[] args) {
  SpringApplication.run(HelloApp.class, args);
 }
}

4.創(chuàng)建controller

@RestController
public class HelloController {

 Logger logger = LoggerFactory.getLogger(HelloController.class);

 @Autowired
 DiscoveryClient discoveryClient;
 
 @RequestMapping("/hello")
 public String hello() {
  ServiceInstance instance = discoveryClient.getLocalServiceInstance();
  //打印服務(wù)的服務(wù)id
  logger.info("*********" + instance.getServiceId());
  return "hello,this is hello-service";
 }
}

5.配置application.properties文件

server.port=9090
#設(shè)置服務(wù)名
spring.application.name=hello-service
#設(shè)置服務(wù)注冊中心的URL,本服務(wù)要向該服務(wù)注冊中心注冊自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

6.啟動(dòng)并測試

1.)啟動(dòng)后再hello-service的控制臺(tái)會(huì)有這種字樣(xxx代表你的PC名)

復(fù)制代碼 代碼如下:
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

eureka的控制臺(tái)會(huì)打印出如下字樣(xxx代表你的PC名)

復(fù)制代碼 代碼如下:
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

2.)再次訪問localhost:1111,會(huì)發(fā)現(xiàn)有服務(wù)注冊到注冊中心了

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

完整目錄結(jié)構(gòu)如下:

搭建過程:

1.創(chuàng)建maven工程(具體實(shí)現(xiàn)略)

2.修改pom文件,引入依賴

<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">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sam</groupId>
 <artifactId>hello-consumer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
 </parent>

 <properties>
  <javaVersion>1.8</javaVersion>
 </properties>

 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>

 </dependencyManagement>

 <dependencies>
  <!-- 引入eureka 客戶端依賴 -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <!-- 引入ribbon 依賴 ,用來實(shí)現(xiàn)負(fù)載均衡,我們這里只是使用,先不作其他介紹-->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>

 </dependencies>
</project>

這里比hello-service服務(wù)提供者,多了ribbon的依賴

3.創(chuàng)建啟動(dòng)類

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {


 //@Bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean
 @Bean
 @LoadBalanced //@LoadBalanced實(shí)現(xiàn)負(fù)載均衡
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }
 
 public static void main(String[] args) {
  SpringApplication.run(ConsumerApp.class, args);
 }
}

這里也要用到@EnableDiscoveryClient, 讓服務(wù)使用eureka服務(wù)器, 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn) 

4.創(chuàng)建controller

@RestController
public class ConsumerController {
 //這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實(shí)例
 @Autowired
 RestTemplate restTemplate;
 @RequestMapping("/hello-consumer")
 public String helloConsumer() {
  //調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port
  restTemplate.getForObject("http://hello-service/hello", String.class);
  return "hello consumer finish !!!";
 }
}

5.配置application.properties文件

server.port=9999
spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
#這里的配置項(xiàng)目和服務(wù)提供者h(yuǎn)ello-service一樣

6.啟動(dòng),測試1.)啟動(dòng)eureka。為了展示負(fù)責(zé)均衡的效果,我們的hello-service啟動(dòng)兩個(gè)服務(wù),啟動(dòng)兩個(gè)服務(wù)的具體步驟如下

以上是hello-service1的啟動(dòng)步驟,端口號(hào)為9090;同樣方法設(shè)置hello-service2,端口號(hào)為9091(具體實(shí)現(xiàn)略)。

2.)啟動(dòng)hello-consumer

3.)再次訪問http://localhost:1111/,會(huì)發(fā)現(xiàn)有2個(gè)hello-service服務(wù)(端口號(hào)一個(gè)是9090,一個(gè)是9091),1個(gè)hello-consume服務(wù)

4.) 多次訪問http://localhost:9999/hello-consumer,會(huì)發(fā)現(xiàn)hello-service1和hello-service2會(huì)輪流被調(diào)用(已經(jīng)實(shí)現(xiàn)了負(fù)責(zé)均衡),可以通過兩者的控制臺(tái)打印內(nèi)容確認(rèn)(還記得我們在hello-service的controller中有個(gè)loggerlogger.info("*********" + instance.getServiceId());嗎?對(duì),就是這個(gè)打印)

四、總結(jié)

以上實(shí)例實(shí)現(xiàn)了基本的服務(wù)治理:

  1. 通過spring-cloud-starter-eureka-server和@EnableEurekaServer實(shí)現(xiàn)服務(wù)注冊中心
  2. 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注冊到服務(wù)注冊中心
  3. 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用注冊中心并發(fā)現(xiàn)服務(wù),通過spring-cloud-starter-ribbon來實(shí)現(xiàn)負(fù)載均衡消費(fèi)服務(wù)

PS:這里說明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我們使用spring進(jìn)行開發(fā),有興趣的朋友可以自行百度了解下。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 總結(jié)Bean的三種自定義初始化和銷毀方法

    總結(jié)Bean的三種自定義初始化和銷毀方法

    這篇文章主要介紹了Bean的三種自定義初始化和銷毀方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù)功能

    Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù)功能

    這篇文章主要介紹了Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • java實(shí)現(xiàn)任意矩陣Strassen算法

    java實(shí)現(xiàn)任意矩陣Strassen算法

    這篇文章主要介紹了java實(shí)現(xiàn)任意矩陣Strassen算法的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Java線程池獲取池中所有線程列表的方法總結(jié)

    Java線程池獲取池中所有線程列表的方法總結(jié)

    在Java中,獲取線程池中所有線程列表并不是一個(gè)直接支持的功能,因?yàn)榫€程池的設(shè)計(jì)通常是為了隱藏和管理底層的線程細(xì)節(jié),從而提供更高層次的抽象和并發(fā)控制能力,本文給大家介紹了Java線程池獲取池中所有線程列表的方法,需要的朋友可以參考下
    2024-10-10
  • Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法

    Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法

    這篇文章主要介紹了Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-02-02
  • Java魔法值處理的四種方式

    Java魔法值處理的四種方式

    這篇文章主要介紹了Java魔法值處理的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)

    SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • SpringBoot中WEB的啟動(dòng)流程分析

    SpringBoot中WEB的啟動(dòng)流程分析

    今天我們就來分析下springboot啟動(dòng)web項(xiàng)目整個(gè)流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-03-03
  • JAVA模擬多線程給多用戶發(fā)送短信

    JAVA模擬多線程給多用戶發(fā)送短信

    這篇文章主要介紹了JAVA模擬多線程給多用戶發(fā)送短信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • java Object wait方法詳細(xì)介紹

    java Object wait方法詳細(xì)介紹

    這篇文章主要介紹了java Object wait方法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評(píng)論