Spring Cloud | 第一篇: 服務的註冊與發現(Eureka)

一、SpringCloud簡介

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙地簡化了分佈式系統基礎設施的開發,如服務發現註冊、配置中心、消息總線、負載均衡、斷路器、數據監控等,都可以用Spring Boot的開發風格做到一鍵啓動和部署。Spring Cloud並沒有重複製造輪子,它只是將目前各家公司開發的比較成熟、經得起實際考驗的服務框架組合起來,通過Spring Boot風格進行再封裝屏蔽掉了複雜的配置和實現原理,最終給開發者留出了一套簡單易懂、易部署和易維護的分佈式系統開發工具包。

二、構建SpirngCloud當中的Eureka模塊

我們在上一篇文章當中講解了如何在一個SpringBoot應用當中構建多模塊應用,這邊接上一篇的代碼繼續進行。假如想這樣構建,首先需要去看一下上一篇《SpringBoot多模塊項目的創建和配置》。


我們可以看到,我們在其中構建了三個模塊,分別爲Mall_EurekaCenter、Mall_ManageService和Mall_WechatService模塊。

現在,我們要實現的任務是把Mall_EurekaCenter當作微服務的服務註冊中心,把其他的兩個服務分別向註冊中心進行註冊。

第一步:配置Mall_EurekaCenter模塊(服務註冊中心)

首先我們在父工程的pom文件當中引入依賴

<?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.smartisan</groupId>
	<artifactId>RediaMallCloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!--父模塊打包類型必須爲pom-->
	<packaging>pom</packaging>

	<!--在父pom文件當中添加模塊的名稱,子模塊pom中<name>標籤當中的值-->
	<modules>
		<module>Mall_EurekaCenter</module>
		<module>Mall_ManagerService</module>
		<module>Mall_WechatService</module>
	</modules>

	<name>RediaMallCloud</name>
	<description>Demo project for Spring Boot</description>

	<!-- parent指明繼承關係,給出被繼承的父項目的具體信息-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.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>
		<!--與《SpringBoot多模塊項目的創建和配置》當中唯一區別點當中的版本管理信息-->
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
		<!-- 在properties中統一控制依賴包的版本,更清晰-->
		<dubbo.version>2.5.3</dubbo.version>
		<zkclient.version>0.10</zkclient.version>
	</properties>

	<!--需要使用SpringCloud組件,首先要在pom文件當中加入下面的這個依賴,這個是與《SpringBoot多模塊項目的創建和配置》當中的唯一區別點-->
	<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>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

然後在Mall_EurekaCenter模塊當中的pom文件引入依賴

<?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.smartisan</groupId>
	<artifactId>malleurekacenter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Mall_EurekaCenter</name>
	<description>Demo project for Spring Boot</description>

	<!-- 把原有的parent的信息註釋掉,繼承我們的父工程-->
	<parent>
		<!--<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/>--> <!-- lookup parent from repository -->
		<groupId>com.smartisan</groupId>
		<artifactId>RediaMallCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

在MallEurekaCenterApplication類上面加上@EnableEurekaServer註解,聲明這是一個EurekaServer。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class MallEurekaCenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(MallEurekaCenterApplication.class, args);
	}
}

然後在Mall_EurekaCenter模塊當中編寫application.yml文件的信息:

#註冊中心應用名稱
spring:
  application:
      name: eureka-server
#eureka.server.enableSelfPreservation:是否向註冊中心註冊自己
#通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server.
eureka:
  server:
      enableSelfPreservation: false
  instance:
      hostname: localhost
#      prefer-ip-address: true
#      preferIpAddress: true
#      ip-address: 172.193.225.185
#      instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  client:
      fetch-registry: false
      register-with-eureka: false
      service-url:
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

這個時候我們啓動Mall_EurekaCenter這個工程,打開瀏覽器,輸入:http://localhost:8761/,界面如下,發現還沒有服務實例出現(因爲我們還沒有向註冊中心註冊服務)。


第二步:編寫服務提供者(這裏編寫Mall_ManagerService模塊,Mall_WechatService模塊相同)並向註冊中心進行註冊。

首先編寫服務提供者的pom文件

<?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.smartisan</groupId>
	<artifactId>mallmanagerservice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Mall_ManagerService</name>
	<description>Demo project for Spring Boot</description>

	<!-- 把原有的parent的信息註釋掉,繼承我們的父工程-->
	<parent>
		<!--<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/>--> <!-- lookup parent from repository -->
		<groupId>com.smartisan</groupId>
		<artifactId>RediaMallCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<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>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

在主類上面增加@EnableEurekaClient註解,聲明自己是一個服務提供者

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class MallManagerServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(MallManagerServiceApplication.class, args);
	}
}

編寫服務提供者的application.yml文件

#服務提供者的運行端口
server:
  port: 8011
#服務提供者的應用名稱,http://localhost:8761/頁面當中會顯示出來
spring:
  application:
    name: mall-managerservice

#用戶配置開發(dev),測試(test),生產(prod)的配置文件,避免頻繁改動
  profiles:
    active: dev
#打包之前註釋掉
eureka:
  client:
    serviceUrl:
       defaultZone: http://admin:password@localhost:8761/eureka/
  instance:
    prefer-ip-address: true

按照上面的步驟,編寫Mall_WechatService模塊,這樣我們編寫好了兩個服務提供者,運行之後,我們在Mall_EurekaCenter的運行日誌當中看到如下信息:


打開瀏覽器,輸入:http://localhost:8761/,界面如下,發現兩個服務實例出現


經過以上步驟,我們就聲明瞭兩個微服務並且向註冊中心進行了註冊。

整個工程的結構如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章