美文网首页
spring boot整合dubbo并直连提供者

spring boot整合dubbo并直连提供者

作者: nextliving | 来源:发表于2018-11-25 08:28 被阅读498次

在上一篇spring boot整合dubbo入门中使用了zookeeper注册中心,本文将不使用zookeeper注册中心,而是让服务提供者和服务消费者直连,并且先给出一个不完善的方案,并在最后给出改进的方案。本文包含2个工程dubbo-provider-demo和dubbo-consumer-demo。注意,直连提供者只能在测试环境使用,生产环境严禁使用。

dubbo-provider-demo工程

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ms</groupId>
    <artifactId>dubbo-provider-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>dubbo-provider-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
        </dependency>
        
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

然后是编写HelloService:

package com.ms.dubbo.service;
/**
 * @author iengchen
 * @since 2018-11-24
 */
public interface HelloService {
    public String sayHello(String name);
}

可以看到该接口只包含一个方法。注意:dubbo-consumer-demo工程也要包含这个接口,并且路径最好一样,也就是最好也放在dubbo-consumer-demo工程的com.ms.dubbo.service这个包下面。

然后编写接口的实现:

package com.ms.dubbo.service;

import com.alibaba.dubbo.config.annotation.Service;
/**
 * @author iengchen
 * @since 2018-11-24
 */
@Service
public class HelloServiceImpl implements HelloService{
    @Override
    public String sayHello(String name) {
        return "hello,"+name;
    }
}

注意:实现类必须要有dubbo提供的注解@Service

application.properties内容如下:


# Spring boot application
spring.application.name = dubbo-provider-demo
server.port = 9090

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages  = com.ms.dubbo.service  
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345
## RegistryConfig Bean
#dubbo.registry.id = my-registry
#dubbo.registry.address = zookeeper://127.0.0.1:2181

dubbo.registry.id = my-registry
dubbo.registry.address = N/A

dubbo.registry.address = N/A使用的N/A表示不使用注册中心。注意:刚才编写的接口和实现类都放在com.ms.dubbo.service这个package下面,因此这里需要配置dubbo.scan.basePackages = com.ms.dubbo.service

dubbo-consumer-demo工程

pom.xml内容和dubbo-provider-demo工程的pom.xml一样。
application.properties内容如下:

# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

然后是接口HelloService,将dubbo-provider-demo中的HelloService拷贝过来即可。
最后是编写一个controller:

package com.ms.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.dubbo.service.HelloService;

/**
 * @author iengchen
 * @since 2018-11-24
 */

@RestController
public class HelloController {
    @Reference(application = "${dubbo.application.id}",
            url = "dubbo://localhost:12345")
    HelloService service;

    @RequestMapping("/say")
    public String sayHello(@RequestParam String name) {
        System.out.println("用户名是:"+name);
        
        return service.sayHello(name);
    }

}

这里使用dubbo提供的注解@Reference引用远程服务。因为没有使用注册中心,@Reference中必须提供url供消费者进行服务直连。

编写完dubbo-provider-demo和dubbo-consumer-demo这2个工程以后,可以启动工程了。

启动provider

使用run as->spring boot app启动:

2018-11-25 08:27:26.256  INFO 88875 --- [           main] c.a.dubbo.common.logger.LoggerFactory    : using logger: com.alibaba.dubbo.common.logger.slf4j.Slf4jLoggerAdapter
2018-11-25 08:27:26.260  INFO 88875 --- [           main] a.b.d.c.e.WelcomeLogoApplicationListener : 

 :: Dubbo Spring Boot (v0.2.0) : https://github.com/apache/incubator-dubbo-spring-boot-project
 :: Dubbo (v2.6.2) : https://github.com/apache/incubator-dubbo
 :: Google group : dev@dubbo.incubator.apache.org

