亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Eureka 注冊與發現操作步驟詳解

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

在搭建Spring Cloud Eureka環境前先要了解整個架構的組成,常用的基礎模式如下圖:

Spring Cloud Eureka 注冊與發現操作步驟詳解

服務提供者:將springboot服務編寫好以后,通過配置注冊中心地址方式注冊,提供給消費者使用。注冊中心:服務的中間橋梁,服務提供者將服務注冊。服務消費者可以通過注冊信息調用需要使用的服務。服務消費者:通過規定的調用方式,讀取注冊中心的注冊信息,調用相應的服務。

Spring Cloud Eureka 注冊與發現操作步驟詳解

根據后續的服務復雜度進化以后,可以看到服務提供者也可以是服務消費者,服務消費者也可以是服務提供者。根據不同的業務情況是可以互相調用的。

下面來搭建一個基礎的eureka。環境還是使用的之前的spring官方下載的。

內容寫的比較詳細,可以跟這一步步操作。

一、注冊中心

Spring Cloud Eureka 注冊與發現操作步驟詳解Spring Cloud Eureka 注冊與發現操作步驟詳解Spring Cloud Eureka 注冊與發現操作步驟詳解

Spring Cloud Eureka 注冊與發現操作步驟詳解

以下是幾個需要修改和添加的地方,后面會有完整的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 服務端包 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 監控管理 --><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 服務端包 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 監控管理 --><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: 注冊中心 client: registerWithEureka: false # 單點的時候設置為 false 禁止注冊自身 fetchRegistry: false serviceUrl: defaultZone: http://localhost:3000/eureka server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000

Spring Cloud Eureka 注冊與發現操作步驟詳解

啟動服務

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

Spring Cloud Eureka 注冊與發現操作步驟詳解

證明注冊中心搭建成功了

二、服務提供者

前面的步驟一樣

Spring Cloud Eureka 注冊與發現操作步驟詳解

ProviderController是新建的一個服務代碼

<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>

不同的地方有加了一個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 ## 注冊到 eureka spring: application: name: single-provider ## 應用程序名稱,后面會在消費者中用到

Spring Cloud Eureka 注冊與發現操作步驟詳解

http://127.0.0.1:3001/hello

Spring Cloud Eureka 注冊與發現操作步驟詳解

http://127.0.0.1:3000/

Spring Cloud Eureka 注冊與發現操作步驟詳解

證明服務已經注冊成功

三、服務消費者

前面還是同上

Spring Cloud Eureka 注冊與發現操作步驟詳解

<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新增以上內容

完整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 注解,用負載均衡策略請求服務提供者 * 這是 Spring Ribbon 的提供的能力 * @return */ @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); //用于調用服務對象 }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';//服務注冊名 @RequestMapping(value = 'commonRequest') public Object commonRequest(){ String url = 'http://'+ applicationName +'/hello'; String s = restTemplate.getForObject(url,String.class);//Ribbon方式調用服務 return s; }}

application.yml

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

Spring Cloud Eureka 注冊與發現操作步驟詳解

啟動服務http://127.0.0.1:3002/commonRequest

Spring Cloud Eureka 注冊與發現操作步驟詳解

返回服務提供者的 hello方法參數。

整個最簡單的過程就完成了,需要更好的使用spring cloud 后續需要了解分布式、負載均衡、熔斷等概念。在后續章節將一步步的拆分。

到此這篇關于Spring Cloud Eureka 注冊與發現操作步驟詳解的文章就介紹到這了,更多相關Spring Cloud Eureka 注冊內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 免费看的黄色小视频 | 午夜久久久久久亚洲国产精品 | 任你躁欧美一级在线精品免费 | 亚洲国产成人在线 | 亚洲一级在线 | 深夜免费福利视频在线播放 | 九九热视频精品在线观看 | 国产播放啪视频免费视频 | 91最新网站免费 | 国产亚洲欧美日韩在线观看一区二区 | 制服丝袜中文字幕第一页 | freexnxx日本欧美18 | 91视频18| 综合亚洲欧美 | 高清不卡一区二区 | 国产欧美日韩免费一区二区 | 免费特级毛片 | 国产不卡a | 成人国产精品免费视频 | 成人性视频免费网站 | 日本一级特黄毛片免费视频9 | 国产大学生真实在线播放 | 欧洲成人全免费视频网站 | 色播亚洲精品网站 亚洲第一 | 在线亚洲精品自拍 | 在线观看精品国产 | 国产成人吃奶一区 | 人人草人人澡 | 免费a黄色| 国产在线免| 五十路一区二区三区视频 | 性生生活网站免费 | a级免费看| 欧美高清免费一级在线 | 欧美精品久久久久久久影视 | 亚洲国产精品自产拍在线播放 | 欧美日韩国产在线观看一区二区三区 | 亚洲精品一区二区三区四区手机版 | 国产第一区精品视频ai换脸 | 久久精品免视看国产成人2021 | 亚洲一区二区三区欧美 |