dubbo 와 spring boot 통합 심 플 버 전(xml 와 annotation)

8498 단어 dubbo
spring boot 와 dubbo 통합 xml+annotation 버 전
(spring 과 dubbo 간 의 통합 인 것 같 습 니 다)provider 요점:1.dubbo.properties 설정 dubbo 의 각종 매개 변수 application,registry.addres dubbo.provider.xml 여 기 는라벨 을 사용 하여 dubbo.properties 의 매개 변 수 를 가 져 오고 provider 사례 를 출력 합 니 다.
"${dubbo.application.name}"/>
    "${dubbo.registry.protocol}" address="${dubbo.registry.address}"/>
    "${dubbo.protocol.name}" port="${dubbo.protocol.port}"/>
    "com.zhb.app.dubbo.Hello" ref="helloImpl"/>

그 중에서 helloImpl 은 코드 에서 annotation 정 의 를 사용 하 는 인 스 턴 스 입 니 다.2.spring boot 에서 아래 annotation 을 사용 하여 설정 파일 을 가 져 옵 니 다.
@Configuration
@PropertySource("classpath:dubbo/dubbo.properties")  
@ImportResource({ "classpath:dubbo/*.xml" }) 
public class DubboConfig {

}

다음 3.build.gradle 가 정의 하 는 제3자 Jar 는 spring boot 와 관련 된 것 을 제외 하고 dubbo 와 zkclient,zkclient 가 없 으 면 프로젝트 시작 이 잘못 되 었 습 니 다.왜 필요 한 지 모 르 겠 어 요.그리고 consumer 와 provider 양쪽 에 공 통 된 api jar.
consumer 요점 1.dubbo.properties 노출 감소 서비스 방식 의 설정 dubbo-consumer.xml
  
    <dubbo:application name="${dubbo.application.name}" />  
      
   <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />    
    <dubbo:reference id="helloImpl" interface="com.zhb.app.dubbo.Hello" />  

그리고 코드 에@autowire 2.annotation 설정 파일 을 도입 합 니 다.3.build.gradle 동상
spring boot 와 dubbo 는 순수 annotation 버 전 을 통합 합 니 다.
provider 요점:1.annotation 을 사용 하여 매개 변 수 를 도입 합 니 다.dubbo.annotation.package 라 는 매개 변 수 는 비교적 중요 합 니 다.annotation 스 캔 을 사용 하여 AnnotationBean,applicationConfig 등 bean 을 사용 하여 dubbo 의 인 자 를 주입 하도록 설정 합 니 다.
@Configuration
@ConditionalOnClass(Exporter.class)
@PropertySource(value = "classpath:dubbo/dubbo.properties")
public class DubboConfiguration {

    @Value("${dubbo.application.name}")
    private String applicationName;

   @Bean
    public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage(packageName);
        return annotationBean;
    }

뒤에 코드 생략...2.spring 의@Component 와 dubbo 의@Service 를 사용 하여 예 시 를 높 인 다.
@Component
@Service(version="1.0.0")
public class HelloImpl implements Hello{
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloImpl.class);
    @Override
    public String sayHello(String name) {
        LOGGER.info("recevie invoke," + name);
        return "hello world," + name;
    }

}

다음 3.이전의 provider.xml 및 관련 클래스 는 사용 하지 않 아 도 됩 니 다.다른 설정 은 변 하지 않 습 니 다.
consumer 요점:1.annotation 을 사용 하여 매개 변 수 를 도입 합 니 다.위의 것 과 비슷 하지만 ProtocolConfig 설정 을 제거 하고 providerConfig 를 consumerConfig 로 변경 합 니 다.다른 것 은 변 하지 않 습 니 다.2.dubbo 의 reference 주 해 를 사용 하여 예제 도입
@Reference(version = "1.0.0")
    private Hello hello;

직접 사용 하면 됩 니 다.3.이전 consumer.xml 및 관련 클래스 는 사용 하지 않 아 도 됩 니 다.다른 설정 은 변 하지 않 습 니 다.
마지막 으로 build.gradle 프로필 추가
import java.lang.invoke.LambdaForm.Compiled;

buildscript{
    ext{
        springBootVersion = '1.3.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

springBoot {
    backupSource = false
    mainClass = 'com.zhb.app.PortalApplication'
}

jar {
    baseName = 'springBootTest'
    version =  '0.0.1-SNAPSHOT'
}

//apply plugin: 'application'
//applicationDefaultJvmArgs = ['-javaagent:E:\\xgsdk\\commonLib\\springloaded-1.2.5.RELEASE.jar -noverify']

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: "libs", include: "*.jar")
    compile "org.springframework.boot:spring-boot-starter-web"

    compile(
            "com.alibaba:fastjson:1.2.4",
            "commons-codec:commons-codec:1.5",
            "org.apache.commons:commons-lang3:3.3.2",
            "com.alibaba:dubbo:2.5.6"
    )

    compile("com.github.sgroschupf:zkclient:0.1")
    {
        exclude group: 'org.slf4j'
    }


    testCompile("org.springframework.boot:spring-boot-starter-test")
}

좋은 웹페이지 즐겨찾기