2018-11-25 08:27:26.262  INFO 88875 --- [           main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.id=dubbo-provider-demo, dubbo.application.name=dubbo-provider-demo, dubbo.protocol.id=dubbo, dubbo.protocol.name=dubbo, dubbo.protocol.port=12345, dubbo.registry.address=N/A, dubbo.registry.id=my-registry, dubbo.scan.basePackages=com.ms.dubbo.service  }

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-11-25 08:27:26.350  INFO 88875 --- [           main] com.ms.DubboProviderDemoApplication      : Starting DubboProviderDemoApplication on MacBookPro with PID 88875 (/Users/chenxin/Eclipse-Workspace/default/dubbo-provider-demo/target/classes started by chenxin in /Users/chenxin/Eclipse-Workspace/default/dubbo-provider-demo)
2018-11-25 08:27:26.350  INFO 88875 --- [           main] com.ms.DubboProviderDemoApplication      : No active profile set, falling back to default profiles: default
2018-11-25 08:27:26.391  INFO 88875 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6ac13091: startup date [Sun Nov 25 08:27:26 CST 2018]; root of context hierarchy
2018-11-25 08:27:26.755  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : dubbo-provider-demo, class : com.alibaba.dubbo.config.ApplicationConfig] has been registered.
2018-11-25 08:27:26.756  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : dubbo-provider-demo] has been registered.
2018-11-25 08:27:26.756  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : my-registry, class : com.alibaba.dubbo.config.RegistryConfig] has been registered.
2018-11-25 08:27:26.756  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : my-registry] has been registered.
2018-11-25 08:27:26.756  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : dubbo, class : com.alibaba.dubbo.config.ProtocolConfig] has been registered.
2018-11-25 08:27:26.757  INFO 88875 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : dubbo] has been registered.
2018-11-25 08:27:26.804  INFO 88875 --- [           main] b.f.a.ServiceAnnotationBeanPostProcessor :  [DUBBO] BeanNameGenerator bean can't be found in BeanFactory with name [org.springframework.context.annotation.internalConfigurationBeanNameGenerator], dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:26.805  INFO 88875 --- [           main] b.f.a.ServiceAnnotationBeanPostProcessor :  [DUBBO] BeanNameGenerator will be a instance of org.springframework.context.annotation.AnnotationBeanNameGenerator , it maybe a potential problem on bean name generation., dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:26.813  WARN 88875 --- [           main] b.f.a.ServiceAnnotationBeanPostProcessor :  [DUBBO] The BeanDefinition[Root bean: class [com.alibaba.dubbo.config.spring.ServiceBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] of ServiceBean has been registered with name : ServiceBean:helloServiceImpl:com.ms.dubbo.service.HelloService, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:26.814  INFO 88875 --- [           main] b.f.a.ServiceAnnotationBeanPostProcessor :  [DUBBO] 1 annotated Dubbo's @Service Components { [Bean definition with name 'helloServiceImpl': Generic bean: class [com.ms.dubbo.service.HelloServiceImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/chenxin/Eclipse-Workspace/default/dubbo-provider-demo/target/classes/com/ms/dubbo/service/HelloServiceImpl.class]] } were scanned under package[com.ms.dubbo.service], dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:26.814  WARN 88875 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2018-11-25 08:27:27.005  INFO 88875 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [com.alibaba.boot.dubbo.autoconfigure.RelaxedDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:27:27.008  INFO 88875 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [com.alibaba.boot.dubbo.autoconfigure.RelaxedDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:27:27.009  INFO 88875 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [com.alibaba.boot.dubbo.autoconfigure.RelaxedDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:27:27.342  INFO 88875 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2018-11-25 08:27:27.366  INFO 88875 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-25 08:27:27.367  INFO 88875 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-11-25 08:27:27.375  INFO 88875 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/chenxin/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-11-25 08:27:27.449  INFO 88875 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-25 08:27:27.449  INFO 88875 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1060 ms
2018-11-25 08:27:27.517  INFO 88875 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-25 08:27:27.520  INFO 88875 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-25 08:27:27.521  INFO 88875 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-25 08:27:27.521  INFO 88875 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-11-25 08:27:27.521  INFO 88875 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-25 08:27:27.601  INFO 88875 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:27:27.725  INFO 88875 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6ac13091: startup date [Sun Nov 25 08:27:26 CST 2018]; root of context hierarchy
2018-11-25 08:27:27.773  INFO 88875 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-25 08:27:27.774  INFO 88875 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-11-25 08:27:27.794  INFO 88875 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:27:27.794  INFO 88875 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:27:27.836  INFO 88875 --- [           main] .f.a.DubboConfigBindingBeanPostProcessor : The properties of bean [name : dubbo-provider-demo] have been binding by prefix of configuration properties : dubbo.application
2018-11-25 08:27:27.843  INFO 88875 --- [           main] .f.a.DubboConfigBindingBeanPostProcessor : The properties of bean [name : my-registry] have been binding by prefix of configuration properties : dubbo.registry
2018-11-25 08:27:27.854  INFO 88875 --- [           main] .f.a.DubboConfigBindingBeanPostProcessor : The properties of bean [name : dubbo] have been binding by prefix of configuration properties : dubbo.protocol
2018-11-25 08:27:28.090  INFO 88875 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-11-25 08:27:28.100  INFO 88875 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] The service ready on spring started. service: com.ms.dubbo.service.HelloService, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:28.157  INFO 88875 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Export dubbo service com.ms.dubbo.service.HelloService to local registry, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:28.157  INFO 88875 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Export dubbo service com.ms.dubbo.service.HelloService to url dubbo://192.168.0.103:12345/com.ms.dubbo.service.HelloService?anyhost=true&application=dubbo-provider-demo&bind.ip=192.168.0.103&bind.port=12345&dubbo=2.6.2&generic=false&interface=com.ms.dubbo.service.HelloService&methods=sayHello&pid=88875&side=provider&timestamp=1543105648105, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:28.240  INFO 88875 --- [           main] c.a.d.remoting.transport.AbstractServer  :  [DUBBO] Start NettyServer bind /0.0.0.0:12345, export /192.168.0.103:12345, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:27:28.257  INFO 88875 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2018-11-25 08:27:28.262  INFO 88875 --- [           main] com.ms.DubboProviderDemoApplication      : Started DubboProviderDemoApplication in 2.171 seconds (JVM running for 2.638)

启动consumer

使用run as->spring boot app启动:

2018-11-25 08:28:03.775  INFO 88881 --- [           main] c.a.dubbo.common.logger.LoggerFactory    : using logger: com.alibaba.dubbo.common.logger.slf4j.Slf4jLoggerAdapter
2018-11-25 08:28:03.779  INFO 88881 --- [           main] a.b.d.c.e.WelcomeLogoApplicationListener : 

 :: Dubbo Spring Boot (v0.2.0) : https://github.com/apache/incubator-dubbo-spring-boot-project
 :: Dubbo (v2.6.2) : https://github.com/apache/incubator-dubbo
 :: Google group : dev@dubbo.incubator.apache.org

2018-11-25 08:28:03.782  INFO 88881 --- [           main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.id=dubbo-consumer-demo, dubbo.application.name=dubbo-consumer-demo, dubbo.protocol.id=dubbo, dubbo.protocol.name=dubbo, dubbo.protocol.port=12345}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-11-25 08:28:03.870  INFO 88881 --- [           main] com.ms.DubboConsumerDemoApplication      : Starting DubboConsumerDemoApplication on MacBookPro with PID 88881 (/Users/chenxin/Eclipse-Workspace/default/dubbo-consumer-demo/target/classes started by chenxin in /Users/chenxin/Eclipse-Workspace/default/dubbo-consumer-demo)
2018-11-25 08:28:03.870  INFO 88881 --- [           main] com.ms.DubboConsumerDemoApplication      : No active profile set, falling back to default profiles: default
2018-11-25 08:28:03.906  INFO 88881 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4313f5bc: startup date [Sun Nov 25 08:28:03 CST 2018]; root of context hierarchy
2018-11-25 08:28:04.285  INFO 88881 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : dubbo-consumer-demo, class : com.alibaba.dubbo.config.ApplicationConfig] has been registered.
2018-11-25 08:28:04.286  INFO 88881 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : dubbo-consumer-demo] has been registered.
2018-11-25 08:28:04.286  INFO 88881 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : dubbo, class : com.alibaba.dubbo.config.ProtocolConfig] has been registered.
2018-11-25 08:28:04.286  INFO 88881 --- [           main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : dubbo] has been registered.
2018-11-25 08:28:04.473  INFO 88881 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration' of type [com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration$$EnhancerBySpringCGLIB$$8630ca83] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:28:04.526  INFO 88881 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [com.alibaba.boot.dubbo.autoconfigure.RelaxedDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:28:04.528  INFO 88881 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [com.alibaba.boot.dubbo.autoconfigure.RelaxedDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-25 08:28:04.875  INFO 88881 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-11-25 08:28:04.897  INFO 88881 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-25 08:28:04.897  INFO 88881 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-11-25 08:28:04.904  INFO 88881 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/chenxin/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-11-25 08:28:04.993  INFO 88881 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-25 08:28:04.993  INFO 88881 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1089 ms
2018-11-25 08:28:05.034  INFO 88881 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-25 08:28:05.036  INFO 88881 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-25 08:28:05.036  INFO 88881 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-25 08:28:05.037  INFO 88881 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-11-25 08:28:05.037  INFO 88881 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-25 08:28:05.187  INFO 88881 --- [           main] .f.a.DubboConfigBindingBeanPostProcessor : The properties of bean [name : dubbo-consumer-demo] have been binding by prefix of configuration properties : dubbo.application
2018-11-25 08:28:05.356  INFO 88881 --- [           main] c.a.d.remoting.transport.AbstractClient  :  [DUBBO] Successed connect to server /192.168.0.103:12345 from NettyClient 192.168.0.103 using dubbo version 2.6.2, channel is NettyChannel [channel=[id: 0x7da10b5b, /192.168.0.103:49686 => /192.168.0.103:12345]], dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:28:05.356  INFO 88881 --- [           main] c.a.d.remoting.transport.AbstractClient  :  [DUBBO] Start NettyClient MacBookPro/192.168.0.103 connect to the server /192.168.0.103:12345, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:28:05.386  INFO 88881 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Refer dubbo service com.ms.dubbo.service.HelloService from url dubbo://localhost:12345/com.ms.dubbo.service.HelloService?application=dubbo-consumer-demo&dubbo=2.6.2&interface=com.ms.dubbo.service.HelloService&methods=sayHello&pid=88881&register.ip=192.168.0.103&side=consumer&timestamp=1543105685191, dubbo version: 2.6.2, current host: 192.168.0.103
2018-11-25 08:28:05.391  INFO 88881 --- [           main] c.a.d.c.s.b.f.a.ReferenceBeanBuilder     : <dubbo:reference object="com.alibaba.dubbo.common.bytecode.proxy0@1d035be3" singleton="true" url="dubbo://localhost:12345" interface="com.ms.dubbo.service.HelloService" uniqueServiceName="com.ms.dubbo.service.HelloService" generic="false" id="com.ms.dubbo.service.HelloService" /> has been built.
2018-11-25 08:28:05.441  INFO 88881 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:28:05.563  INFO 88881 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4313f5bc: startup date [Sun Nov 25 08:28:03 CST 2018]; root of context hierarchy
2018-11-25 08:28:05.607  INFO 88881 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/say]}" onto public java.lang.String com.ms.controller.HelloController.sayHello(java.lang.String)
2018-11-25 08:28:05.610  INFO 88881 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-25 08:28:05.611  INFO 88881 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-11-25 08:28:05.630  INFO 88881 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:28:05.630  INFO 88881 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-25 08:28:05.670  INFO 88881 --- [           main] .f.a.DubboConfigBindingBeanPostProcessor : The properties of bean [name : dubbo] have been binding by prefix of configuration properties : dubbo.protocol
2018-11-25 08:28:05.740  INFO 88881 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-11-25 08:28:05.769  INFO 88881 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-25 08:28:05.772  INFO 88881 --- [           main] com.ms.DubboConsumerDemoApplication      : Started DubboConsumerDemoApplication in 2.16 seconds (JVM running for 2.598)

