SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼
一、Spring Cloud簡(jiǎn)介
Spring Cloud是一個(gè)基千SpringBoot實(shí)現(xiàn)的微服務(wù)架構(gòu)開發(fā) 工具。它為微服務(wù)架構(gòu)中涉及的 配置管理、服務(wù)治理、 斷路器、 智能路由、微代理、 控制總線、 全局鎖、 決策競(jìng)選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開源產(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 通過為Eureka 增加了 Spring Boot 風(fēng)格的自動(dòng)化配置,我們只需通過簡(jiǎn)單引入依賴和注解配置就能讓 Spring Boot 構(gòu)建的微服務(wù)應(yīng)用輕松地與 Eureka 服務(wù)治理體系進(jìn)行整合。
服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊, 它主要用來實(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)用并訪問 http://localhost:5555/??梢钥吹饺缦聢D所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 欄是空的, 說明該注冊(cè)中心還沒有注冊(cè)任何服務(wù)。
(4)注冊(cè)服務(wù)提供者在完成了服務(wù)注冊(cè)中心的搭建之后,接下來我們嘗試將一個(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)訪問eurekaserver:(可以很清楚的看到HELLOSERVER信息)

(7-2)訪問helloserver:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Ribbon負(fù)載均衡器處理方法
這篇文章主要介紹了Spring Cloud Ribbon負(fù)載均衡器處理方法,看看是如何獲取服務(wù)實(shí)例,獲取以后做了哪些處理,處理后又是如何選取服務(wù)實(shí)例的,需要的朋友可以參考下2018-02-02
IDEA修改java文件后 不用重啟Tomcat服務(wù)便可實(shí)現(xiàn)自動(dòng)更新
這篇文章主要介紹了IDEA修改java文件后 不用重啟Tomcat服務(wù)便可實(shí)現(xiàn)自動(dòng)更新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換
這篇文章主要介紹了基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Java接口的回調(diào)機(jī)制的實(shí)現(xiàn)
本文主要介紹了Java中回調(diào)機(jī)制的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
java+SpringBoot設(shè)計(jì)實(shí)現(xiàn)評(píng)教系統(tǒng)
這篇文章主要介紹了用過Java SpringBoot實(shí)現(xiàn)一個(gè)簡(jiǎn)單的評(píng)價(jià)系統(tǒng),可以做到學(xué)生管理、教師管理、評(píng)教管理、指標(biāo)管理、課程管理等。感興趣的可以了解一下2021-12-12

