Normally, a service will not only be a client or a server. Generally, microservices are services and services calling each other. So, how to configure them? Next comes the previous one. Hello World: Introduction to dubbo If we change the configuration, we can realize the normal micro-service architecture. Compared with the previous, I have added a new project which is both a client and a server.
HelloServerClientServiceImpl.java:
package com.sawshaw.dubbo_server_client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.sawshaw.dubbo_interface.HelloInterface; import com.sawshaw.dubbo_interface.HelloServerClientInterface; @Service public class HelloServerClientServiceImpl implements HelloServerClientInterface{ @Autowired private HelloInterface hello; public String sayHi(String name) { System.out.println("hello server-client......client send name is "+name); String result=hello.sayHello(name); return "hello-server-client "+result; } }
provider.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- Providers apply information for computing dependencies --> <dubbo:application name="provider"/> <!-- Use zookeeper The registry exposes the service address, which is started zookeeper Default exposed address --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- use dubbo The protocol exposes the service port itself on the random port and replaces it if it is occupied. --> <dubbo:protocol name="dubbo" port="20881" /> <!-- Declare service interfaces that need to be exposed --> <dubbo:service interface="com.sawshaw.dubbo_interface.HelloServerClientInterface" ref="hiService"/> <!-- And local bean Implementing services as well --> <bean id="hiService" class="com.sawshaw.dubbo_server_client.HelloServerClientServiceImpl"/> </beans>
consumer.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- The consumer application name, used to calculate dependencies, is not a matching condition, not the same as the provider ,To achieve start-up error-free should be changed to false--> <dubbo:application name="consumer" default="false"/> <!-- Connection Registry Configuration --> <dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper" check="false"/> <!-- Generate remote service proxies that can be local and local bean Same use helloService check="false" Start without checking whether dependencies have been started--> <dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface" check="false"/> </beans>
client call, HelloClient.java:
package com.sawshaw.dubbo_client; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sawshaw.dubbo_interface.HelloInterface; import com.sawshaw.dubbo_interface.HelloServerClientInterface; public class HelloClient{ public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloInterface hs = (HelloInterface) context.getBean("helloService"); String result = hs.sayHello("dubbo"); System.out.println( "client output...."+result); HelloServerClientInterface hi = (HelloServerClientInterface) context.getBean("hiService"); String result1 = hi.sayHi("dubbo"); System.out.println( "client output...."+result1); } }
Client calls server-client, server-client calls server, so server-client is both client and server.
Start-up Inspection
By default, Dubbo checks the availability of dependent services at startup, throws exceptions when unavailable, and prevents Spring initialization from completing so that problems can be detected as early as possible when online. By default, check="true". To be converted into
<! -- Generate a remote service proxy that uses Hello service check= "false" to start without checking whether the dependencies have been started, just like a local bean - > <dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface" check="false"/>
When an application is both a provider and a consumer, it is bound to configure the name of the application separately. When the application is started, an error will be reported:
java.lang.IllegalStateException: Duplicate application configs: <dubbo:application name="XXX" id="XXX" /> and <dubbo:application name="XXXX" id="XXXX" />
The solution is to add default="false" to one of the applications, for example:
<dubbo:application name="consumer" default="false"/>
Code Address: Links: https://pan.baidu.com/s/1h7pNFCSprX51CwUv7vHPFw Password: cu6r
Reprinted at: https://www.cnblogs.com/JAYIT/p/9645728.html