https://github.com/apache/incubator-shenyu/releases 下载2.4.0
maven install:
cd incubator-shenyu
mvn clean install -Dmaven.javadoc.skip=true -B -Drat.skip=true -Djacoco.skip=true -DskipITs -DskipTests
版本:
使用shenyu2.4.0版本 https://shenyu.apache.org/zh/docs/2.4.0 nacos2.0.1版本admin配置
shenyu-admin管理界面项目
1.执行sql 注意sql必须按版本严格执行 不同版本的sql会出现变化 比如context_path变contextPath
incubator-shenyu-2.4.0shenyu-adminsrcmainresourcesmeta-INFschema.sql
2.修改incubator-shenyu-2.4.0shenyu-adminsrcmainresourcesapplication.yml
spring: profiles: active: mysql # h2 datasource: url: jdbc:mysql://localhost:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver
3.升级lombok插件支持@SuperBuilder
4.支持服务端nacos同步选择器和规则
pom:bootstrap网关配置yml配置文件: shenyu: register: registerType: nacos # 使用nacos获取客户端注册的信息即uri和元数据 http #http #zookeeper #etcd #nacos #consul serverLists: 127.0.0.1:8848 #localhost:2181 #http://localhost:2379 #localhost:8848 props: sessionTimeout: 5000 connectionTimeout: 2000 checked: true zombieCheckTimes: 5 scheduledTime: 10 nacosNameSpace: ShenyuRegisterCenter # 命名空间id和名称都是ShenyuRegisterCenter 和服务发现不同 只用于数据admin和client的实例,接口数据注册 sync: websocket: # 支持和网关bootstrap用websocket同步路由(插件 选择器 规则) enabled: true org.apache.shenyu shenyu-register-server-nacos # nacos-client用的2.0.0版本${project.version}
网关需要支持nacos,用于springcloud的自动服务发现
1.pom
org.apache.shenyu shenyu-spring-boot-starter-plugin-springcloud${project.version} org.springframework.cloud spring-cloud-commons2.2.0.RELEASE org.springframework.cloud spring-cloud-starter-netflix-ribbon2.2.0.RELEASE org.apache.shenyu shenyu-spring-boot-starter-plugin-httpclient${project.version} com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery2.1.0.RELEASE
2.application-local.yml
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 你的nacos地址 shenyu: sync: websocket : urls: ws://localhost:9095/websocket # 网关和admin通信使用默认的websocket 动态同步路由无须重启 需要admin支持websocket同步client配置
1.pom 使用的2.4.0版本
org.apache.shenyu shenyu-spring-boot-starter-client-springcloud2.4.0 org.apache.shenyu shenyu-register-client-nacos2.4.0
2.application.yml
server: port: 8884 address: 0.0.0.0 spring: application: name: springCloud-test cloud: nacos: discovery: # 默认public 服务发现与注册 server-addr: 127.0.0.1:8848 springCloud-test: ribbon.NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule shenyu: client: # 用于接口暴露的注册 命名空间ShenyuRegisterCenter registerType: nacos # http #zookeeper #etcd #nacos #consul serverLists: 127.0.0.1:8848 # http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848 props: contextPath: /springcloud # 网关前缀 必须存在 网关通过该前缀访问 会在服务发现层转换为serviceId port: 8884 nacosNameSpace: ShenyuRegisterCenter
2.1解析
shenyu: client: registerType: http serverLists: http://localhost:9095 props: contextPath: /http appName: http isFull: true # registerType : 服务注册类型,请参考应用客户端接入文档 # serverList: 服务列表,请参考应用客户端接入文档 # contextPath: 为你的项目在shenyu网关的路由前缀。 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由。 # appName:你的应用名称,不配置的话,会默认取application 中的名称 # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
2.2 通过注解暴露接口 暴露的接口通过nacos的ShenyuRegisterCenter 同步到admin的规则 其contextPath同步到选择器
@RestController @RequestMapping("/order") @ShenyuSpringCloudClient(path = "/order") public class OrderController { @GetMapping("/findById") // @ShenyuSpringCloudClient(path = "/findById") // /order/findById 不会暴露到网关 public OrderDTO findById(@RequestParam("id") final String id) { OrderDTO orderDTO = new OrderDTO(); orderDTO.setId(id); orderDTO.setName("hello world spring cloud findById"); return orderDTO; } @GetMapping("/path/{id}/{name}") @ShenyuSpringCloudClient(path = "/path/**") // /order/path/** 暴露到网关 public OrderDTO getPathVariable(@PathVariable("id") final String id, @PathVariable("name") final String name) { OrderDTO orderDTO = new OrderDTO(); orderDTO.setId(id); orderDTO.setName("hello world spring cloud restful: " + name); return orderDTO; } } @RestController @RequestMapping("/test") @ShenyuSpringCloudClient(path = "/test/**") // /test/** 所有接口暴露到网关 public class TestController { ... }执行
访问http://localhost:9095/ 账号密码 admin 123456
进入基础配置-插件管理-第二页 点击springCloud插件开启
启动admin bootstrap client
访问http://localhost:9195/springcloud/test/findByUserId?userId=2
等同于访问http://localhost:8884/test/findByUserId?userId=2 springcloud(contextPath) handle处理为 serviceId:springCloud-test(application.name) 用于服务发现
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)