您好,登錄后才能下訂單哦!
小編這次要給大家分享的是詳解Spring http服務遠程調用實現過程,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
最近公司有個新的需求,寫了一個接口,想要把服務暴露出去,但是這個服務所在的進程是非web項目,(可以理解成schedule/batch等進程項目),所以沒有tomcat等容器,而且只有這一個服務,無論是加dubbo服務還是加tomcat等容器都顯得復雜了。那么應該如何將服務暴露出去?
經過網上搜索后,最終解決問題,記錄在此。
為了快速搭建,使用springboot來搭建項目:
項目結構如圖:
首先需要創建一個接口,服務的提供者和服務的調用方都依賴這個模塊。
package com.xiazhi.spring.service.api; import com.sun.istack.internal.NotNull; import com.sun.istack.internal.Nullable; /** * @author 趙帥 * @date 2020/6/8 */ public interface IUserService { /** * 獲取姓名 * @return 姓名 */ String getName(); /** * 根據姓名獲取年齡 * @param name 姓名 * @return 年齡 */ @NotNull Integer getAge(@Nullable String name); }
然后在service模塊中,實現接口作為服務的提供方,需要依賴的jar包有:
<dependencies> <dependency> <groupId>com.xiazhi</groupId> <artifactId>spring-service-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies>
配置文件需要加上:
spring.main.web-application-type=none
實現接口:
package com.xiazhi.spring.service.impl; import com.sun.istack.internal.NotNull; import com.sun.istack.internal.Nullable; import com.xiazhi.spring.service.api.IUserService; import org.springframework.stereotype.Service; /** * @author 趙帥 * @date 2020/6/8 */ @Service public class UserServiceImpl implements IUserService { @Override public String getName() { return "張一"; } @Override @NotNull public Integer getAge(@Nullable String name) { if ("".equals(name)) { return 10; } return 18; } }
暴露服務:
package com.xiazhi.spring.service.config; import com.sun.net.httpserver.HttpHandler; import com.xiazhi.spring.service.api.IUserService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskExecutor; import org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter; import org.springframework.remoting.support.SimpleHttpServerFactoryBean; import java.util.HashMap; import java.util.Map; /** * @author 趙帥 * @date 2020/6/8 */ @Configuration public class HttpInvokerConfiguration { private final IUserService userService; private final TaskExecutor taskExecutor; public HttpInvokerConfiguration(IUserService userService, TaskExecutor taskExecutor) { this.userService = userService; this.taskExecutor = taskExecutor; } /** * 將IUserService服務暴露出去 */ @Bean public SimpleHttpInvokerServiceExporter serviceExporter() { SimpleHttpInvokerServiceExporter exporter = new SimpleHttpInvokerServiceExporter(); exporter.setService(userService); exporter.setServiceInterface(IUserService.class); return exporter; } /** * 為暴露的服務啟用http服務 * @return httpServer工廠類 */ @Bean public SimpleHttpServerFactoryBean serverFactoryBean() { SimpleHttpServerFactoryBean factoryBean = new SimpleHttpServerFactoryBean(); Map<String, HttpHandler> map = new HashMap<>(2); factoryBean.setContexts(map); factoryBean.setPort(9999); factoryBean.setExecutor(taskExecutor); return factoryBean; } }
啟動服務。
然后是服務的調用方,依賴有:
<dependencies> <dependency> <groupId>com.xiazhi</groupId> <artifactId>spring-service-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
配置文件:
package com.xiazhi.spring.config; import com.xiazhi.spring.service.api.IUserService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; /** * @author 趙帥 * @date 2020/6/8 */ @Configuration public class HttpServiceConfiguration { /** * 使用http代理工廠調用服務 * @return http代理工廠創建代理對象 */ @Bean public HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean() { HttpInvokerProxyFactoryBean factoryBean = new HttpInvokerProxyFactoryBean(); factoryBean.setServiceUrl("http://localhost:9999/userService"); factoryBean.setServiceInterface(IUserService.class); return factoryBean; } }
使用接口,調用方法:
package com.xiazhi.spring.controller; import com.xiazhi.spring.service.api.IUserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 趙帥 * @date 2020/6/8 */ @RestController public class UserController { private final IUserService userService; public UserController(IUserService userService) { this.userService = userService; } @GetMapping("/test") public String test() { String name = userService.getName(); Integer age = userService.getAge(null); System.out.println(String.format("姓名:[%s],age:[%s]", name, age)); return name; } }
運行,調用test路徑測試調用結果。
項目完整結構:
看完這篇關于詳解Spring http服務遠程調用實現過程的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。