微服务项目搭建流程


父工程:

  1、在父工程中对所有的依赖做版本控制

  2、根据不用的架构使用不同的依赖

  • 公共的依赖包直接引入比如lombok
  • 对用得到的进行版本个控制比如SpringBoot、SpringCloud、SpringCloud-alibaba、elasticsearch.client、elasticsearch、redis、RabbitMQ(amqp)、Seata、mybatis-plus、mybatis、mysql等

  3、对不同的服务做相关配置

   gateway: 

server:
  port: 10010
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: localhost:8848
    gateway:
      routes:
        - id: order
          uri: lb://order-service
          predicates:
            - Path=/order/**,/pay/**,/cart/**
        - id: item
          uri: lb://item-service
          predicates:
            - Path=/item/**
      #配置-跨域
      globalcors: # 全局的跨域处理
        #解决options请求被拦截问题
        add-to-simple-url-handler-mapping: true
        corsConfigurations:
          '[/**]':
            #允许哪些网站的跨域请求
            allowedOrigins: "*"
            #允许的跨域ajax的请求方式
            allowedMethods:
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            #允许在请求中携带的头信息
            allowedHeaders: "*"
            #是否允许携带cookie
            allowCredentials: true
            #这次跨域检测的有效期
            maxAge: 360000

elasticSearch

 

server:
  port: 8083
spring:
  application:
    name: search-service
  cloud:
    nacos:
      server-addr: localhost:8848
es:
  host: localhost
  port: 9200
  scheme: http
feign:
  httpclient:
    connection-timeout: 2000
    readTimeout: 2000
    enabled: true

 RabbitMQ和数据库

server:
  port: 8082
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: localhost:8848
  datasource:
    url: jdbc:mysql://localhost:3306/db?useSSL=false&serverTimezone=UTC&characterEncoding=UTF8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
    virtual-host: /

Redis

  redis:
    host: localhost
    port: 6379
    database: 0

通过Feign服务添加的请求头

@Component
public class MyFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        requestTemplate.header("xxxx", "yyyy");
    }
}

 

posted @ 2023-09-06 22:01  天天开心1?  阅读(52)  评论(0)    收藏  举报