该项目位于地址:https://github.com/xwbGithub/springBoot-dubbo
由于该项目掺杂着基于SpringBoot的yml方式,普通注入的方式,里面的代码配置拥有好几种,此处以核心代码api注入的方式进行了配置,很多文件都不全,需要在github上拉去代码,关闭不必要的注入文件,此处即可使用。
此处提供者和消费者均为SpringBoot-dubbo项目下的子模块
parent项目pom文件
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!-- 导入dubbo的starter【starter中包括了dubbo和zookeeper】 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译版本为1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- spring-boot parent变更为依赖方式 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>gmail-interface</module>
<module>boot-order-service-consumer</module>
<module>boot-user-service-provider</module>
</modules>服务端搭建
pom文件
<dependencies>
<dependency>
<groupId>com.atguigu.gmail</groupId>
<artifactId>gmail-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>yml配置
server:
port: 8080主启动类
@SpringBootApplication
@DubboComponentScan(basePackages = "com.atguigu.gmail")
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}api注解注入配置bean
@Configuration
public class MyDubboConfig {
/**
* 相当于此bean替代了dubbo.application.* 标签
* @return applicationConfig
*/
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-service-provider");//设置应用名
return applicationConfig;
}
/**
* 设置注册中心配置
* @return
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("127.0.0.1");
registryConfig.setPort(2181);
registryConfig.setProtocol("zookeeper");
registryConfig.setCheck(false);
return registryConfig;
}
/**
* 通信协议配置项
* @return
*/
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
return protocolConfig;
}
/**
* 监控中心
* @return
*/
@Bean
public MonitorConfig monitorConfig() {
MonitorConfig monitorConfig = new MonitorConfig();
return monitorConfig;
}
/**
* 服务提供接口
* @param userService 入参的userService<br>
* 是从ioc中拿去 @Component public class UserServiceImpl implements UserService {...}
* @return
*/
@Bean
public ServiceConfig<UserService> userServiceServiceConfig(UserServiceImpl userService) {
ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(UserService.class);
serviceConfig.setRef(userService);
serviceConfig.setVersion("1.0.0");
serviceConfig.setTimeout(3000);
serviceConfig.setRetries(3);
//配置每一个method的属性
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("getUserAddressList");
methodConfig.setTimeout(3000);
methodConfig.setRetries(2);
//将method的设置关联到service的配置中
List<MethodConfig> methods = new ArrayList();
methods.add(methodConfig);
serviceConfig.setMethods(methods);
return serviceConfig;
}
/**
* providerConfig的统一配置
*
* @return
*/
@Bean
public ProviderConfig providerConfig() {
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setTimeout(3000);
providerConfig.setRetries(3);
return providerConfig;
}
}
提供者Service
@com.alibaba.dubbo.config.annotation.Service //暴露服务
@Component
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("UserServiceImpl..");
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//if (Math.random() > 0.5) {
// throw new RuntimeException();
//}
return Arrays.asList(address1, address2);
}客户端搭建
yml文件:
server:
port: 8081pom文件
<dependency>
<groupId>com.atguigu.gmail</groupId>
<artifactId>gmail-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>主启动类
@SpringBootApplication
//@EnableHystrix
@DubboComponentScan(basePackages = "com.atguigu.gmail")
public class BootOrderServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
}
}核心configuration
@Configuration
public class MyDubboConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("order-service-consumer");
return applicationConfig;
}
/**
* 客户端config配置
*
* @return consumerConfig
*/
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setTimeout(30000);
consumerConfig.setCheck(false);
consumerConfig.setRetries(3);
consumerConfig.setVersion("*");
return consumerConfig;
}
/**
* 注册中心配置
*
* @return registryConfig
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setPort(2181);
registryConfig.setAddress("127.0.0.1");
registryConfig.setCheck(true);
registryConfig.setProtocol("zookeeper");
return registryConfig;
}
/**
* 消费者端配置
*
* @return referenceConfig
*/
@Bean
public ReferenceConfig<UserService> referenceConfig() {
ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setInterface(UserService.class);
referenceConfig.setVersion("*");
referenceConfig.setTimeout(10000);
//配置每一个method的属性
MethodConfig config = new MethodConfig();
config.setName("getUserAddressList");
config.setTimeout(20000);
config.setRetries(3);
//将method的设置关联到service配置中
List<MethodConfig> methodConfig = new ArrayList<>();
methodConfig.add(config);
referenceConfig.setMethods(methodConfig);
return referenceConfig;
}service方法
@Service
public class UserServiceImpl implements OrderService {
//@Reference注解的作用是在zookeeper中找服务提供者
//@Reference(check = false)启动时是否检查该服务在zookeeper中存在,默认是true。
//@Reference(timeout = 3000)
//@Reference(stub = "com.atguigu.gmail.service.impl.UserServiceStub")本地存根
//@Reference(url = "127.0.0.1:20881") //dubbo直连
@Reference(loadbalance = "roundrobin")//负载均衡机制
private UserService userService;
//@Reference(timeout = 2000)
@Override
//此处用到了服务容错,如果不适用则直接注释掉即可
@HystrixCommand(fallbackMethod = "exceptionMethod")
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id" + userId);
return userService.getUserAddressList(userId);
}
/**
* 容错方法
*
* @return
*/
public List<UserAddress> exceptionMethod() {
List<UserAddress> arrayList = new ArrayList<>();
UserAddress userAddress = new UserAddress();
userAddress.setId(10);
userAddress.setPhoneNum("123456");
userAddress.setUserAddress("上海市");
arrayList.add(userAddress);
userAddress.setConsignee("测试");
userAddress.setIsDefault("Y");
return arrayList;
}
}
地址栏请求服务

版权声明:本文为www1056481167原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。