这里写目录标题
dubbo框架整合
pom文件管理
<!-- dubbo 集成 begin -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- 代理类升级 -->
<!--dubbo默认的代理字节码生成技术和mybatis-plus集成后,反射获取不到父类方法,需要升级javassist包-->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-GA</version>
</dependency>
<!-- dubbo序列化方式采用kryo-->
<!--dubbo默认使用的序列化采用的是Hession2, 复杂对象序列化存在问题,调整成kryo-->
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.45</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.2</version>
</dependency>
<!-- dubbo 集成 end-->
bootstrap.yml开启配置中心
在Nacos上添加配置
这里标记的单独的Group,于是在工程内需要单独设置分组名称
配置内容
spring:
main:
#是否允许重名服务
allow-bean-definition-overriding: true
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/restkeeper_operator?serverTimezone=Asia/Shanghai
username: root
password: root
修改common.yml
dubbo:
scan:
base-packages: com.restkeeper
protocols:
dubbo:
name: dubbo
port: -1
serialization: kryo
registry:
address: spring-cloud://127.0.0.1
consumer:
timeout: 600000
management:
endpoints:
web:
exposure:
include: "*"
logging:
level:
com.restkeeper: debug
dubbo序列化问题
工程中dubbo序列化采用了kryo,dubbo默认使用的序列化采用的是Hession2, 复杂对象序列化存在问题,调整成kryo
<!--dubbo默认使用的序列化采用的是Hession2, 复杂对象序列化存在问题,调整成kryo-->
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.45</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.2</version>
</dependency>
mybatisplus dubbo 代码整合
Service实现类
/**
* 运营端管理员 service实现类
*/
//@Service("operatorUserService")
@Service(version = "1.0.0",protocol = "dubbo")
/**
* dubbo中支持的协议
* dubbo 默认
* rmi
* hessian
* http
* webservice
* thrift
* memcached
* redis
*/
public class OperatorUserServiceImpl extends ServiceImpl<OperatorUserMapper, OperatorUser> implements IOperatorUserService {
}
启动工程
查看服务详情,服务注册成功~
Service服务测试
编写测试代码
@SpringBootTest
@RunWith(SpringRunner.class)
public class OperatorUserServiceTest {
/*@Autowired
@Qualifier("operatorUserService")
private IOperatorUserService operatorUserService;*/
@Reference(version = "1.0.0", check=false)
private IOperatorUserService operatorUserService;
@Test
@Transactional
@Rollback(false)
public void addUser(){
OperatorUser operatorUser = new OperatorUser();
operatorUser.setLoginname("demo");
operatorUser.setLoginpass("123456");
//密码密文存储
//operatorUser.setLoginpass(Md5Crypt.md5Crypt("123456".getBytes()));
operatorUserService.save(operatorUser);
}
}
消费端和dubbo整合
编写消费端的bootstrap.yml
spring:
application:
name: operator-web
cloud:
nacos:
discovery:
# server-addr: 120.26.40.20:8848
server-addr: 127.0.0.1:8848
config:
# server-addr: 120.26.40.20:8848
server-addr: 127.0.0.1:8848
file-extension: yml #指定文件扩展名,默认为properties
prefix: operator-web
#添加共享配置的dataId,如多个使用逗号分隔,并且越靠后,优先级越高
#文件后缀名不能少,只支持yaml,yml,properies
shared-dataids: common.yml
#哪些共享配置支持动态刷新,如多个使用逗号分隔
refreshable-dataids: common.yml
#group: RESTKEEPER_GROUP
profiles:
active: dev
server:
port: 8083
添加消费端在nacos上的配置文件
编写Controller
@Reference(version = "1.0.0", check=false)
private IOperatorUserService operatorUserService;
/**
* 运营端管理员分页查询
* @param pageNum
* @param pageSize
* @param name
* @return
*/
@GetMapping("/pageList/{page}/{pageSize}")
public IPage<OperatorUser> findListByPage(@PathVariable(name="page") int pageNum,
@PathVariable(name="pageSize") int pageSize){
//开启分页查询
IPage<OperatorUser> page = new Page<OperatorUser>(pageNum,pageSize);
log.info("管理员数据分页查询: "+ JSON.toJSONString(page));
return operatorUserService.page(page);
}
测试效果
gateway+Nacos及消費者整合实现负载均衡
pom.xml依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
bootstrap.yml
spring:
application:
name: gateway
profiles:
active: dev
cloud:
# 使用 Naoos 作为服务注册发现
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yml
prefix: gateway
server:
port: 8085
nacos 新建gateway-dev.yml
spring:
application:
cloud:
# 路由网关配置
gateway:
# 设置与服务注册发现组件结合,这样可以采用服务名的路由策略
discovery:
locator:
enabled: true
# 配置路由规则
routes:
# 采用自定义路由 ID(有固定用法,不同的 id 有不同的功能,详见:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
- id: OPERATOR-CONSUMER
# 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
uri: lb://operator-web
# Predicate 翻译过来是“谓词”的意思,必须,主要作用是匹配用户的请求,有很多种用法
predicates:
# Method 方法谓词
- Method=GET,POST,PUT,DELETE
# 配置日志级别,方别调试
logging:
level:
org.springframework.cloud.gateway: debug
测试代码
注:这里直接返回String可能会出异常,可以使用封装类型返回
@Value("${server.port}")
private String port;
@Reference(version = "1.0.0",check = false)
private IOperatorUserService operatorUserService;
@GetMapping(value = "/port")
public ResponseEntity<?> echo() {
LinkedHashMap resultMap = new LinkedHashMap();
((Map)resultMap).put("port",port);
CommonReturn commonReturn = new CommonReturn(resultMap);
ServiceReturn<?> objectServiceReturn = new ServiceReturn<>(commonReturn);
return ControllerReturn.returnNormal(objectServiceReturn);
}
启动两个服务
查看nacos
访问URL查看结果