shenyu单机部署和整合springcloud(手动和注解自动配置),nacos微服务网关

文章目录

  • shenyu
    • springcloud使用
      • 初期准备
      • admin配置
      • bootstrap网关配置
      • client配置
      • 执行
    • 手动配置springcloud服务发现

shenyu

springcloud使用

初期准备

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.0\shenyu-admin\src\main\resources\META-INF\schema.sql

2.修改incubator-shenyu-2.4.0\shenyu-admin\src\main\resources\application.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:

    org.apache.shenyu
    shenyu-register-server-nacos # nacos-client用的2.0.0版本
    ${project.version}


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

bootstrap网关配置

网关需要支持nacos,用于springcloud的自动服务发现

1.pom


    org.apache.shenyu
    shenyu-spring-boot-starter-plugin-springcloud
    ${project.version}



    org.springframework.cloud
    spring-cloud-commons
    2.2.0.RELEASE


    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon
    2.2.0.RELEASE



    org.apache.shenyu
    shenyu-spring-boot-starter-plugin-httpclient
    ${project.version}


    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery
    2.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-springcloud
    2.4.0


    org.apache.shenyu
    shenyu-register-client-nacos
    2.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) 用于服务发现

手动配置springcloud服务发现

需要配置插件 contextPath和springCloud,配置元数据metaData(不配置元数据 请求类型默认http rpcType无法使用springCloud)

比如不配置contextPath 单个服务存在多个不同的前缀路径:

  1. 服务路径http://localhost:8888/order/findById?id=1 服务名springCloud-test
  2. 网关路径http://localhost:9195/order/findById?id=1

先配置元数据 springCloud-test

路径:/springcloud/** 
服务接口:springCloud-test
RPC类型:springCloud

配置springCloud插件

选择器:
uri match /test/**
handle处理 serviceId springCloud-test
规则:
uri match /test/**
path:/test/**

完成rpc数据和插件配置 此时访问http://localhost:9195/order/findById?id=1

global过滤器会从metaData获取rpc路径匹配 改变context的rpc类型http——>springCloud
springCloud插件从ribbon的loadBalancer获取ServiceInstance 组装真实url realURL
    http://ServiceInstance的IP:端口号/order/findById?id=1

你可能感兴趣的:(java,微服务,java,微服务,后端,shenyu)