SpringCloud--Feign&GateWay

Feign

创建项目勾选web SpringWeb

1.0 创建生产者SpringCloudFeignProvider

端口号:8081
pom.xml引入依赖

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            <version>2021.0.5.0version>
        dependency>

application.yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #表示注册到哪个注册中心
  application:
    name: provider #应用名 生产者

添加注解
@EnableDiscoveryClient
SpringCloud--Feign&GateWay_第1张图片

1.1 创建消费者SpringCloudFeignCustomer

端口号:8082
pom.xml引入依赖

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            <version>2021.0.5.0version>
        dependency>

application.yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #表示注册到哪个注册中心
  application:
    name: customer #应用名 生产者

添加注解
@EnableDiscoveryClient
SpringCloud--Feign&GateWay_第2张图片

1.2 开启nacos

D:\ideaworkspace\jar\nacos-server-2.2.0\nacos\bin>startup.cmd -m standalone

1.3 访问

http://127.0.0.1:8848/nacos/#/login
userName:nacos
passWord:nacos

保证服务可以注册到注册中心上

2.0 在Provider(生产者中)SpringCloudFeignProvider

2.1 引入依赖
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
            <version>3.1.8version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-loadbalancerartifactId>
            <version>3.1.8version>
        dependency>
2.2 写feign层

创建feign
编写interface MyFeign

package com.linnuoci.springcloudfeignprovider.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name ="customer")//指定服务名称
//provider要调用customer的接口
//@FeignClient注解的name属性值表示要调用服务的名字
public interface MyFeign {
    @RequestMapping("/test")//会把这个方法转接到customer的test方法
    public String m1(String str);

}

创建controller包 创建FeignController

package com.linnuoci.springcloudfeignprovider.controller;

import com.linnuoci.springcloudfeignprovider.feign.MyFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FeignController {
    @Autowired
    private MyFeign myFeign;

    @RequestMapping("/test")
    @ResponseBody
    public String test(String str){

        return myFeign.m1(str);
    }

}

在启动项上添加注解@EnableFeignClients//启用 Feign 客户端
SpringCloud--Feign&GateWay_第3张图片

3.0 customer(消费者中)SpringCloudFeignCustomer

3.1 创建Controller 编写TestFeignController
package com.linnnuoci.springcloudfeigncustomer.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestFeignController {
    @RequestMapping("/test")
    public String test(@RequestBody String str){
        return "这是我customer返回的:"+str;
    }
}

访问 http://localhost:8081/test

SpringCloud–GateWay

创建项目
创建项目时不要引入任何依赖 与spring-web冲突

1.0 添加依赖pom.xml

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
            <version>3.1.8version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-loadbalancerartifactId>
            <version>3.1.8version>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            <version>2021.0.5.0version>
        dependency>
1.1 在java同级目录下创建resources标记为资源根

创建application.yml添加配置

server:
  port: 8083
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #表示注册到哪个注册中心
    gateway:
      routes:   #路由
      - id: provider #路由的id 自己起的不要跟其他路由id重复
        uri: lb://provider #设置路由的url 表示往哪个服务代理 lb后写的是服务的名字 application name
        predicates:
          - Path=/provider/* #路径匹配规则  当访问 http://127.0.0.1:8083/provider/test 时,会转发到provider服务 http://127.0.0.1:8081/test
      discovery:
        locator:
          enabled: true #开启路由发现
  application:
    name: gateway #应用名 网关


provider中的application name
SpringCloud--Feign&GateWay_第4张图片

1.2 启动项上添加注解@EnableDiscoveryClient

SpringCloud--Feign&GateWay_第5张图片

访问 http://localhost:8083/provider/test?str=123

你可能感兴趣的:(SpringCloud,spring,cloud,gateway,spring)