項目中加入統(tǒng)一配置问畅,服務(wù)發(fā)現(xiàn)艾栋,api路由等云服務(wù)功能爆存。這里采用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>
服務(wù)發(fā)現(xiàn)(eureka)
創(chuàng)建項目huip-eureka
pom.xml
這是eureka服務(wù)提供項目闲勺,需要包含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霉翔。內(nèi)容如下:
server:
port: 8761
spring:
application:
name: huip-eureka-server
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8886} # 統(tǒng)一配置服務(wù)地址
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
統(tǒng)一配置服務(wù)(config)
創(chuàng)建項目huip-config
pom.xml
這是config服務(wù)提供項目睁蕾,需要包含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服務(wù)中序芦,則active的改成git臭杰,配置相應的uri,username,password連接git。內(nèi)容如下:
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谚中。把各項目的配置文件放置到這個目錄下渴杆。
運行服務(wù)
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
啟動服務(wù)的順序為: - huip-eureka
啟動完成后宪塔,訪問http://localhost:8761/
現(xiàn)在還沒有服務(wù)被發(fā)現(xiàn)磁奖。 - huip-config
啟動完成后,在http://localhost:8761中就發(fā)現(xiàn)了一個服務(wù)
-
其它服務(wù)就無順序要求了某筐。這里就加入huip-user服務(wù)進行測試
啟動完成user服務(wù)
再做前面用戶服務(wù)的測試比搭。得到相同的結(jié)果