3. SpringCloud-Alibaba integrates config configuration center and gateway gateway

Integrated gateway gateway.

1. Create a submodule springboot project in the parent module

2. Import maven related dependencies

		<!--        服务注册与发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--        网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
<!--        响应式编程-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

3. Add annotations above the main startup class to @EnableDiscoveryClientstart nacos service discovery

4. Write a gateway configuration file (including request verification rules)

Example configuration (with comments)

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.3:8086
    gateway:
      discovery:
        locator:
          include-expression: "*"
      routes:
#        设置路由id,必须唯一
        - id: after_route
#          设置映射地址,lb://服务名->http://服务名对应的ip地址:端口号
          uri: lb://customer
#          配置校验请求网关的地址是否符合要求
          predicates:
#            路径校验(/**放开所有)
            - Path=/**
        - id: test_route
            #          设置映射地址,lb://服务名->http://服务名对应的ip地址:端口号
          uri: lb://provider
            #          配置校验请求网关的地址是否符合要求
          predicates:
            #            路径校验(/**放开所有)
            - Path=/test/**
##            请求方式只能为get请求
#            - Method=GET
##            请求参数必须包含green,逗号后为正则表达式,表示检验值的规则 如:http:localhost:5656/test?green=ccca
#            - Query=green,ccc.
##           在该时间之后
#            - After=2020-01-20T17:42:47.789-07:00[America/Denver]
##           在该时间之前
#            - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
##            时间段匹配,在以下时间请求会匹配该路由
#            - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
##            通过cookie的键值匹配,值支持正则表达式
#            - Cookie=aa,bb
##           请求头匹配 值支持正则表达式
#            - Header=X-Request-Id, \d+
##           主机匹配
#            - Host=**.com,**.com
##           ip地址匹配
#            - RemoteAddr=192.168.1.1/24
##           权重匹配,如果有多个路由匹配,通过权重选择
#            - Weight=group1, 2
##            可以二次处理请求路径
#          filters:
##           匹配后自动在路径前添加前缀
#            - PrefixPath=/a
##           匹配后去点路径前缀n个   如:http:localhost:8080/a/b/c  则变为 http:localhost:8080/b/c
#            - StripPrefix=1
##           删除请求参数
#            - RemoveRequestParameter=red
##           设置路径
#            - SetPath=/{segment}
##           设置请求头
#            - SetRequestHeader=foo, bar-{segment}
##            重定向
#            - RedirectTo=302, https://acme.org
#            args:
##              重试请求
#              retries: 3
##              请求状态
#              statuses: BAD_GATEWAY
##              请求方法
#              methods: GET,POST
#              backoff:
#                firstBackoff: 10ms
#                maxBackoff: 50ms
#                factor: 2
#                basedOnPreviousValue: false
##                请求大小限制
#              maxSize: 5000000
  application:
    name: gateWay-nacos
management:
  endpoint:
    gateway:
      enabled: true
server:
  port: 5656

test

我们网关的端口是5656

The gateway is the entry point for all requests. We request the service of the gateway to see if it is forwarded to the request interface corresponding to the service name.
insert image description here
If successful, the above content is returned by the interface corresponding to the consumer.

Integrated Configuration Center

1. Create a submodule springboot project in the parent module

2. Import maven related dependencies

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

3. Enter the nacos registration center console to add a new configuration center

insert image description here
The configuration example is shown in the figure

dataId needs to correspond to the application name

insert image description here

4. Create bootstrap.ymlfiles and configure

Enable the configuration center and configure the address of the configuration center. The
application name needs to correspond to the dataid of the nacos configuration center
insert image description here

yml configuration is as follows:

spring:
  cloud:
    nacos:
      config:
        server-addr: 192.168.1.3:8086
        enabled: true
        file-extension: yml
 #        应用名称需要对应nacos配置中心的dataid,他会使用对应的配置中心的配置
  application:
    name: aConfig

You can use annotations on classes you use configuration files to @RefreshScopesupport real-time refresh. Configuration center modification will be synchronously modified
insert image description here

The interface is shown in the figure, and the integrated configuration center is tested.
insert image description here
test was successful. The configuration center integration hello world is complete!

Epilogue

Welcome to discuss and exchange ❤️❤️❤️

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324116139&siteId=291194637