項目中加入統一配置,服務發現,api路由等云服務功能。這里采用spring cloud。
修改/huip/pom.xml
在這里,把各個項目都要用到的配置模塊,eureka模塊包含進來。也可以各個子項目中分別包含。
<?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.biboheart</groupId>
<artifactId>huip</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>Hospital unified information platform</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<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-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
... 略 ...
</dependencies>
</project>
服務發現(eureka)
創建項目huip-eureka
創建項目
pom.xml
這是eureka服務提供項目,需要包含spring-cloud-starter-netflix-eureka-server模塊。spring cloud Finchley之后才有這個模塊。
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.biboheart</groupId>
<artifactId>huip</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>huip-eureka</artifactId>
<name>huip-eureka</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
在src/main/resources中新建文件bootstrap.yml。內容如下:
server:
port: 8761
spring:
application:
name: huip-eureka-server
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8886} # 統一配置服務地址
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.host:localhost}:${server.port:8761}/eureka
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
統一配置服務(config)
創建項目huip-config
創建項目
pom.xml
這是config服務提供項目,需要包含spring-cloud-config-server組件。
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.biboheart</groupId>
<artifactId>huip</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>huip-config</artifactId>
<name>huip-config</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
在src/main/resources中新建文件bootstrap.yml。這里,用本地路徑配置。如果把配置文件放置到git服務中,則active的改成git,配置相應的uri,username,password連接git。內容如下:
server:
port: 8886
spring:
application:
name: huip-config-server
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/config
# git:
# uri: *****************************
# username: *********
# password: *********
management:
security:
enabled: false
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
statusPageUrlPath: /admin/info
healthCheckUrlPath: /admin/health
non-secure-port: ${server.port:8886}
prefer-ip-address: true
metadata-map:
instanceI-i: ${spring.application.name}:${random.value}
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka
在src/main/resources下新建文件夾config。把各項目的配置文件放置到這個目錄下。
運行服務
huip-user項目修改:
- 新建bootstrap.yml
spring:
application:
name: huip-user-server
cloud:
config:
enabled: true
discovery:
enabled: true
service-id: huip-config-server
eureka:
instance:
non-secure-port: ${server.port:8180}
prefer-ip-address: true
metadata-map:
instance-id: ${spring.application.name}:${random.value}
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka
- 將application.yml移到huip-config/src/main/resources/config目錄下,改名為huip-user-server.yml
啟動服務的順序為: - huip-eureka
啟動完成后,訪問http://localhost:8761/
eureka
現在還沒有服務被發現。 - huip-config
啟動完成后,在http://localhost:8761中就發現了一個服務
配置中心服務 -
其它服務就無順序要求了。這里就加入huip-user服務進行測試
啟動完成user服務
用戶服務
再做前面用戶服務的測試。得到相同的結果
用戶服務測試