IDEA項目搭建六——使用Eureka進行項目服務化
阿新 • • 發佈:2018-06-27
技術 啟動 消費者 ade 簡單 服務 fig 設置 pid
一、Eureka的作用
這裏先簡單說明使用eureka進行業務層隔離,實現項目服務化也可以理解為微服務,我一直崇尚先實現代碼再學習理論,先簡單上手進行操作,eureka使用分為三塊,1是服務註冊中心,2是服務生產模塊,3是服務消費模塊
關系調用說明:
- 服務生產者啟動時,向服務註冊中心註冊自己提供的服務
- 服務消費者啟動時,在服務註冊中心訂閱自己所需要的服務
- 註冊中心返回服務提供者的地址信息給消費者
- 消費者從提供者中調用服務
服務生產者會每30s發送心跳,向服務中心續租服務有效時間,當一段時間生產者沒有向服務中心續租將會被移出服務提供註冊表
二、搭建eurkea服務註冊中心
新建Project或者Module,選擇Maven結構
簡單看一下我的模塊結構
1、修改pom.xml文件,增加springboot和springcloud的配置,由於使用的最新的spring boot所以spring cloud也是使用最新版本,註意這裏的version是Finchley.RELEASE
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2、新建resources文件夾並在File -> Project Structure 中設置為Resources格式,並增加application.yml格式文件,也可用properties格式
spring: application: #Eureka服務註冊中心名稱 name: javademo-tyh-eureka-server server: #服務註冊中心端口號 port: 11000 eureka: instance: #服務註冊中心主機名 hostname: localhost client: #是否向服務註冊中心註冊自己 register-with-eureka: false #是否檢索服務 fetch-registry: false #服務註冊中心的地址 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、在main方法的類中增加註解,從main方法中寫spring boot的運行代碼
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class AppEureka { public static void main( String[] args ) { SpringApplication.run(AppEureka.class, args); } }
OK,現在eurkea的服務註冊中心就配置完畢了,從main這裏啟動一下,瀏覽器中訪問http://localhost:11000就能看到運行狀態界面了,如果端口號沖突自己修改一下即可
三、搭建後端服務生產模塊
四、搭建前端服務消費模塊
IDEA項目搭建六——使用Eureka進行項目服務化