亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

最簡(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類上加:

@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類似,創(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)文章

  • SpringBoot圖文并茂詳解如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù)

    SpringBoot圖文并茂詳解如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù)

    這篇文章主要介紹了SpringBoot如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • 一文讀懂a(chǎn)va中的Volatile關(guān)鍵字使用

    一文讀懂a(chǎn)va中的Volatile關(guān)鍵字使用

    volatile關(guān)鍵字的作用保證了變量的可見(jiàn)性(visibility)。被volatile關(guān)鍵字修飾的變量,如果值發(fā)生了變更,其他線程立馬可見(jiàn),避免出現(xiàn)臟讀的現(xiàn)象。這篇文章主要介紹了ava中的Volatile關(guān)鍵字使用,需要的朋友可以參考下
    2020-03-03
  • 詳解JavaWeb過(guò)濾器 Filter問(wèn)題解決

    詳解JavaWeb過(guò)濾器 Filter問(wèn)題解決

    過(guò)濾器就是對(duì)事物進(jìn)行過(guò)濾的,在Web中的過(guò)濾器,當(dāng)然就是對(duì)請(qǐng)求進(jìn)行過(guò)濾,我們使用過(guò)濾器,就可以對(duì)請(qǐng)求進(jìn)行攔截,然后做相應(yīng)的處理,實(shí)現(xiàn)許多特殊功能,今天主要給大家講解JavaWeb過(guò)濾器 Filter問(wèn)題解決,感興趣的朋友一起看看吧
    2022-10-10
  • Java中線程上下文類加載器超詳細(xì)講解使用

    Java中線程上下文類加載器超詳細(xì)講解使用

    這篇文章主要介紹了Java中線程上下文類加載器,類加載器負(fù)責(zé)讀取Java字節(jié)代碼,并轉(zhuǎn)換成java.lang.Class類的一個(gè)實(shí)例的代碼模塊。本文主要和大家聊聊JVM類加載器ClassLoader的使用,需要的可以了解一下
    2022-12-12
  • java調(diào)用相互依賴的dll的處理方法

    java調(diào)用相互依賴的dll的處理方法

    大家好,本篇文章主要講的是java調(diào)用相互依賴的dll的處理方法,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java反射機(jī)制的講解

    Java反射機(jī)制的講解

    今天小編就為大家分享一篇關(guān)于Java反射機(jī)制的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • Java WindowBuilder 安裝及基本使用的教程

    Java WindowBuilder 安裝及基本使用的教程

    這篇文章主要介紹了Java WindowBuilder 安裝及基本使用的教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • java二維數(shù)組遍歷的2種代碼

    java二維數(shù)組遍歷的2種代碼

    這篇文章主要介紹了java二維數(shù)組遍歷的2種代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Java設(shè)置Access-Control-Allow-Origin允許多域名訪問(wèn)的實(shí)現(xiàn)方法

    Java設(shè)置Access-Control-Allow-Origin允許多域名訪問(wèn)的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java設(shè)置Access-Control-Allow-Origin允許多域名訪問(wèn)的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • spring整合redisson開(kāi)啟緩存方式

    spring整合redisson開(kāi)啟緩存方式

    這篇文章主要介紹了spring整合redisson開(kāi)啟緩存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論