Spring cloud Eureka
Eureka Server,注冊中心
Eureka Client,所有要進行注冊的微服務通過Eureka Client 連接到 Eureka Server ,完后注冊
一.創建父工程,pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.southwind</groupId>
<artifactId>aispringcloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eurekaserver</module>
<module>eurekaclient</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--解決 jdk 9 以上沒有 jaxb api 的問題-->
<!--<dependency>-->
<!--<groupId>javax.xml.bind</groupId>-->
<!--<artifactId>jaxb-api</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-impl</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-core</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>javax.activation</groupId>-->
<!--<artifactId>activation</artifactId>-->
<!--<version>1.1.1</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<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>
2.然后在父類里面創建兩個子工程
在第一個子項目中創建配置文件 application.yml 添加EUREKA Server相關配置
#端口號
server:
port: 7070
#是否在注冊中心把自己當成一個微服務注冊
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:9090/eureka/
屬性說明:
server.por:當前Eureka Server 服務端口
eureka.client.register-with-eureka:是否將當前的Eureka Server 服務作為客戶端進行注冊
eureka.client.fetch-fegistry:是否獲取其他Eureka Server 服務的數據
eureka.client.service-url.defaultzone:注冊中心訪問地址
性對應的pom.xml的配置:
<parent>
<artifactId>aispringcloud</artifactId>
<groupId>com.southwind</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaserver</artifactId>
<!--注冊中心-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
.創建啟動類
@SpringBootApplication //聲明該類是springboot的入口
@EnableEurekaServer //聲明該類少一個Eureka Server微服務,提供注冊和發現功能(注冊中心
public class EurekaServerApplication {
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
####### Eureka Client代碼的實現
1.創建 Module,pom.xml
第二個子項目中 pom.xml 的配置 yml 配置 啟動類配置
<parent>
<artifactId>aispringcloud</artifactId>
<groupId>com.southwind</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaclient</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
yml的配置:
server:
port: 9090
# 當前服務器注冊在Eureka Server上面的名字
spring:
application:
name: provider
# 注冊中心訪問的地址
eureka:
client:
service-url:
defaultZone: http://localhost:9090/eureka/
# 是否將當前服務的ip注冊到 Eureka Server
instance:
prefer-ip-address: true
實體類:
package com.ssouthwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by 86182 on 2019/7/29.
*/
@SpringBootApplication
public aspect ProviderAppliaction {
public static void main(String[] args){
SpringApplication.run(ProviderAppliaction.class,args);
}
}