In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "what are the correct ways to open Spring native Rpc". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the correct way to open Spring native Rpc" can help you solve your doubts.
What is Rpc?
Rpc (Remote Procedure Call): encapsulate the internal implementation of the remote invocation process is rpc,rpc mainly to simplify remote service invocation, popular is to call remote services (cross-host, cross-process) just like calling local methods. Fegin technology in Spring Cloud system can also be regarded as a Rpc technology that uses http protocol to transmit data.
Rpc in Spring
Native Rpc implementations of six different data transmission modes are built into Spring, namely WebService, Jms, Rmi, Http, Hessian (http) and Amqp. Those who are familiar with Rpc know that in Java, the invocation of Rpc services is mainly achieved by generating proxies for service interfaces, such as Dubbo and Motan, and so is the implementation of Spring. In Rpc service invocation, there are two roles, namely, the provider of the service and the caller (consumer). On the one hand, through the proxy, the service caller will transfer the interface name and method parameters defined by the service to the provider. On the other hand, the service provider takes the interface information to find the local service to generate the call result and return it to the caller. So the six Rpc implementations described below will have a common service interface definition, as well as their respective proxy implementation configurations.
Define service interface / * * @ WebService annotations are only used for RPC services provided by ws * / @ WebService public interface AccountService {Account getAccount (String name); class Account implements Serializable {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;}
The public api is used in both Rpc providers and consumers. The provider implements this interface to provide services, and the consumer generates a proxy implementation of the interface through a proxy, and then sends a specific message through the underlying encapsulation. Similar to using dubbo and motan
Invoke the service code @ SpringBootApplication public class WsConsumerApplication {@ Autowired private AccountService accountService; @ PostConstruct public void callRpcService () {System.out.println ("RPC remote access starts!") ; System.err.println (accountService.getAccount ("kl") .getName (); System.out.println ("RPC remote access is over!") ;} public static void main (String [] args) {SpringApplication.run (WsConsumerApplication.class, args);}}
Every Rpc implementation is the same, invoking the service by injecting the proxy implementation of the AccountService interface. However, the configuration of agents for each Rpc will be slightly different, mainly in that different transport technologies will use different configurations. Generally speaking, it is common to connect url (http://127.0.0.1, tcp://172.0.0.1, rmi://127.0.0.1), port, proxy interface information, and so on.
WEBSERVICE's RPC implementation service provider service implementation @ WebService (serviceName= "AccountService", endpointInterface = "com.spring.rpc.api.AccountService") @ Service public class AccountServiceImpl extends SpringBeanAutowiringSupport implements AccountService {Logger logger = LoggerFactory.getLogger (getClass ()); @ Override @ WebMethod public Account getAccount (String name) {logger.info ("{} request to get an account!" , name); Account account = new Account (); account.setName (name + "account"); return account;}}
Unlike other service implementations, WebService defines services with @ WebService and @ WebMethod annotation tags
Service exposure @ Configuration public class WsConfig {private String ipList = "127.0.0.1"; private String userName = "admin"; private String passWord = "sasa"; @ Bean public SimpleHttpServerJaxWsServiceExporter rmiServiceExporter (Authenticator authenticator) {SimpleHttpServerJaxWsServiceExporter exporter = new SimpleHttpServerJaxWsServiceExporter (); exporter.setHostname ("127.0.0.1"); exporter.setPort (8083); exporter.setAuthenticator (authenticator); return exporter;} @ Bean public Authenticator authenticator () {Authenticator authenticator = new Authenticator () Authenticator.setIpList (ipList); authenticator.setUserName (userName); authenticator.setPassWord (passWord); return authenticator;}}
After completing the above code, we have built a complete WebService service with user, password, ip whitelist and other API permissions authentication. You can see the definition of the service by visiting: http://127.0.0.1:8083/AccountServiceImpl?WSDL, as shown below:
Service consumer @ Configuration public class WsConfig {@ Bean ("accountService") public JaxWsPortProxyFactoryBean accountService () throws Exception {JaxWsPortProxyFactoryBean factoryBean = new JaxWsPortProxyFactoryBean (); factoryBean.setServiceName ("AccountService"); factoryBean.setPortName ("AccountServiceImplPort"); factoryBean.setNamespaceUri ("http://provider.ws.rpc.spring.com/"); URL wsdlDocumentUrl = new URL (" http://127.0.0.1:8083/AccountServiceImpl?WSDL"); ") FactoryBean.setWsdlDocumentUrl (wsdlDocumentUrl); factoryBean.setServiceInterface (AccountService.class); factoryBean.setUsername ("admin"); factoryBean.setPassword ("sasa"); return factoryBean;}}
Get the proxy instance of AccountService.class by declaring JaxWsPortProxyFactoryBean. When the service invocation method is injected, it actually triggers a remote call to WebService
HTTP's RPC implementation service provider service implementation @ Service public class AccountServiceImpl implements AccountService {Logger logger = LoggerFactory.getLogger (getClass ()); @ Override public Account getAccount (String name) {logger.info ("{} request to get an account!" , name); Account account = new Account (); account.setName (name + "account"); return account;}} Service exposure @ Configuration public class HttpConfig {@ Bean ("/ AccountService") public HttpInvokerServiceExporter rmiServiceExporter (AccountServiceImpl accountService) {HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter (); exporter.setService (accountService); exporter.setServiceInterface (AccountService.class); return exporter } @ Bean public ServletRegistrationBean servletRegistrationBean (DispatcherServlet dispatcherServlet) {ServletRegistrationBean servlet = new ServletRegistrationBean (); servlet.setServlet (dispatcherServlet); servlet.setName ("remoting"); servlet.setLoadOnStartup (1); servlet.addUrlMappings ("/ remoting/*"); return servlet;}} Service Consumer @ Configuration public class HttpConfig {@ Bean ("accountService") public HttpInvokerProxyFactoryBean accountService () {HttpInvokerProxyFactoryBean factoryBean = new HttpInvokerProxyFactoryBean () FactoryBean.setHttpInvokerRequestExecutor (new HttpComponentsHttpInvokerRequestExecutor ()); factoryBean.setServiceUrl ("http://127.0.0.1:8081/remoting/AccountService"); factoryBean.setServiceInterface (AccountService.class); return factoryBean;}}"
As you can see, when configuring the Rpc service consumer for the Http implementation, it is similar to WebService, and defining a FactoryBean is ok. In fact, the other four Rpc implementations are pretty much the same. I won't list them one by one later.
After reading this, the article "what are the correct ways to open Spring native Rpc" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.