Java框架原理后端相关

SpringBoot实例:医院统一信息平台(spring clo

2018-11-07  本文已影响527人  碧波之心

项目中加入统一配置,服务发现,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项目修改:

  1. 新建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
  1. 将application.yml移到huip-config/src/main/resources/config目录下,改名为huip-user-server.yml
    启动服务的顺序为:
  2. huip-eureka
    启动完成后,访问http://localhost:8761/
    eureka
    现在还没有服务被发现。
  3. huip-config
    启动完成后,在http://localhost:8761中就发现了一个服务
    配置中心服务
  4. 其它服务就无顺序要求了。这里就加入huip-user服务进行测试
    启动完成user服务


    用户服务

    再做前面用户服务的测试。得到相同的结果


    用户服务测试
上一篇下一篇

猜你喜欢

热点阅读