当前位置: 首页 > 工具软件 > swagger-dubbo > 使用案例 >

dubbo2.7+如何集成swagger

漆雕稳
2023-12-01

原配置:

<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-core</artifactId>
			<version>1.5.16</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.0</version>
			<scope>provided</scope>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

修改后

<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-core</artifactId>
			<version>1.5.20</version>
		</dependency>

		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.0</version>
			<scope>provided</scope>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

DubboPropertyConfig类
import com.alibaba.dubbo.config.ApplicationConfig;
改为
import org.apache.dubbo.config.ApplicationConfig;

ReferenceManager类
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
改为
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;

public Object getProxy(String interfaceClass) {
        Set<Entry<Class<?>, Object>> entrySet = interfaceMapProxy.entrySet();
        for (Entry<Class<?>, Object> entry : entrySet) {
            if (entry.getKey().getName().equals(interfaceClass)) { return entry.getValue(); }
        }

        for (ServiceBean<?> service : services) {
            if (interfaceClass.equals(service.getInterfaceClass().getName())) {
                ReferenceConfig<Object> reference = new ReferenceConfig<Object>();
                reference.setApplication(service.getApplication());
                reference.setRegistry(service.getRegistry());
                reference.setRegistries(service.getRegistries());
                reference.setInterface(service.getInterfaceClass());
                reference.setVersion(service.getVersion());
                interfaceMapProxy.put(service.getInterfaceClass(), reference.get());
                return reference.get();
            }
        }
        return null;
    }

改为

public Object getProxy(String interfaceClass) {
        Set<Entry<Class<?>, Object>> entrySet = interfaceMapProxy.entrySet();
        for (Entry<Class<?>, Object> entry : entrySet) {
            if (entry.getKey().getName().equals(interfaceClass)) { return entry.getValue(); }
        }

        for (ServiceBean<?> service : services) {
            if (interfaceClass.equals(service.getInterfaceClass().getName())) {
                ReferenceConfig<Object> reference = new ReferenceConfig<Object>();
                reference.setApplication(service.getApplication());
                reference.setRegistry(service.getRegistry());
                reference.setRegistries(service.getRegistries());
                reference.setInterface(service.getInterfaceClass());

                reference.setVersion(service.getVersion());
                reference.setGroup(service.getGroup());
                reference.setRetries(service.getRetries());
                reference.setActives(service.getActives());
                reference.setCache(service.getCache());
                // 集群方式
                reference.setCluster(service.getCluster());
                reference.setLayer(service.getLayer());
                reference.setTimeout(service.getTimeout());
                reference.setConnections(service.getConnections());
                // 负载均衡策略
                reference.setLoadbalance(service.getLoadbalance());
                interfaceMapProxy.put(service.getInterfaceClass(), reference.get());
                return reference.get();
            }
        }
        return null;
    }

最重要的是:ReferenceManager类中
Field field = SpringExtensionFactory.class.getDeclaredField(“contexts”);
改为
Field field = SpringExtensionFactory.class.getDeclaredField(“CONTEXTS”);

然后将项目打包,按照https://github.com/Sayi/swagger-dubbo的配置
简单修改后:https://github.com/hjnjmjkj/swagger-dubbo.git

参考链接:Dubbo微服务基于swagger自动生成文档及测试调用

 类似资料: