Feign
创建项目勾选web SpringWeb
1.0 创建生产者SpringCloudFeignProvider
端口号:8081
pom.xml引入依赖
xml
<!--nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.5.0</version>
</dependency>
application.yml配置
yml
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #表示注册到哪个注册中心
application:
name: provider #应用名 生产者
添加注解
@EnableDiscoveryClient
1.1 创建消费者SpringCloudFeignCustomer
端口号:8082
pom.xml引入依赖
xml
<!--nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.5.0</version>
</dependency>
application.yml配置
yml
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #表示注册到哪个注册中心
application:
name: customer #应用名 生产者
添加注解
@EnableDiscoveryClient
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 引入依赖
xml
<!--feign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.1.8</version>
</dependency>
<!--加载器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.1.8</version>
</dependency>
2.2 写feign层
创建feign
包
编写interface MyFeign
java
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
类
java
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 客户端
3.0 customer(消费者中)SpringCloudFeignCustomer
3.1 创建Controller 编写TestFeignController
java
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;
}
}
SpringCloud--GateWay
创建项目
创建项目时不要引入任何依赖 与spring-web冲突
1.0 添加依赖pom.xml
xml
<!--GateWay依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.8</version>
</dependency>
<!--加载器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.1.8</version>
</dependency>
<!--nacos依赖 用于注册到nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.5.0</version>
</dependency>
1.1 在java
同级目录下创建resources
标记为资源根
创建application.yml
添加配置
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