第二步:创建2个服务提供者,实现接口访问Redis
首先创建2个Module
此处由于Dubbo版本和Springboot版本冲突,我修改了Springboot的版本为1.5.9.RELEASE。版本参考下图
如果版本不对应,启动的时候可能报错,找不到配置文件:
java.lang.IllegalStateException: Cannot load configuration class: com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:403) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:249) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:127) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at com.wdw.server_provider1.ServerProvider1Application.main(ServerProvider1Application.java:12) [classes/:na]
1)pom.xml文件引入Redis,Dubbo,Zookeeper,以及自定义接口依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wdw</groupId>
<artifactId>server_provider1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server_provider1</name>
<description>First Provider</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.2.RELEASE</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<!--Redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--自定义接口-->
<dependency>
<groupId>com.wdw</groupId>
<artifactId>Interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
2)在apllication.yml文件中添加配置
server:
port: 8084
spring:
redis:
host: 127.0.0.1
port: 6379
timeout: 2000
database: 0
password:
dubbo:
application:
#此处做负载均衡测试,2个provider应用名保持一致
name: Server_Provider
registry:
#注册中心地址
address: zookeeper://127.0.0.1:2181
#暴露的端口号
protocol:
port: 20881```
3)测试一下Redis是否能正常存取
Redis的安装,启动就不说了,这里我首先手动存进去了一个数据
编写一个测试类,由于是一个小Demo,就没有自定义写Redis的配置类,直接使用RedisTemplate来操作字符串也够用了。 成功打印出来123, 234,测试成功
4)编写接口实现类
由于需要Dubbo实现Rpc,需要给此实现类添加@Service注解,此注解是用于Dubbo暴露服务的,所以不是用Springboot中的@Service引用,同时也给实现类加上@Component注解
package com.wdw.server_provider1.service;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import service.MyInterface;
/**
* create by WdW on 2019-11-27 16:43
*/
@Service
@Component
public class ServiceImpl implements MyInterface {
private StringBuilder sb ;
@Autowired
private RedisTemplate<String,String> redisTemplate;
private StringBuilder builder(){
return new StringBuilder("Service_Provider1:");
}
@Override
public String getPerson(String personKey) {
sb = builder();
return sb.append(redisTemplate.opsForValue().get(personKey)).toString();
}
@Override
public String setPerson(String personKey, String personValue) {
sb = builder();
//插入后,返回personKey值
redisTemplate.opsForValue().set(personKey,personValue);
if(getPerson(personKey)!=null){
return sb.append(personKey).toString();
}
return null;
}
}
5) 在Springboot启动类加入@EnableDubbo注解启用Dubbo的注解支持
运行工程:如果没有修改版本问题,则会出现下面报错:
修改回正确版本
还是利用Maven打包,后台运行。
Service_Provider2是一样的。
就不重复了,注意修改下发布的端口,和对外暴露的接口,顺便修改下实现类的返回,可以标识是哪个服务返回的即可。2个服务的应用名称应该保持一致,这里是一样的服务只是用来做负载均衡,但对于dubbo来说是一个服务。
使用Dubbo-admin查看:
正常的样子:
到此2个服务提供者就创建完成