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

SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼

 更新時(shí)間:2018年04月08日 09:49:33   作者:smartdt  
這篇文章主要介紹了SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一、Spring Cloud簡(jiǎn)介

Spring Cloud是一個(gè)基千SpringBoot實(shí)現(xiàn)的微服務(wù)架構(gòu)開(kāi)發(fā) 工具。它為微服務(wù)架構(gòu)中涉及的 配置管理、服務(wù)治理、 斷路器、 智能路由、微代理、 控制總線、 全局鎖、 決策競(jìng)選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開(kāi)發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開(kāi)源產(chǎn)品,還可能會(huì)新增),如下所述。

Spring Cloud Config: 配置管理工具、Spring Cloud Netflix: 核心組件、Spring Cloud Bus: 事件、消息總線等等。

二、Spring Cloud Eureka

Spring Cloud Eureka 是 Spring Cloud Netflix 微服務(wù)套件中的一部分, 它基于 NetflixEureka 做了二次封裝, 主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能。 Spring Cloud 通過(guò)為Eureka 增加了 Spring Boot 風(fēng)格的自動(dòng)化配置,我們只需通過(guò)簡(jiǎn)單引入依賴和注解配置就能讓 Spring Boot 構(gòu)建的微服務(wù)應(yīng)用輕松地與 Eureka 服務(wù)治理體系進(jìn)行整合。

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

三、實(shí)例

(1)工具:IntelliJ IDEA

(2)新建一個(gè)空項(xiàng)目


(3)新建一個(gè)Eureka Server,Module,名為:eurekaserver

工程右鍵->創(chuàng)建Module-> 選擇spring initialir ->填好項(xiàng)目名,下一步->,如圖選擇Eureka Server:

(3-1)pom.xml

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-web</artifactId> 
</dependency> 
 
<dependency> 
  <groupId>org.springframework.cloud</groupId> 
  <artifactId>spring-cloud-starter-eureka-server</artifactId> 
</dependency> 
 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
  <scope>test</scope> 
</dependency> 

(3-2)application.yml

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

備注:eureka.client.register-with-eureka: 由于該應(yīng)用為注冊(cè)中心,所以設(shè)置為 false, 代表不向注冊(cè)中心注冊(cè)自己。
eureka.client.fetch-registry: 由于注冊(cè)中心的職責(zé)就是維護(hù)服務(wù)實(shí)例,它并不需要去檢索服務(wù), 所以也設(shè)置為 false。

(3-3)入口類

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 
 
@EnableEurekaServer 
@SpringBootApplication 
public class EurekaserverApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(EurekaserverApplication.class, args); 
  } 
} 

(3-4)啟動(dòng)測(cè)試


在完成了上面的配置后,啟動(dòng)應(yīng)用并訪問(wèn) http://localhost:5555/。可以看到如下圖所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 欄是空的, 說(shuō)明該注冊(cè)中心還沒(méi)有注冊(cè)任何服務(wù)。

(4)注冊(cè)服務(wù)提供者在完成了服務(wù)注冊(cè)中心的搭建之后,接下來(lái)我們嘗試將一個(gè)既有的 Spring Boot 應(yīng)用加入 Emeka 的服務(wù)治理體系中去。

(5)再新建一個(gè)Eureka Client,Module,名為:helloserver,這個(gè)helloserver作為eurekaserver的子model


(6)改造父model和子model的pom配置(6-1)eurekaserver的pom.xml配置:

<packaging>pom</packaging> 
<modules> 
  <module>../helloserver</module> 
</modules> 

(6-2)eurekaserver的全部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.example</groupId> 
  <artifactId>demoeurekaserver</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>pom</packaging> 
  <modules> 
    <module>../helloserver</module> 
  </modules> 
  <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.9.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> 
    <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> 
  </properties> 
 
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
 
    <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-eureka-server</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>${spring-cloud.version}</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> 
</project> 

(6-3)helloserver的pom.xml配置:

<parent> 
  <groupId>com.example</groupId> 
  <artifactId>demoeurekaserver</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <relativePath>../eurekaserver/pom.xml</relativePath> 
</parent> 

(6-4)helloserver的全部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> 
  <parent> 
    <groupId>com.example</groupId> 
    <artifactId>demoeurekaserver</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <relativePath>../eurekaserver/pom.xml</relativePath> 
  </parent> 
  <artifactId>helloserver</artifactId> 
  <packaging>jar</packaging> 
  <name>helloserver</name> 
  <description>Demo project for Spring Boot</description> 
  <properties> 
    <start-class>com.example.helloserver.HelloserverApplication</start-class> 
  </properties> 
  <dependencies> 
    <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>${spring-cloud.version}</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> 
</project> 

(6-5)helloserver的application.yml配置:

server: 
 port: 5556 
 
spring: 
 application: 
  name: helloserver 
eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://localhost:5555/eureka/ 

(6-6)helloserver的啟動(dòng)類:

@EnableEurekaServer 
@SpringBootApplication 
@RestController 
public class HelloserverApplication { 
  private final Logger log = (Logger) LoggerFactory.getLogger(HelloserverApplication.class); 
  @Autowired 
  private DiscoveryClient client; 
 
  @RequestMapping(name = "/hello", method = RequestMethod.GET) 
  public String index() { 
    ServiceInstance instance = client.getLocalServiceInstance(); 
    log.info("/hello, host:" + instance.getHost() + ",service_id:" + instance.getServiceId()); 
    return "Hello SpringCloud~"; 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(HelloserverApplication.class, args); 
  } 
} 

(7)分別啟動(dòng)eurekaserver和helloserver,并測(cè)試:

(7-1)訪問(wèn)eurekaserver:(可以很清楚的看到HELLOSERVER信息)


(7-2)訪問(wèn)helloserver:


(7-3)查看helloserver控制臺(tái)信息:


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

相關(guān)文章

  • Java中的八種基本數(shù)據(jù)類型詳解

    Java中的八種基本數(shù)據(jù)類型詳解

    本文詳細(xì)講解了Java中的八種基本數(shù)據(jù)類型,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法小結(jié)

    Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法小結(jié)

    這篇文章主要介紹了Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式總結(jié)分析了java基于數(shù)學(xué)運(yùn)算與判斷實(shí)現(xiàn)不重復(fù)隨機(jī)數(shù)的生成功能,需要的朋友可以參考下
    2017-07-07
  • 后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)

    后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)

    x-www-form-urlencoded格式是一種常見(jiàn)的HTTP請(qǐng)求數(shù)據(jù)格式,它將請(qǐng)求參數(shù)編碼為鍵值對(duì)的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下
    2023-05-05
  • java實(shí)現(xiàn)簡(jiǎn)易外賣訂餐系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)易外賣訂餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易外賣訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot整合Canal與RabbitMQ監(jiān)聽(tīng)數(shù)據(jù)變更記錄

    SpringBoot整合Canal與RabbitMQ監(jiān)聽(tīng)數(shù)據(jù)變更記錄

    這篇文章主要介紹了SpringBoot整合Canal與RabbitMQ監(jiān)聽(tīng)數(shù)據(jù)變更記錄,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Java Map 按照Value排序的實(shí)現(xiàn)方法

    Java Map 按照Value排序的實(shí)現(xiàn)方法

    Map是鍵值對(duì)的集合接口,它的實(shí)現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-08-08
  • Java數(shù)據(jù)結(jié)構(gòu)之順序表和鏈表精解

    Java數(shù)據(jù)結(jié)構(gòu)之順序表和鏈表精解

    我在學(xué)習(xí)完順序表后一直對(duì)順序表和鏈表的概念存在一些疑問(wèn),這里給出一些分析和看法,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java中如何獲取當(dāng)前服務(wù)器的IP地址

    Java中如何獲取當(dāng)前服務(wù)器的IP地址

    這篇文章主要給大家介紹了關(guān)于Java中如何獲取當(dāng)前服務(wù)器的IP地址的相關(guān)資料,我們可以使用Java中的InetAddress類來(lái)獲取Linux服務(wù)器的IP地址,需要的朋友可以參考下
    2023-07-07
  • Java編程簡(jiǎn)單應(yīng)用

    Java編程簡(jiǎn)單應(yīng)用

    本文主要介紹了三個(gè)簡(jiǎn)單Java小程序———1、HelloWorld(HelloWorld的來(lái)源);2、輸出個(gè)人信息3、輸出特殊圖案。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • Java網(wǎng)絡(luò)編程實(shí)例——簡(jiǎn)單模擬在線聊天

    Java網(wǎng)絡(luò)編程實(shí)例——簡(jiǎn)單模擬在線聊天

    學(xué)了java網(wǎng)絡(luò),也是該做個(gè)小案例來(lái)鞏固一下了。本次案例將使用UDP和多線程模擬即時(shí)聊天,簡(jiǎn)單練練手。
    2021-05-05

最新評(píng)論

襄垣县| 盐城市| 镶黄旗| 江达县| 浦北县| 隆昌县| 得荣县| 奇台县| 陈巴尔虎旗| 福鼎市| 藁城市| 定州市| 景宁| 香港 | 岢岚县| 宁都县| 德兴市| 北票市| 微博| 榆林市| 井研县| 廉江市| 合肥市| 柳林县| 延寿县| 永春县| 临漳县| 凯里市| 建德市| 阿克苏市| 南澳县| 江阴市| 堆龙德庆县| 康乐县| 九寨沟县| 西林县| 望都县| 镇平县| 常熟市| 临高县| 二手房|