浏览器访问

2个服务都启动成功以后,可以打开浏览器访问http://127.0.0.1:8080/say?name=iengchen,如果界面显示hello,iengchen说明服务直连正常。

改进方案

以上的方法有一个问题,那就是@Reference中必须配置url,但是发布到生产环境还需要去掉url。加入代码中有多处使用了@Reference,就必须修改多处。因此,可以使用另外一种方式,不需要修改@Reference。这种方式是在${user.home}路径下建立一个名为dubbo-resolve.properties的文件,里面配置需要直连的接口。dubbo服务启动时默认会自动加载${user.home}/dubbo-resolve.properties文件,不需要在我们开发的应用中做任何配置,参考直连提供者.

改进具体步骤

在我的用户目录下建立/Users/chenxin/dubbo-resolve.properties文件,文件内容为:

com.ms.dubbo.service.HelloService=dubbo://localhost:12345

同时修改dubbo-consumer-demo工程下的HelloController为:

package com.ms.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.dubbo.service.HelloService;

/**
 * @author iengchen
 * @since 2018-11-24
 */

@RestController
public class HelloController {

    @Reference
    HelloService service;
    
    @RequestMapping("/say")
    public String sayHello(@RequestParam String name) {
        System.out.println("用户名是:"+name);
        
        return service.sayHello(name);
    }

}

然后重新启动dubbo-provider-demo和dubbo-consumer-demo工程。

参考

相关文章

网友评论

      本文标题:spring boot整合dubbo并直连提供者

      本文链接:https://www.haomeiwen.com/subject/dbguqqtx.html