pig4cloud框架系列四:去nacos服务

前言:由于公司的要求不同,比如有些公司使用K8s部署项目,来替代nacos实现服务发现功能,所以本文简单记录一下如何去掉nacos

pig4cloud官网拉取的demo是复合工程,我本地是先把复合工程拆分为多个独立的工程,然后再实现的去掉nacos服务。

独立出来的工程主要有,pig-auth、pig-upms、pig-security、pig-gateway、pig-log、pig-common等,具体的拆分步骤可参考网上的例子。

一,pig-gateway 服务调整
1,找到 pig-gateway服务的pom.xml文件,将nacos的maven相关依赖去掉,同时添加bootstrap依赖(不添加启动会报缺少config:import set 找不到,其他的服务模块也需要添加)。
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
2,将启动类上的@EnableDiscoveryClient去掉

3,将原有nacos配置中心上的配置文件迁移到application.yml文件中并修改

server:
  port: 9999

spring:
  application:
    name: @artifactId@
  cloud:
    gateway:
      routes:
        # 认证中心
        - id: pig-auth
          uri: http://pig-auth:3000
          predicates:
            - Path=/auth/**
          filters:
            # 验证码处理
            - ValidateCodeGatewayFilter
            # 前端密码解密
            - PasswordDecoderFilter
        #UPMS 模块
        - id: pig-upms-biz
          uri: http://pig-upms-biz:4000
          predicates:
            - Path=/admin/**
          filters:
            # 限流配置
            - name: RequestRateLimiter
              args:
                key-resolver: '#{@remoteAddrKeyResolver}'
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
        # 代码生成模块
        - id: pig-codegen
          uri: http://pig-codegen:5002
          predicates:
            - Path=/gen/**
        # 代码生成模块
        - id: pig-quartz
          uri: http://pig-quartz:5007
          predicates:
            - Path=/job/**
        # 固定路由转发配置 无修改
        - id: openapi
          uri: http://pig-gateway:9999
          predicates:
            - Path=/v3/api-docs/**
          filters:
            - RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/$\{path}/v3/api-docs


gateway:
  encode-key: 'thanks,pig4cloud'
  ignore-clients:
    - test
    - client

二,pig-auth 服务调整

1,同上

2,同上

3,将原有nacos配置中心上的配置文件迁移到application.yml文件中并修改

server:
  port: 3000

spring:
  application:
    name: @artifactId@
  freemarker:
    allow-request-override: false
    allow-session-override: false
    cache: true
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    enabled: true
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: true
    prefer-file-system-access: true
    suffix: .ftl
    template-loader-path: classpath:/templates/

# 配置文件加密根密码
jasypt:
  encryptor:
    password: pig
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

pig:
  upms:
    url: http://pig-upms-biz:4000
  auth:
    url: http://pig-auth:3000

三,pig-upms 服务调整

1,同上

2,同上

3,pig-umps-api 服务新增application.yml配置文件

pig:
  upms:
    url: http://pig-upms-biz:4000
  auth:
    url: http://pig-auth:3000

4,pig-umps-biz 服务新增application.yml配置文件内容调整

server:
  port: 4000

spring:
  application:
    name: @artifactId@
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: mysql
    url: jdbc:mysql://localhost:3306/pig?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
  cache:
    type: redis
  redis:
    host: pig-redis
  cloud:
    sentinel:
      eager: true
      transport:
        dashboard: pig-sentinel:5003

# 文件上传相关 支持阿里云、华为云、腾讯、minio
file:
  bucketName: s3demo
  local:
    enable: true
    base-path: /Users/lengleng/Downloads/img

mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml
  type-handlers-package: com.pig4cloud.pig.common.mybatis.handler
  global-config:
    banner: false
    db-config:
      id-type: auto
      table-underline: true
      logic-delete-value: 1
      logic-not-delete-value: 0
  configuration:
    map-underscore-to-camel-case: true
    jdbc-type-for-null: 'null'
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# feign 配置
feign:
  sentinel:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  compression:
    request:
      enabled: true
    response:
      enabled: true

pig:
  upms:
    url: http://pig-upms-biz:4000
  auth:
    url: http://pig-auth:3000

5,pig-umps-api 服务调整所有的feign接口

 四,本地host文件修改

1,文件管理器打开 C:\Windows\System32\drivers\etc找到hosts文件并修改

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#        127.0.0.1       localhost
#        ::1             localhost
127.0.0.1   activate.navicat.com
127.0.0.1   pig-mysql
127.0.0.1   pig-redis
127.0.0.1   pig-gateway
127.0.0.1   pig-register
127.0.0.1   pig-sentinel
127.0.0.1   pig-monitor
127.0.0.1   pig-job
127.0.0.1   pig-seata
127.0.0.1   pig-auth
127.0.0.1   pig-upms-biz

2,代码调整完之后build,clean install,然后依次启动pig-auth, pig-upms, pig-gateway服务,整合结束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章