解决rpc项目中项目之间的调用问题.
消费服务模式.
maven包:
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-aop
com.alibaba
dubbo
2.5.7
org.apache.curator
curator-recipes
4.0.0
org.apache.zookeeper
zookeeper
3.5.3-beta
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
provider:服务提供方
springApplication.java(扫描服务包)
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan(basePackages = "com.guanheng.service")
public class ApplicationBooter {
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(ApplicationBooter.class);
application.run(args);
}
}
configuretion.java(bean注入)
package com.guanheng;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class ApplicationConfiguretion extends WebMvcConfigurerAdapter{
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("provider-test");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
}
serviceimpl.java(服务类)
package com.guanheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.guanheng.service.AnnotateService;
@Service(timeout = 5000)
public class AnnotateServiceImpl implements AnnotateService{
@Override
public String provider(){
return "Hello Word";
}
}
consumer(消费方)
springApplication.java(扫描服务包) 同上
configuretion.java
package com.guanheng;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class ApplicationConfiguretion extends WebMvcConfigurerAdapter{
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("consumer-test");
return applicationConfig;
}
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setTimeout(3000);
return consumerConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
}
consumerServiceImpl.java(@Reference 引用服务类)
package com.guanheng.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.guanheng.service.AnnotateConsumerService;
import com.guanheng.service.AnnotateService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class AnnotateConsumerServiceImpl implements AnnotateConsumerService {
@Reference
private AnnotateService annotateService;
@Override
public void showResule() {
System.out.println(annotateService.provider());
}
}
HelloWordController.java(url访问测试)
package com.guanheng.controller;
import com.guanheng.service.AnnotateConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private AnnotateConsumerService annotateConsumerService;
@RequestMapping("/hello")
public String hello(){
annotateConsumerService.showResule();
return "Hello Word";
}
}
效果如下