Dubbo to use the environment to build

And the integration of traditional ssm - write XML configuration files

Build a service provider and service consumer, consumer service across applications to achieve long-distance call service provider

Public extraction module

  • Public extraction module

Consumer long-distance call service provider of services, at least he should get references that class to provide services in the service provider, one that consumers and service of one, if the cluster will double, so the extraction public module, storage classes and interfaces for public use

common

service provider

  • rely
// 自定义的公共模块
<dependency>
    <groupId>com.changwu</groupId>
    <artifactId>commom</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.3</version>
</dependency>
  • Writing service business Service, the specific implementation of the interface
public class UserServiceImpl implements UserService {
    public List<UserAddress> getUserAddressList() {
        UserAddress a1=   new UserAddress(1,"张三","北京市朝阳区");
        UserAddress a2=    new UserAddress(2,"李四","山东济南");
        return Arrays.asList(a1,a2);
    }
}
  • Write a configuration file provider.xml, intended as a service provider will be registered into its own registry, exposing themselves to a specific interface to provide services for the consumer services
<?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">

    <!-- 暴露服务提供者, name=服务名(不能和其他服务名重复) -->
    <dubbo:application name="user-service-provider"  />

    <!-- z指定zookeeper的地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 指定通信规则,通信协议, 通信端口. 服务消费者和dubbo之间的通信-->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口  ref=服务的真正实现, 下面使用<bean>调用-->
    <dubbo:service interface="com.changwu.service.UserService" ref="userService" />

    <!-- 接口的实现 -->
    <bean id="userService" class="com.changwu.service.impl.UserServiceImpl" />

    <dubbo:monitor protocol="registry"></dubbo:monitor>

</beans>

Reference Manual configuration schema , click the View all tabs, and a detailed explanation

Added: property configuration rewritten to cover priority

priority

  • When you start using the -D parameter set the highest priority JVM
  • Followed, dubbo.xml
  • Again the dubbo.properties

The following is a typical sample configuration dubbo.properties.

dubbo.application.name=foo
dubbo.application.owner=bar
dubbo.registry.address=10.20.153.10:9090
  • idea to set the virtual machine startup parameters

Virtual machine parameter adjustment


  • log4j configuration file (if not add a warning)
###set log levels###
log4j.rootLogger=info, stdout
###output to console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

Consumer services

  • rely
// 自定义的公共模块
<dependency>
    <groupId>com.changwu</groupId>
    <artifactId>commom</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
// zk的客户端依赖 curator
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.13.0</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.13</version>
</dependency>
  • Write consumer.xml

Write your own service profile name, configure address registry, and then pull a list of services to the registry, the current configuration of consumers used which interfaces are implemented by remote RPC call, you can also configure the monitoring center to see services health, call each other circumstances, do not forget to use Spring annotations have to configure the scanning package

<?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">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="order-service"  />

    <!--注册中心地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成远程服务代理,可以和本地bean一样使用 userService -->
    <dubbo:reference id="userService" interface="com.changwu.service.UserService" />
    
    <!--添加包扫描-->
    <context:component-scan base-package="com.changwu"></context:component-scan>

    <!--配置监控中心,有下面两种方式 1. 去注册中心自动发现, 2. 直连模式-->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
   <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>-->

</beans>
  • Consumers call service providers that implement the
    following @Service notes using native Spring
@Service // 没有用dubbo的service注解
public class OrderServiceImpl implements OrderService {
    @Autowired
    UserService userService;

    public List initOrder(String userId) {
        // 查询用户的地址
        System.out.println("userId == "+userId);
        List<UserAddress> userAddressList = userService.getUserAddressList("1");
        for (UserAddress userAddress : userAddressList) {
            System.out.println(userAddress.getAdress());
        }
        return userAddressList;
    }
}
  • Start the test
public class MainApp {
public static void main(String[] args) {
    ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
    OrderService orderService = ioc.getBean(OrderService.class);
    orderService.initOrder("1");
    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Integration Springboot

Public module

Also reuse classes, interfaces into and then referenced in other modules

service provider

  • rely
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.changwu</groupId>
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<!--
    dubbo整合进springboot
    zookeeper,curate它都已经导入进来了
 -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
  • The configuration file to replace provider.xml application.properties

Xml possible to combine the tag and attribute names, using '.' Separator. One attribute per line

# 指定当前的服务, 一般为了防止重复,设置成当前module的名字
dubbo.application.name=provider
# 指定注册中心的地址
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
# dubbo 使用的协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 原配置文件中暴露服务的名字,使用类似下面的方式,但是在SpringBoot中使用 @Service注解(dubbo的)
# dubbo.service.interface=com.changwu.service.UserService
# 监控中心
dubbo.monitor.protocol=registry
server.port=8082
  • Service provider interface to achieve
    dubbo @Service use interface
@Component
@Service //使用dubbo的Service 对外保留服务
public class UserServiceImpl implements UserService {
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress a1=   new UserAddress(1,"张三","北京市朝阳区");
        UserAddress a2=   new UserAddress(2,"李四","山东济南");
        return Arrays.asList(a1,a2);
    }
}
  • Start class, open dubbo configuration
@EnableDubbo
@SpringBootApplication
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class);
    }
}

Consumer services

  • rely
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.changwu</groupId>
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
  • Use application.properties profile replacement consumer.xml
# 告诉注册中心自己的名字
dubbo.application.name=consumer
# 注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 配置监控中心
dubbo.monitor.protocol=registry
# 原来在配置文件中使用 dubbo:reference 配置远程调用的接口,现在用注解
server.port=8081
  • Remote call @Reference dubbo comment
