创建项目勾选web SpringWeb
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 #应用名 生产者
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 #应用名 生产者
D:\ideaworkspace\jar\nacos-server-2.2.0\nacos\bin>startup.cmd -m standalone
http://127.0.0.1:8848/nacos/#/login
userName:nacos
passWord:nacos
保证服务可以注册到注册中心上
SpringCloudFeignProvider
<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>
创建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 客户端
SpringCloudFeignCustomer
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
创建项目
创建项目时不要引入任何依赖 与spring-web冲突
<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>
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 #应用名 网关
@EnableDiscoveryClient
访问 http://localhost:8083/provider/test?str=123