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

最簡(jiǎn)單的Spring Cloud教程第一篇:服務(wù)的注冊(cè)與發(fā)現(xiàn)(Eureka)

 更新時(shí)間:2017年08月22日 10:15:54   作者:方志朋  
這篇文章主要給大家介紹了關(guān)于Spring Cloud服務(wù)的注冊(cè)與發(fā)現(xiàn)(Eureka)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring cloud具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

本文主要給大家介紹關(guān)于Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)(Eureka)的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹:

一、spring cloud簡(jiǎn)介

spring cloud 為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)的一些工具,包括配置管理、服務(wù)發(fā)現(xiàn)、斷路器、路由、微代理、事件總線、全局鎖、決策競(jìng)選、分布式會(huì)話等等。它運(yùn)行環(huán)境簡(jiǎn)單,可以在開(kāi)發(fā)人員的電腦上跑。另外說(shuō)明spring cloud是基于springboot的,所以需要開(kāi)發(fā)中對(duì)springboot有一定的了解,如果不了解的話可以看這篇文章。另外對(duì)于“微服務(wù)架構(gòu)” 不了解的話,可以通過(guò)搜索引擎搜索“微服務(wù)架構(gòu)”了解下。

二、創(chuàng)建服務(wù)注冊(cè)中心

在這里,我們需要用的的組件上Spring Cloud Netflix的Eureka ,eureka是一個(gè)服務(wù)注冊(cè)和發(fā)現(xiàn)模塊。

2.1 首先創(chuàng)建一個(gè)maven主工程。

2.2 然后創(chuàng)建2個(gè)model工程:一個(gè)model工程作為服務(wù)注冊(cè)中心,即Eureka Server,另一個(gè)作為Eureka Client。

下面以創(chuàng)建server為例子,詳細(xì)說(shuō)明創(chuàng)建過(guò)程:

右鍵工程->創(chuàng)建model-> 選擇spring initialir 如下圖:

下一步->選擇cloud discovery->eureka server ,然后一直下一步就行了。

創(chuàng)建完后的工程的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.forezp</groupId>
 <artifactId>eurekaserver</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>eurekaserver</name>
 <description>Demo project for Spring Boot</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.2.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 </properties>

 <dependencies>
 <!--eureka server -->
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka-server</artifactId>
 </dependency>

 <!-- spring boot test-->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
 </dependencies>

 <dependencyManagement>
 <dependencies>
  <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-dependencies</artifactId>
  <version>Dalston.RC1</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>

 <repositories>
 <repository>
  <id>spring-milestones</id>
  <name>Spring Milestones</name>
  <url>https://repo.spring.io/milestone</url>
  <snapshots>
  <enabled>false</enabled>
  </snapshots>
 </repository>
 </repositories>


</project>

2.3 啟動(dòng)一個(gè)服務(wù)注冊(cè)中心,只需要一個(gè)注解@EnableEurekaServer,這個(gè)注解需要在springboot工程的啟動(dòng)application類(lèi)上加:

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

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

**2.4 **eureka是一個(gè)高可用的組件,它沒(méi)有后端緩存,每一個(gè)實(shí)例注冊(cè)之后需要向注冊(cè)中心發(fā)送心跳(因此可以在內(nèi)存中完成),在默認(rèn)情況下erureka server也是一個(gè)eureka client ,必須要指定一個(gè) server。eureka server的配置文件appication.yml:

server:
 port: 8761

eureka:
 instance:
 hostname: localhost
 client:
 registerWithEureka: false
 fetchRegistry: false
 serviceUrl:
 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

通過(guò)eureka.client.registerWithEureka:falsefetchRegistry:false來(lái)表明自己是一個(gè)eureka server.

2.5 eureka server 是有界面的,啟動(dòng)工程,打開(kāi)瀏覽器訪問(wèn):

http://localhost:8761 ,界面如下:

No application available 沒(méi)有服務(wù)被發(fā)現(xiàn) ……^_^

因?yàn)闆](méi)有注冊(cè)服務(wù)當(dāng)然不可能有服務(wù)被發(fā)現(xiàn)了。

三、創(chuàng)建一個(gè)服務(wù)提供者 (eureka client)

當(dāng)client向server注冊(cè)時(shí),它會(huì)提供一些元數(shù)據(jù),例如主機(jī)和端口,URL,主頁(yè)等。Eureka server 從每個(gè)client實(shí)例接收心跳消息。 如果心跳超時(shí),則通常將該實(shí)例從注冊(cè)server中刪除。

創(chuàng)建過(guò)程同server類(lèi)似,創(chuàng)建完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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.forezp</groupId>
 <artifactId>service-hi</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>service-hi</name>
 <description>Demo project for Spring Boot</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.2.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 </properties>

 <dependencies>
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka</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>
 </dependency>
 </dependencies>

 <dependencyManagement>
 <dependencies>
  <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-dependencies</artifactId>
  <version>Dalston.RC1</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>

 <repositories>
 <repository>
  <id>spring-milestones</id>
  <name>Spring Milestones</name>
  <url>https://repo.spring.io/milestone</url>
  <snapshots>
  <enabled>false</enabled>
  </snapshots>
 </repository>
 </repositories>


