欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年03月12日 16:33:51   作者:godliu711  
這篇文章主要介紹了Spring Cloud Eureka 注冊與發(fā)現(xiàn)操作步驟詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

在這里插入圖片描述

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

在這里插入圖片描述

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

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

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

一、注冊中心

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

在這里插入圖片描述

以下是幾個需要修改和添加的地方,后面會有完整的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>

		<!-- 監(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 服務端包 -->
		<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
@SpringBootApplication
public 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

在這里插入圖片描述

啟動服務

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

在這里插入圖片描述

證明注冊中心搭建成功了

二、服務提供者

前面的步驟一樣

在這里插入圖片描述

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
@SpringBootApplication
public 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;

@RestController
public 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: 3001

eureka:
 instance:
 preferIpAddress: true
 client:
 serviceUrl:
 defaultZone: http://localhost:3000/eureka ## 注冊到 eureka 
spring:
 application:
 name: single-provider ## 應用程序名稱,后面會在消費者中用到

在這里插入圖片描述

http://127.0.0.1:3001/hello

在這里插入圖片描述

http://127.0.0.1:3000/

在這里插入圖片描述

證明服務已經(jīng)注冊成功

三、服務消費者

前面還是同上

在這里插入圖片描述

<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
@SpringBootApplication
public class EurekaClient2Application {

	/**
 * 注入 RestTemplate 
 * 并用 @LoadBalanced 注解,用負載均衡策略請求服務提供者
 * 這是 Spring Ribbon 的提供的能力
 * @return
 */
 @LoadBalanced
 @Bean
 public RestTemplate restTemplate() {
 return new RestTemplate(); //用于調(diào)用服務對象
 }
	
	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;

@RestController
public 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方式調(diào)用服務
 return s;
 }
}

application.yml

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

在這里插入圖片描述

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

在這里插入圖片描述

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

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

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

相關文章

  • java自動裝箱拆箱深入剖析

    java自動裝箱拆箱深入剖析

    基本數(shù)據(jù)(Primitive)類型的自動裝箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0開始提供的功能。java語言規(guī)范中說道:在許多情況下包裝與解包裝是由編譯器自行完成的(在這種情況下包裝成為裝箱,解包裝稱為拆箱)
    2012-11-11
  • springboot微服務項目集成html頁面的實現(xiàn)

    springboot微服務項目集成html頁面的實現(xiàn)

    本文主要介紹了springboot微服務項目集成html頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • java表單提交中文亂碼的解決方法

    java表單提交中文亂碼的解決方法

    這篇文章主要介紹了java表單提交中文亂碼的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • IDEA快速顯示Run DashBoard的圖文詳解

    IDEA快速顯示Run DashBoard的圖文詳解

    這篇文章主要介紹了IDEA快速顯示Run DashBoard的圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • SSH框架網(wǎng)上商城項目第11戰(zhàn)之查詢和刪除商品功能實現(xiàn)

    SSH框架網(wǎng)上商城項目第11戰(zhàn)之查詢和刪除商品功能實現(xiàn)

    這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第11戰(zhàn)之查詢和刪除商品功能實現(xiàn)的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 教你用Java SpringBoot如何解決跨域

    教你用Java SpringBoot如何解決跨域

    在項目開發(fā)中,時常會遇到跨域問題,本文主要介紹了五種解決跨域的方法,使用最多的是第三種,需要的朋友們下面隨著小編來一起學習學習吧
    2021-09-09
  • SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解

    SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解

    這篇文章主要為大家介紹了SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 詳解spring boot rest例子

    詳解spring boot rest例子

    這篇文章主要介紹了詳解spring boot rest例子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Hibernate三種狀態(tài)和Session常用的方法

    Hibernate三種狀態(tài)和Session常用的方法

    本文主要介紹了Hibernate三種狀態(tài)和Session常用的方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • javaWeb實現(xiàn)簡單文件上傳

    javaWeb實現(xiàn)簡單文件上傳

    這篇文章主要為大家詳細介紹了JAVAWeb實現(xiàn)簡單文件上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評論