国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

瀏覽:16日期:2023-07-20 09:34:49

在搭建Spring Cloud Eureka環(huán)境前先要了解整個(gè)架構(gòu)的組成,常用的基礎(chǔ)模式如下圖:

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

服務(wù)提供者:將springboot服務(wù)編寫好以后,通過(guò)配置注冊(cè)中心地址方式注冊(cè),提供給消費(fèi)者使用。注冊(cè)中心:服務(wù)的中間橋梁,服務(wù)提供者將服務(wù)注冊(cè)。服務(wù)消費(fèi)者可以通過(guò)注冊(cè)信息調(diào)用需要使用的服務(wù)。服務(wù)消費(fèi)者:通過(guò)規(guī)定的調(diào)用方式,讀取注冊(cè)中心的注冊(cè)信息,調(diào)用相應(yīng)的服務(wù)。

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

根據(jù)后續(xù)的服務(wù)復(fù)雜度進(jìn)化以后,可以看到服務(wù)提供者也可以是服務(wù)消費(fèi)者,服務(wù)消費(fèi)者也可以是服務(wù)提供者。根據(jù)不同的業(yè)務(wù)情況是可以互相調(diào)用的。

下面來(lái)搭建一個(gè)基礎(chǔ)的eureka。環(huán)境還是使用的之前的spring官方下載的。

內(nèi)容寫的比較詳細(xì),可以跟這一步步操作。

一、注冊(cè)中心

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

以下是幾個(gè)需要修改和添加的地方,后面會(huì)有完整的pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 最新版的 eureka 服務(wù)端包 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 監(jiān)控管理 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency> Spring Cloud Spring Boot Angel版本 兼容Spring Boot 1.2.x Brixton版本 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x Camden版本 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x Dalston版本、Edgware版本 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x Finchley版本 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x Greenwich版本 兼容Spring Boot 2.1.x

這里采用Finchley.SR2版本 springboot版本改成 2.1.3.RELEASE

完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eureka1</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- 最新版的 eureka 服務(wù)端包 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 監(jiān)控管理 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

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

然后在代碼文件中添加@EnableEurekaServer注解

修改application.yml文件

server: port: 3000 # 端口eureka: instance: hostname: eureka-center appname: 注冊(cè)中心 client: registerWithEureka: false # 單點(diǎn)的時(shí)候設(shè)置為 false 禁止注冊(cè)自身 fetchRegistry: false serviceUrl: defaultZone: http://localhost:3000/eureka server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

啟動(dòng)服務(wù)

瀏覽器輸入 http://127.0.0.1:3000

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

證明注冊(cè)中心搭建成功了

二、服務(wù)提供者

前面的步驟一樣

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

ProviderController是新建的一個(gè)服務(wù)代碼

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- eureka 客戶端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

不同的地方有加了一個(gè)spring-boot-starter-web和spring-cloud-starter-netflix-eureka-client客戶端完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eurekaClient1</artifactId><version>0.0.1-SNAPSHOT</version><name>eurekaClient1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- eureka 客戶端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

EurekaClient1Application

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplicationpublic class EurekaClient1Application {public static void main(String[] args) {SpringApplication.run(EurekaClient1Application.class, args);}}

ProviderController

package com.example.demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ProviderController { @RequestMapping(value = '/hello') public String hello(){ return 'hello spring cloud!'; } @RequestMapping(value = '/nice') public String nice(){ return 'nice to meet you!'; }}

application.yml

server: port: 3001eureka: instance: preferIpAddress: true client: serviceUrl: defaultZone: http://localhost:3000/eureka ## 注冊(cè)到 eureka spring: application: name: single-provider ## 應(yīng)用程序名稱,后面會(huì)在消費(fèi)者中用到

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

http://127.0.0.1:3001/hello

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

http://127.0.0.1:3000/

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

證明服務(wù)已經(jīng)注冊(cè)成功

三、服務(wù)消費(fèi)者

前面還是同上

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- eureka 客戶端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

pom.xml新增以上內(nèi)容

完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eurekaClient1</artifactId><version>0.0.1-SNAPSHOT</version><name>eurekaClient1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- eureka 客戶端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

EurekaClient2Application

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableEurekaClient@EnableFeignClients@SpringBootApplicationpublic class EurekaClient2Application {/** * 注入 RestTemplate * 并用 @LoadBalanced 注解,用負(fù)載均衡策略請(qǐng)求服務(wù)提供者 * 這是 Spring Ribbon 的提供的能力 * @return */ @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); //用于調(diào)用服務(wù)對(duì)象 }public static void main(String[] args) {SpringApplication.run(EurekaClient2Application.class, args);}}

ConsumerController

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class ConsumerController {@Autowired private RestTemplate restTemplate; private static final String applicationName = 'single-provider';//服務(wù)注冊(cè)名 @RequestMapping(value = 'commonRequest') public Object commonRequest(){ String url = 'http://'+ applicationName +'/hello'; String s = restTemplate.getForObject(url,String.class);//Ribbon方式調(diào)用服務(wù) return s; }}

application.yml

server: port: 3002eureka: client: serviceUrl: defaultZone: http://127.0.0.1:3000/eureka ## 注冊(cè)到 eureka instance: preferIpAddress: truespring: application: name: single-customer

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

啟動(dòng)服務(wù)http://127.0.0.1:3002/commonRequest

Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解

返回服務(wù)提供者的 hello方法參數(shù)。

整個(gè)最簡(jiǎn)單的過(guò)程就完成了,需要更好的使用spring cloud 后續(xù)需要了解分布式、負(fù)載均衡、熔斷等概念。在后續(xù)章節(jié)將一步步的拆分。

到此這篇關(guān)于Spring Cloud Eureka 注冊(cè)與發(fā)現(xiàn)操作步驟詳解的文章就介紹到這了,更多相關(guān)Spring Cloud Eureka 注冊(cè)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产一区二区中文字幕 | 久久手机在线视频 | 成人免费国产欧美日韩你懂的 | 欧美日本国产 | 视频二区国产 | 亚洲一区二区三区免费在线观看 | 91精品欧美综合在线观看 | 欧美日韩一级二级三级 | 精品国产综合区久久久久久 | 日本高清va不卡视频在线观看 | 国产毛片久久国产 | 啪啪一级片 | 免费一级做a爰片性色毛片 免费一极毛片 | 三级午夜三级三点在看 | 国产精品久久久久久久毛片 | 99re热视频这里只精品 | 国产真实乱子伦精品视手机观看 | 国产成人18黄网站免费网站 | 国产人成午夜免视频网站 | 亚洲精品亚洲一区二区 | 成人免费视频在 | 黄色美女免费看 | 久久91精品国产91久久跳舞 | 日韩一级特黄毛片在线看 | 制服诱惑中文字幕 | 亚洲毛片免费在线观看 | 国产高清自拍视频 | 玖玖玖精品视频免费播放 | 99精品在线 | 国产精品二区三区免费播放心 | 久久精品在线观看 | 韩国毛片基地 | fc2成年手机免费共享视频 | 久久精品中文字幕一区 | 国产美女在线精品亚洲二区 | 亚洲精品久久久久午夜三 | 深夜爽爽爽gif福利免费 | 国产精品欧美日韩 | 国产精品黄在线观看免费软件 | 欧美成人午夜做爰视频在线观看 | 国产黄毛片 |