1. 程式人生 > 其它 >nacos Spring cloud 服務配置與呼叫(postgresql & spring cloud)

nacos Spring cloud 服務配置與呼叫(postgresql & spring cloud)

前提 serviceA 呼叫 serviceB

配置

<-- 父工程引入-->
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.0.RELEASE</version>
            </dependency>
        </dependencies>
</dependencyManagement>

<-- serviceB -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
<-- serviceA  按理說子工程不需要版本號,估計是父工程的版本不支援? 後續有時間再調查-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
      <-- openfeign-->
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

seriviceA Application

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) // 不需要資料庫

serviceB Applicaiton

@EnableDiscoveryClient
@SpringBootApplication

serviceA bootstrap.yml

spring:
  application:
    name: service-A
  cloud:
    nacos:
      discovery:
        server-addr: http://xxx.xxx.xxx.xx:8848
server:
  port: 7294
management:
  endpoints:
    web:
      exposure:
        include: '*'

serviceB bootstrap.yml

spring:
  application:
    name: service-B
  cloud:
    nacos:
      discovery:
        server-addr: http://xxx.xxx.xxx.xx:8848
      config:
        server-addr: http://xxx.xxx.xxx.xx:8848
        file-extension: yaml
server:
  port: 7293
management:
  endpoints:
    web:
      exposure:
        include: '*'
~~~yml
### serviceB application.yml
~~~yml
spring:
    profiles:
      active: dev

nacos 配置 serviceB 配置資訊service-B-dev.yaml

spring:
  datasource:
    name: pg
    url: jdbc:postgresql://xxx.xxx.xxx.xx/postgres
    username: 
    password: 
    # 使用druid資料來源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.postgresql.Driver
mybatis-plus: # 以下均可刪除,是mybatis-plus 的一些配置
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 這個是列印sql日誌的,可以刪除
  type-handlers-package: com.xxx.xxx.typehandlers # 這個是型別轉換需要的,可以刪除
  global-config: # 這些是配置全域性預設值的,可以刪除
    db-config:
      logic-delete-field: deleteStatus
      logic-delete-value: 1
      logic-not-delete-value: 0
~~~yaml
通過知識/經驗的分享,節省開發者的時間.