</project>

通過(guò)注解@EnableEurekaClient 表明自己是一個(gè)eurekaclient.

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

 @Value("${server.port}")
 String port;
 @RequestMapping("/hi")
 public String home(@RequestParam String name) {
 return "hi "+name+",i am from port:" +port;
 }

}

僅僅@EnableEurekaClient是不夠的,還需要在配置文件中注明自己的服務(wù)注冊(cè)中心的地址,application.yml配置文件如下:

eureka:
 client:
 serviceUrl:
 defaultZone: http://localhost:8761/eureka/
server:
 port: 8762
spring:
 application:
 name: service-hi

需要指明spring.application.name,這個(gè)很重要,這在以后的服務(wù)與服務(wù)之間相互調(diào)用一般都是根據(jù)這個(gè)name 。

啟動(dòng)工程,打開(kāi)http://localhost:8761 ,即eureka server 的網(wǎng)址:

你會(huì)發(fā)現(xiàn)一個(gè)服務(wù)已經(jīng)注冊(cè)在服務(wù)中了,服務(wù)名為SERVICE-HI ,端口為7862

這時(shí)打開(kāi) http://localhost:8762/hi?name=forezp ,你會(huì)在瀏覽器上看到 :

hi forezp,i am from port:8762

源碼下載:https://github.com/forezp/SpringCloudLearning/tree/master/chapter1

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

參考資料

springcloud eureka server 官方文檔

springcloud eureka client 官方文檔

相關(guān)文章

  • Java利用EasyExcel讀取寫(xiě)入Excel詳情

    Java利用EasyExcel讀取寫(xiě)入Excel詳情

    這篇文章主要介紹了Java利用EasyExcel讀取寫(xiě)入Excel詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 關(guān)于idea引入spring boot <parent></parent>父依賴(lài)標(biāo)紅問(wèn)題

    關(guān)于idea引入spring boot <parent></parent>父依賴(lài)標(biāo)紅問(wèn)題

    這篇文章主要介紹了idea引入spring boot <parent></parent>父依賴(lài)標(biāo)紅問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • java限流算法詳細(xì)

    java限流算法詳細(xì)

    這篇文章詳細(xì)的介紹了java限流算法常用到的算法計(jì)數(shù)算法、漏桶算法、令牌桶等算法的相關(guān)資料,需要的朋友可以參考下文,希望本篇文章能幫助到您
    2021-09-09
  • springmvc 防止表單重復(fù)提交的兩種方法

    springmvc 防止表單重復(fù)提交的兩種方法

    最近在本地開(kāi)發(fā)測(cè)試的時(shí)候,遇到一個(gè)表單重復(fù)提交的現(xiàn)象。本文主要介紹了springmvc 防止表單重復(fù)提交的兩種方法,感興趣的可以了解一下
    2021-08-08
  • C++享元模式詳解

    C++享元模式詳解

    這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之享元模式Flyweight,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • mybatis一對(duì)多方式實(shí)現(xiàn)批量插入

    mybatis一對(duì)多方式實(shí)現(xiàn)批量插入

    這篇文章主要介紹了mybatis一對(duì)多方式實(shí)現(xiàn)批量插入,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring JPA配置文件Eclipse報(bào)錯(cuò)如何解決

    Spring JPA配置文件Eclipse報(bào)錯(cuò)如何解決

    這篇文章主要介紹了Spring JPA配置文件Eclipse報(bào)錯(cuò)如何解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot整合Redis將對(duì)象寫(xiě)入redis的實(shí)現(xiàn)

    SpringBoot整合Redis將對(duì)象寫(xiě)入redis的實(shí)現(xiàn)

    本文主要介紹了SpringBoot整合Redis將對(duì)象寫(xiě)入redis的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • spring boot整合netty的實(shí)現(xiàn)方法

    spring boot整合netty的實(shí)現(xiàn)方法

    這篇文章主要介紹了spring boot整合netty的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解Java枚舉與接口常量和類(lèi)常量的區(qū)別

    詳解Java枚舉與接口常量和類(lèi)常量的區(qū)別

    這篇文章主要為大家詳細(xì)介紹了Java中枚舉與接口常量、類(lèi)常量有什么區(qū)別,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-10-10

最新評(píng)論

方山县| 高台县| 崇左市| 乌拉特前旗| 鲁山县| 连江县| 潜山县| 安庆市| 秦皇岛市| 邳州市| 二手房| 同仁县| 红原县| 济宁市| 上林县| 昭平县| 金山区| 裕民县| 伊宁县| 芦溪县| 环江| 德庆县| 准格尔旗| 扶风县| 磐石市| 桃源县| 张家港市| 兰州市| 青冈县| 安乡县| 清原| 泽州县| 富阳市| 太和县| 嘉禾县| 措勤县| 平利县| 宽甸| 凉山| 南召县| 类乌齐县|