Getting Case

(A) Basic Configuration

provider.xml:

<! - 1. Specify the current service name -> 
< Dubbo: the Application name = "the User-Service-Provider" > </ : Dubbo the Application > 

<! - 2. designated registration center -> 
< Dubbo: Registry Protocol = "ZooKeeper" address = "127.0.0.1:2181" > </ Dubbo: Registry > 

<-! 3. specify the communication rule (communication protocol, a communication port) -> 
< Dubbo: protocol name = "Dubbo" port = "20880" > </ Dubbo: Protocol > 

! <- 4. Exposing services ref points to the real -> 
< Dubbo:service interface="com.jcx.gmall.service.UserService" ref="userServiceImpl01"></dubbo:service>

<!--服务实现-->
<bean id="userServiceImpl" class="com.jcx.gmall.service.impl.UserServiceImpl"></bean>

<!--连接监控中心-->
<dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>

consumer.xml:

< Context: the Component-Scan Base-Package Penalty for = "com.jcx.gmall.service.impl" > </ context: the Component-Scan > 

<-! 1. Specify the current service name -> 
< Dubbo: the Application name = " Consumer-Service--Order " > </ Dubbo: file application > 

<-! 2. specify registries -> 
< Dubbo: Registry address =" ZooKeeper: //127.0.0.1: 2181 " > </ Dubbo: Registry > 

< ! - 3. generate remote service agent Interface -> 
< Dubbo: Reference interface = "com.jcx.gmall.service.UserService"id="userService"></Dubbo: Reference > 

<-! 4. Connection Monitoring Center -> 
< Dubbo: Monitor address = "127.0.0.1:7070" > </ Dubbo: Monitor >

(B) labels

The official document: http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

 

 

 

priority:

a. Method grade> interface level> Global
b. Consumer Configuration> Provider Configuration

 

 

 

 

a. retries (retries)


The first call is not included, 0 for no retries
set number of retries: query, delete, modify
can not set the number of retries: What's New

<dubbo:service retries="5" />

<dubbo:reference retries="5" />

<dubbo:reference>
<dubbo:method name="getUserAddressList" retries="5" />
</dubbo:reference>

b. 超时时间(timeout)


默认是1000ms

消费者:

<dubbo:consumer timeout="5000" />

<dubbo:reference interface="com.jcx.gmall.service.UserService" timeout="2000">
<dubbo:method name="getUserAddressList" timeout="3000" />
</dubbo:reference>

提供者:

<dubbo:provider timeout="5000" />

<dubbo:provider interface="com.jcx.gmall.service.UserService" timeout="2000">
<dubbo:method name="getUserAddressList" timeout="3000" />
</dubbo:provider>

c. 版本号(version)
消费者:

<!-- old -->
<dubbo:reference id="userService" interface="com.jcx.gmall.service.UserService" version="1.0.0" />

<!-- new -->
<dubbo:reference id="userService" interface="com.jcx.gmall.service.UserService" version="2.0.0" />

<!-- 不区分版本 -->
<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />

提供者:

<!-- old -->
<dubbo:service interface="com.jcx.gmall.service.UserService" version="1.0.0" />

<!-- new -->
<dubbo:service interface="com.jcx.gmall.service.UserService" version="2.0.0" />

(三)代码实现

1. API:

bean:

public class UserAddress implements Serializable {

    private Integer id;
    private String userAddress;
    private String userId;
    private String consignee;
    private String phoneNum;
    private String isDefault;

    public UserAddress() {
        super();
        // TODO Auto-generated constructor stub
    }

    public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
                       String isDefault) {
        super();
        this.id = id;
        this.userAddress = userAddress;
        this.userId = userId;
        this.consignee = consignee;
        this.phoneNum = phoneNum;
        this.isDefault = isDefault;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }


}

service:

public interface OrderService {

    public void initOrder(String userId);

}
public interface UserService {
    
    public List<UserAddress> getUserAddressList(String userId);

}

2. provide:

service.impl

public class UserServiceImpl implements UserService {

    public List<UserAddress> getUserAddressList(String userId) {

        UserAddress address1 = new UserAddress(1, "11111", "1", "111", "11111111", "Y");
        UserAddress address2 = new UserAddress(2, "22222", "2", "222", "22222222", "N");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Arrays.asList(address1, address2);
    }

}

Application:

public class MainApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();
        System.in.read();
    }
}

provide.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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--1.指定当前服务名-->
    <dubbo:application name="user-service-provider"></dubbo:application>

    <!--2.指定注册中心-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

    <!--3.指定通信规则(通信协议,通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

    <!--4.暴露服务   ref指向真正实现-->
    <dubbo:service interface="com.jcx.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0">
        <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
    </dubbo:service>

    <!--服务实现-->
    <bean id="userServiceImpl" class="com.jcx.gmall.service.impl.UserServiceImpl"></bean>


    <!--连接监控中心-->
    <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>
</beans>

 

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.jcx.dubbo.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.jcx.dubbo.demo</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!--引入zookeeper-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

</project>

 

3. consumer

service.impl:

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

    public void initOrder(String userId) {
        System.out.println("用户id:" + userId);
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
    }
}

Application:

public class MainApplication {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        OrderService orderService = applicationContext.getBean(OrderService.class);
        orderService.initOrder("1");
        System.out.println("调用完成....");
        System.in.read();
    }
}

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://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.jcx.gmall.service.impl"></context:component-scan>

    <dubbo:application name="order-service-consumer"></dubbo:application>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

    <!--生成远程服务代理接口  check:启动时是否检查-->
    <dubbo:reference interface="com.jcx.gmall.service.UserService" id="userService" timeout="5000" retries="3" version="*">
    </dubbo:reference>

    <!--配置消费者统一规则-->
    <dubbo:consumer check="false">

    </dubbo:consumer>

    <!--连接监控中心-->
    <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>
</beans>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.jcx.dubbo.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.jcx.dubbo.demo</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!--引入zookeeper-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

</project>

Guess you like

Origin www.cnblogs.com/s-star/p/12050943.html