fix:添加seata的AT模式

上级 c641f54f
......@@ -65,7 +65,11 @@
<artifactId>fastjson</artifactId>
<version>2.0.21</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -14,11 +14,8 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String sex;
}
}
\ No newline at end of file
......@@ -54,5 +54,38 @@
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -134,7 +134,7 @@ public class ConsumerUserController {
*/
@GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result test() {
return Result.ok();
return Result.ok("test");
}
......@@ -143,6 +143,6 @@ public class ConsumerUserController {
*/
@GetMapping(value = "/index", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result index() {
return Result.ok();
return Result.ok("index");
}
}
\ No newline at end of file
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.entity.Person;
import com.kwan.springcloudalibaba.service.IPersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/seata")
public class SeataControlller {
@Autowired
private IPersonService personService;
@RequestMapping(value = "/save", method = RequestMethod.GET)
public Person save() {
//保存用户
Person person = new Person();
person.setAddress("深圳");
person.setUsername("秦梓淞111");
person.setGender("男");
return personService.savePerson(person);
}
}
package com.kwan.springcloudalibaba.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "person")
public class Person {
private Integer id;
private String username;
private String address;
private String gender;
}
\ No newline at end of file
package com.kwan.springcloudalibaba.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springcloudalibaba.entity.Person;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PersonMapper extends BaseMapper<Person> {
}
package com.kwan.springcloudalibaba.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springcloudalibaba.entity.Person;
public interface IPersonService extends IService<Person> {
Person savePerson(Person person);
}
package com.kwan.springcloudalibaba.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springcloudalibaba.api.FeignUserService;
import com.kwan.springcloudalibaba.entity.Person;
import com.kwan.springcloudalibaba.entity.User;
import com.kwan.springcloudalibaba.mapper.PersonMapper;
import com.kwan.springcloudalibaba.service.IPersonService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {
@Autowired
private PersonMapper personMapper;
@Autowired
private FeignUserService feignUserService;
@Override
@GlobalTransactional
public Person savePerson(Person person) {
personMapper.insert(person);
User user = new User();
user.setName(person.getUsername());
user.setSex(person.getGender());
feignUserService.save(user);
return person;
}
}
......@@ -17,4 +17,28 @@ spring:
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
refresh-enabled: true #默认自动刷新
seata:
application-id: nacos-server-consumer
tx-service-group: my-tx-group
service:
vgroup-mapping:
my-tx-group: seata-server
grouplist:
seata-server: 127.0.0.1:8091
enabled: true
ribbon:
#建立连接超时时间
ConnectTimeout: 5000
#建立连接之后,读取响应资源超时时间
ReadTimeout: 5000
feign:
client:
config:
#这里填具体的服务名称(也可以填default,表示对所有服务生效)
default:
#connectTimeout和readTimeout这两个得一起配置才会生效
connectTimeout: 5000
readTimeout: 5000
\ No newline at end of file
package com.kwan.springcloudalibaba.api;
import com.kwan.springcloudalibaba.common.Result;
import com.kwan.springcloudalibaba.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -20,4 +22,6 @@ public interface FeignUserService {
@RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
Result selectOne(@PathVariable(value = "id") Integer id);
@RequestMapping(method = RequestMethod.POST, value = "/save", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
Result save(@RequestBody User user);
}
......@@ -2,6 +2,18 @@
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.kwan.springcloudalibaba</groupId>
......
package com.kwan.springcloudalibaba.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 以配置文件的方式来配置gateway网关转发
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/17 22:59
*/
@Configuration
public class GatewayConfig {
/**
* 访问外网淘宝网
*
* @param builder
* @return
*/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("gateway_baidu", r -> r.path("/gateway/toTaobao")
.uri("http://www.taobao.com"))
.build();//id,访问配置path,以及真正访问地址
}
}
package com.kwan.springcloudalibaba.util;
import java.time.ZonedDateTime;
public class GetZonedDateTime {
public static void main(String[] args) {
ZonedDateTime zonedDateTime=ZonedDateTime.now();
//输出时区时间
System.out.println(zonedDateTime);
}
}
......@@ -24,6 +24,7 @@ spring:
profiles:
active: dev
cloud:
#gateway网关配置
gateway:
enabled: true #开启网关
discovery:
......@@ -34,18 +35,39 @@ spring:
uri: lb://nacos-server-consumer
predicates:
- Path=/nacos/consumer/test,/nacos/consumer/index
# - After=2023-02-17T23:26:22.403+08:00[Asia/Shanghai]
# - Before=2023-02-17T23:26:22.403+08:00[Asia/Shanghai]
# - Between=2023-02-17T23:26:22.403+08:00[Asia/Shanghai],2023-03-30T16:00:22.432+08:00[Asia/shanghai]
# - Between=2023-03-17T23:26:22.403+08:00[Asia/Shanghai],2023-03-30T16:00:22.432+08:00[Asia/shanghai]
# - Cookie=username,Lisi
# - Cookie=username,\d+
# - Header=id,001
# - Header=id,\d+
# - Host=**.somehost.org,**.anotherhost.org
# - Method=GET,POST
- Query=number,abc
# - Method=POST
- id: route2
uri: lb://nacos-server-producer
predicates:
- Path=/service/**
filter:
- AddRequestHeader=X-Request-Id,12345
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
#nacos配置
nacos:
discovery:
server-addr: http://120.79.36.53:8848 #服务注册地址
username: nacos
password: nacos
#sentinel配置
sentinel:
transport:
dashboard: 127.0.0.1:8181
......
......@@ -85,5 +85,9 @@
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class UserController {
/**
* 通过主键查询单条数据
*
* <p>
* http://localhost:9091/user/1
*
* @param id 主键
......@@ -50,4 +50,10 @@ public class UserController {
public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Result save(@RequestBody User user) {
return Result.ok(userService.saveUser(user));
}
}
\ No newline at end of file
......@@ -29,4 +29,12 @@ public interface IUserService extends IService<User> {
* @return
*/
User getUserByName(String sex);
/**
* 保存用户
*
* @param user
* @return
*/
User saveUser(User user);
}
......@@ -34,4 +34,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
}
return userByName;
}
@Override
public User saveUser(User user) {
userMapper.insert(user);
// final int i = 1 / 0;
return user;
}
}
\ No newline at end of file
#端口号
server:
port: 9091
#spring配置
spring:
application:
......@@ -17,4 +17,13 @@ spring:
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
refresh-enabled: true #默认自动刷新
seata:
application-id: nacos-server-producer
tx-service-group: my-tx-group
service:
vgroup-mapping:
my-tx-group: seata-server
grouplist:
seata-server: 127.0.0.1:8091
enabled: true
\ No newline at end of file
......@@ -85,5 +85,9 @@
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class UserController {
/**
* 通过主键查询单条数据
*
* <p>
* http://localhost:9091/user/1
*
* @param id 主键
......@@ -39,7 +39,6 @@ public class UserController {
return Result.ok(userService.getUserById(id));
}
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<User> addAdvertise() {
log.info("测试日志={}", "success");
......@@ -50,4 +49,9 @@ public class UserController {
public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Result save(@RequestBody User user) {
return Result.ok(userService.saveUser(user));
}
}
\ No newline at end of file
package com.kwan.springcloudalibaba.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@TableName(value = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String sex;
}
......@@ -29,4 +29,13 @@ public interface IUserService extends IService<User> {
* @return
*/
User getUserByName(String sex);
/**
* 保存用户
*
* @param user
* @return
*/
User saveUser(User user);
}
......@@ -34,4 +34,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
}
return userByName;
}
@Override
public User saveUser(User user) {
userMapper.insert(user);
// final int i = 1 / 0;
return user;
}
}
\ No newline at end of file
#端口号
server:
port: 9092
#spring配置
spring:
application:
......@@ -17,4 +17,13 @@ spring:
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
refresh-enabled: true #默认自动刷新
seata:
application-id: nacos-server-producer
tx-service-group: my-tx-group
service:
vgroup-mapping:
my-tx-group: seata-server
grouplist:
seata-server: 127.0.0.1:8091
enabled: true
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册