最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊,它主要用來實(shí)現(xiàn)各個微服務(wù)實(shí)例的自動化注冊和發(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)建啟動類

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

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

4.配置application.properties文件

#設(shè)置tomcat服務(wù)端口號
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.啟動服務(wù)并訪問,我們會看到這樣的畫面:

二、注冊服務(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)建啟動類

/***
 * 
 * @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.啟動并測試

1.)啟動后再hello-service的控制臺會有這種字樣(xxx代表你的PC名)

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

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

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

2.)再次訪問localhost:1111,會發(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)建啟動類

@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
#這里的配置項目和服務(wù)提供者h(yuǎn)ello-service一樣

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

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

2.)啟動hello-consumer

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

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

四、總結(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot數(shù)據(jù)庫操作圖文教程

    springboot數(shù)據(jù)庫操作圖文教程

    本文以圖文并茂的形式給大家介紹了springboot數(shù)據(jù)庫操作,感興趣的朋友一起看看吧
    2017-07-07
  • Java 8 對 HashSet 元素進(jìn)行排序的操作方法

    Java 8 對 HashSet 元素進(jìn)行排序的操作方法

    Java 中HashSet是一個不保證元素順序的集合類,其內(nèi)部是基于 HashMap 實(shí)現(xiàn)的,HashSet不支持排序,我們在需要對HashSet 排序時,必須將其轉(zhuǎn)換為支持排序的集合或數(shù)據(jù)結(jié)構(gòu),如 List,本文將詳細(xì)介紹在 Java 8 中如何對 HashSet 中的元素進(jìn)行排序,感興趣的朋友一起看看吧
    2024-11-11
  • Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決

    Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決

    這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • java實(shí)現(xiàn)微信App支付服務(wù)端

    java實(shí)現(xiàn)微信App支付服務(wù)端

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信App支付服務(wù)端,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • springCloud項目搭建流程步驟分解

    springCloud項目搭建流程步驟分解

    SpringCloud 作為當(dāng)下最為流行的微服務(wù)框架,也越來越多的人去學(xué)習(xí)和使用這個框架。下面,我將帶大家簡單地認(rèn)識一下 SpringCloud 框架,以及如何來搭建一個 SpringCloud 項目環(huán)境的教程
    2022-05-05
  • Java SE求解漢諾塔問題的示例代碼

    Java SE求解漢諾塔問題的示例代碼

    漢諾塔問題是一個經(jīng)典的問題。漢諾塔(Hanoi Tower),又稱河內(nèi)塔,源于印度一個古老傳說。本文將用Java SE求解這一問題,感興趣的可以學(xué)習(xí)一下
    2022-03-03
  • spring security與corsFilter沖突的解決方案

    spring security與corsFilter沖突的解決方案

    這篇文章主要介紹了spring security與corsFilter沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java利用httpclient通過get、post方式調(diào)用https接口的方法

    Java利用httpclient通過get、post方式調(diào)用https接口的方法

    這篇文章主要介紹了Java利用httpclient通過get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Java流處理stream使用詳解

    Java流處理stream使用詳解

    Java8的另一大亮點(diǎn)Stream,它與java.io包里的InputStream和OutputStream是完全不同的概念,下面這篇文章主要給大家介紹了關(guān)于Java8中Stream詳細(xì)使用方法的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • 堆排序原理及算法代碼詳解

    堆排序原理及算法代碼詳解

    這篇文章主要介紹了堆排序算法的講解及Java版實(shí)現(xiàn),堆排序基于堆這種數(shù)據(jù)結(jié)構(gòu),在本文中對堆的概念也有補(bǔ)充介紹,需要的朋友可以參考下
    2021-08-08

最新評論

黄龙县| 大方县| 和硕县| 和田县| 永康市| 奉新县| 丽江市| 固安县| 什邡市| 金塔县| 和顺县| 孝感市| 姚安县| 吉木萨尔县| 佛坪县| 安乡县| 桃源县| 枣阳市| 六盘水市| 大新县| 合阳县| 崇礼县| 屯门区| 天门市| 怀远县| 庆云县| 望奎县| 新绛县| 正阳县| 淮安市| 成安县| 千阳县| 长子县| 安多县| 体育| 修文县| 昌邑市| 五峰| 韶关市| 临沧市| 久治县|