1. dubbox needs to be installed locally, the address is https://github.com/dangdangdotcom/dubbox.git , the version used is 2.8.4
2. Create a new spring boot project with version 1.5.6.RELEASE
- Create maven module
spring-boot-dubbox-api: dubbo service interface
spring-boot-dubbox-provider: dubbo service provider
- Introduce dubbo configuration
@Configuration @ImportResource("classpath:dubbo/dubbo.xml") public class PropertiesLoad { }The specific configuration of dubbo.xml is as follows
<!-- dubbo scan Service--> <dubbo:annotation package="org.spring.boot.dubbox.provider.service"/> <dubbo:application name="provider" owner="platin"/> <dubbo:registry protocol="zookeeper" address="zookeeper://localhost:2181" /> <!--Use kyro serialization--> <dubbo:protocol name="dubbo" serialization="kryo" port="20990" /> <!-- dobbox is released based on resteasy's REST interface, sever uses Tomcat --> <dubbo:protocol name="rest" port="8090" server="tomcat" />
- publish service
@Service//alibaba service annotation @Path("user")//rest service //rest service data format, the class and method are specified at the same time, the priority in the method is high @Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" }) public class UserServiceImpl implements UserService { private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @POST @Path("save") @Produces({ ContentType.APPLICATION_JSON_UTF_8 }) //Type of dubbox encapsulation public WrappResult save() { //Solve the problem that primitive types are not serialized by JAXB return new WrappResult((int) (Math.random() * 10)); } @GET @Path("{id : \\d+}") public User getUserById(@PathParam("id") Long id, @Context HttpServletRequest request) { logger.info("Client address is:{}", request.getRemoteAddr()); User user = new User(1L, "SpringBoog-Dubbox",20); return user; } @GET @Path("/xml/{id : \\d+}") @Produces(ContentType.TEXT_XML_UTF_8) //Use xml format public User getUserByIdXml(@PathParam("id") Long id) { logger.info("Client address is:{}", RpcContext.getContext().getRemoteAddressString()); User user = new User(10L, "Xml-Entity",20); return user; } }
3. Spring Boot startup class
@SpringBootApplication public class ProviderApp { public static void main(String[] args) throws IOException { SpringApplication.run(ProviderApp.class, args); } // load using jetty // @Bean // public EmbeddedServletContainerFactory servletContainer() { // JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); // return factory; // } }
- rest service publish result
main INFO zookeeper.ZookeeperRegistry: [DUBBO] Register: rest://10.*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true…… main INFO zookeeper.ZookeeperRegistry: [DUBBO] Subscribe: provider://10*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true&…… main INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url provider://10.*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true&……
- rest service verification
http://localhost:8090/user/1.xml
<user> <age>20</age> <id>1</id> <name>SpringBoog-Dubbox</name> </user>
http://localhost:8090/user/1.json
{ "id": 1, "name": "SpringBoog-Dubbox", "age": 20 }
Project address https://github.com/hjguang/spring-boot-dubbox