avatar

十六小站

欢迎来到我的个人主页! 期待与您分享我的经验与故事,一起探索技术的无穷可能!

  • 首页
  • NAS专题
  • 关于
Home WebService接口动态发布
文章

WebService接口动态发布

Posted 2023-11-23 Updated 2024-10- 19
By 十六 已删除用户
5~6 min read

背景

WebService接口经常会碰到不同环境接口展示的地址不同。 如下: file 我们不同环境需要访问不同的baseURL,那么如何去动态配置呢?

发布类实现ApplicationRunner接口

在WebService接口的动态发布类中实现ApplicationRunner接口,并实现接口方法,能够在服务启动后,执行该方法进行接口的发布。

实现逻辑

## 1. 配置BaseUrl,博主是微服务,配置中心中不同环境可以自动加载不同的配置文件,不同配置文件中配置对应的BaseUrl即可。
## 2. 添加拦截器,如果在WebService中存在拦截器,那么需要使用自动注入将拦截器先引入进来。

例如:

@Autowired
private WebServiceAuthInterceptor interceptor;
## 3. 发布接口拼接URL。

代码实现

/***
     * @desc <发布所有webService接口的初始化方法>
     * <功能详细描述>
     * @param
     * @date 2023年11月15日 17:10
     * @return  void
     * @exception
    */
    public void run(ApplicationArguments args) throws Exception {
        Reflections reflections = new Reflections(WEB_SERVICE_BASE_PACKAGE_NAME);
        Set> webServiceClasses = reflections.getTypesAnnotatedWith(WebService.class);

        List> interceptors = new ArrayList<>();
        interceptors.add(interceptor);

        // 处理获取到的WebService类
        for (Class webServiceClass : webServiceClasses) {
            if (!webServiceClass.isInterface()) { //判断如果不是接口,则发布webService

                String serviceName = webServiceClass.getAnnotation(WebService.class).serviceName();
                String requestUrl = getWayBaseUrl + Bus.DEFAULT_BUS_ID + "/" + serviceName;
                EndpointImpl endpoint = new EndpointImpl(springBus(), applicationContext.getBean(webServiceClass));

                endpoint.setInInterceptors(interceptors);
                endpoint.setPublishedEndpointUrl(requestUrl);
                endpoint.publish(serviceName);
            }
        }
    }

前提需要对应的WebSerive实现类已经注入到了Spring中。

后端
License:  CC BY 4.0
Share

Further Reading

Sep 30, 2025

微服务之Auth篇

auth服务重要是认证授权,签发jwt token使用。 生成密钥对 # 在项目 src/main/resources 下生成 jwt.jks(演示用,生产用更严格的密码/keystore) keytool -genkeypair \ -alias jwt \ -keyalg RSA \

Sep 6, 2025

SpringBoot3.X-2(缓存Redis/memory)

本文主要是实现缓存的集成,由于是单体项目 ,目前整合了内存缓存和Redis缓存两种,可以通过配置来切换。 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-sta

Sep 5, 2025

SpringBoot3.X-1(MP+Druid)

本次基于springboot-3.5.5,先附上文档地址:https://docs.spring.io/spring-boot/reference/data/sql.html 初始化springboot项目 项目创建步骤不做记录 集成mybaits-plus</

OLDER

群晖搭建iSCSI服务

NEWER

Springboot环境下的启动参数说明

Recently Updated

  • KubeShpere部署(4.1.2)
  • 微服务之Auth篇
  • 记一次前端优化(vue2)
  • SpringBoot3.X-2(缓存Redis/memory)
  • SpringBoot3.X-1(MP+Druid)

Trending Tags

Java Docker 前端 中间件 数据库 群晖 unraid

Contents

©2025 十六小站. Some rights reserved.

Using the Halo theme Chirpy