1、添加依赖jar
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<!--使用resteasy netty发布rest服务 -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty4</artifactId>
<version>3.9.3.Final</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
<!--估计是消费端使用,demo仅仅展示服务端 -->
<!--<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.9.3.Final</version>
</dependency>-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.9.3.Final</version>
</dependency>
2、编写服务接口
public interface DemoService {
UserInfo test();
}
3、编写服务实现
@Path("/aa")
@Service(protocol = {"dubbo", "rest"})
public class DemoServiceImpl implements DemoService {
private AtomicInteger atomicInteger = new AtomicInteger();
@GET
@Path("/aa")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Override
public UserInfo retry() {
log.info("retry()重试请求 start: ");
atomicInteger.getAndAdd(1);
UserInfo userInfo = new UserInfo();
userInfo.setUserName("retry");
userInfo.setAge(atomicInteger.get());
return userInfo;
}
}
4 SpringBoot application.properties中添加配置
#dubbo协议配置
dubbo.protocols.dubbo.name=dubbo
dubbo.protocols.dubbo.port=20880
#rest协议配置
dubbo.protocols.rest.name=rest
dubbo.protocols.rest.port=20980
dubbo.protocols.rest.server=netty
5 浏览器访问
6 本文主要是为了记录dubbo如何使用resteasy和netty发布rest服务