nacos服务注册中心+gateway网关+服务提供者

1.Spring Cloud GateWay需在pom.xml中增加如下配置:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway</artifactId>
   <version>2.1.5.RELEASE</version>
</dependency>
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>Greenwich.SR5</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

2. 注册发现服务需要在pom.xml中增加如下配置:

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>2.1.1.RELEASE</version>
</dependency>

3.Spring Cloud GateWay 注册和发现注册中心服务需要在application中增加如下配置:

server:
  port: 9000
spring:
  application:
    name: getway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: first_route
          uri: lb://service-provider
          filters:
            - AddRequestHeader=X-Request-Foo, Bar
          predicates:
            - Path=/echo/**
        - id: second_route
          uri: https://www.jd.com/
          filters:
            - AddRequestHeader=X-Request-Foo, Bar
          predicates:
            - Path=/second/**
    #service-consumer
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #注册中心地址

 

4.Application启动类中增加注解@EnableDiscoveryClient



5.服务提供者

在pom.xml中增加如下依赖

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>2.1.1.RELEASE</version>
</dependency>

application.yml中增加服务名称和注册中心地址

#service-provider
Spring:
  application:
    name: service-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

application 启动类中增加注解@EnableDiscoveryClient

6.启动Gateway和服务提供者,注册中心服务如下:

 

 

 

 

 

7.通过网关访问服务:

http://localhost:9000/echo/hello

github:

https://github.com/changqinrong/spring-cloud-getway

https://github.com/changqinrong/nacos-demo-provider-server

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读