@Service
public class OrderServiceImpl implements OrderService{
    //@Autowired
    @Reference // dubbo的注解
    UserService userService;
    public  List<UserAddress> initOrder(String userId) {
        return  userService.getUserAddressList("1");
    }
}
  • Written Controller
  • Start class, open dubbo configuration
@EnableDubbo
@SpringBootApplication
public class MainApp {
    public static void main(String[] args) {
        SpringApplication.run(MainApp.class);
    }
}

Other common configuration

Example , dubbo common configuration example

Start checking

Consumer services in the start is going to check whether they require a remote call service has been registered to the registry, once checked into the registry without checking to call this method even if no error will automatically start and stop the Spring , in the development, you can choose to manually turn off this configuration

  • Configuration file
配置某个服务启动时检查
在服务消费者中,关闭某个服务的启动时检查 (没有提供者时报错):
<dubbo:reference interface="com.foo.BarService" check="false" />

统一配置全部服务启动时不检查
关闭所有服务的启动时检查 (没有提供者时报错):
<dubbo:consumer check="false" />

默认是true. 表示注册中心不存在时报错
关闭注册中心启动时检查 (注册订阅失败时报错):
<dubbo:registry check="false" />
  • By dubbo.properties
dubbo.reference.com.foo.BarService.check=false
dubbo.reference.check=false
dubbo.consumer.check=false
dubbo.registry.check=false
  • By JVM parameters
java -Ddubbo.reference.com.foo.BarService.check=false
java -Ddubbo.reference.check=false
java -Ddubbo.consumer.check=false 
java -Ddubbo.registry.check=false

Timeout settings

Consumer long-distance call service providers, service providers realize the logic there may be a lot of time-consuming operation, it is likely there are a lot of threads blocked on this method, timeout setting to solve this problem within a specified time once, still did not return any data, it will return immediately to ensure system available

Defaults The timeout, and dubbo: cosumer default timeout is 1000ms

Below, set the timeout to 3 seconds

<!-- 生成远程服务代理,可以和本地bean一样使用 userService -->
<dubbo:reference id="userService" interface="com.changwu.service.UserService" timeout="3000"/>

Timeout priority

From the figure above, I marked their role, the highest priority is the method level, interfaces, followed by the lowest global level - the same level of consumer set priorities, followed provider settings

number of retries

General timeout configuration timeout and retry count retries conjunction

After the call fails, it will try again depending on the number of retries remote calls, the number of retries does not include for the first time, if we configure three, count for the first time, try to call up to four times

Clusters, cluster polling consumers will be retried if the service provider exists

  • Idempotent times
    • Query, delete, modify (characterized by the same request, the same parameters regardless of how many times the execution, the result is the same), set the number of retries
  • Non-idempotent
    • Added, the number is not provided idempotent, retries = 0
<!-- 生成远程服务代理,可以和本地bean一样使用 userService -->
<dubbo:reference id="userService" interface="com.changwu.service.UserService" retries="3" timeout="3000"/>

Multiple versions

We give some of the features of the system upgrade, but can not put it all at once after the upgrade code applies to all machines in the cluster, multi-version control, a small part of the first cluster of machines to upgrade, no not available then gradually expand the scale of the phenomenon, the final completion replace all

Old and new versions of the service providers of all inherited the same interface com.foo.BarService , by ref points to a different realization Bean

老版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="1.0.0" ref="userService1" />
<bean id="userService1" class="xxx">

新版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="2.0.0" ref="userService2"/>
<bean id="userService2" class="yyy">
老版本服务消费者配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />

新版本服务消费者配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />

如果不需要区分版本,可以按照以下的方式配置 [1]:
<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />

Local stub

If you want to send in the consumer services prior to remote procedure call other methods to verify parameters such as, empty sentenced to treatment, you can choose to use a local stub

Such as, UserService is a service provider implementation, consumers call the remote service, as follows consumers provide their own implementation locally, and determine the parameters of legality

public class UserServiceImpl implements UserService{

    // 注入这个被服务提供者支持的接口
    private final UserService userService;

    // 提供构造函数
    public UserServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
         // 判空
        if (!StringUtils.isEmpty(userId)){
            return userService.getUserAddressList(userId);
        }
        return null;
    }
}
  • Profiles
在 spring 配置文件中按以下方式配置:
# 是消费者在本地自己的实现 com.changwu.service.UserServiceImpl
<dubbo:service interface="com.changwu.service.UserServiceImpl" stub="true" />
或

<dubbo:service interface="com.foo.BarService" stub="com.changwu.service.UserServiceImpl" />

Dubbo and SpringBoot integration of three ways

method 1

  • Introducing dubbo-starter
    • Use @EnableDubbo (scanning rule specified package) disposed on the main class
    • Configuration properties in application.properties
    • Service Provider: Exposing services using @Service
    • Consumer Services: Use @Reference reference service

Method 2

If you want to add, such as method-level configuration, leave dubboxml profile

  • Remove @EnableDubbo notes on startup class
  • Add @ImportResource on startup class (localtion = "classpath: provider / consumer.xml")
  • Application.properties remove the related configuration dubbo
  • Service providers use provider.xml profile
  • Consumer services are consumer.xml profile

Method 3 Notes Configuration

2.6.3 is required dubbo

Guess you like

Origin www.cnblogs.com/ZhuChangwu/p/11564221.html