feign starter 使用实例
This commit is contained in:
33
examples/feign-example/pom.xml
Normal file
33
examples/feign-example/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>feign-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>feign-example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-feign-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
public class FeignExampleApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FeignExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.config.ClientConfiguration;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.hystrix.JSONPlaceHolderFallback;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
|
||||
|
||||
@FeignClient(value = "jplaceholder", url = "https://jsonplaceholder.typicode.com/", configuration = ClientConfiguration.class, fallback = JSONPlaceHolderFallback.class)
|
||||
public interface JSONPlaceHolderClient {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/posts")
|
||||
List<Post> getPosts();
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
|
||||
Post getPostById(@PathVariable("postId") Long postId);
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
@Configuration
|
||||
public class ClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder errorDecoder() {
|
||||
return new CustomErrorDecoder();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.config;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.exception.BadRequestException;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.exception.NotFoundException;
|
||||
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class CustomErrorDecoder implements ErrorDecoder {
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
switch (response.status()) {
|
||||
case 400:
|
||||
return new BadRequestException();
|
||||
case 404:
|
||||
return new NotFoundException();
|
||||
default:
|
||||
return new Exception("Generic error");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.exception;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class BadRequestException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public BadRequestException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BadRequestException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BadRequestException: " + getMessage();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.exception;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NotFoundException: " + getMessage();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.hystrix;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.client.JSONPlaceHolderClient;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
|
||||
|
||||
@Component
|
||||
public class JSONPlaceHolderFallback implements JSONPlaceHolderClient {
|
||||
|
||||
@Override
|
||||
public List<Post> getPosts() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long postId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Post {
|
||||
|
||||
private String userId;
|
||||
private Long id;
|
||||
private String title;
|
||||
private String body;
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
|
||||
|
||||
public interface JSONPlaceHolderService {
|
||||
|
||||
List<Post> getPosts();
|
||||
|
||||
Post getPostById(Long id);
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.client.JSONPlaceHolderClient;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.service.JSONPlaceHolderService;
|
||||
|
||||
@Service
|
||||
public class JSONPlaceHolderServiceImpl implements JSONPlaceHolderService {
|
||||
|
||||
private @Autowired JSONPlaceHolderClient jsonPlaceHolderClient;
|
||||
|
||||
@Override
|
||||
public List<Post> getPosts() {
|
||||
return jsonPlaceHolderClient.getPosts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long id) {
|
||||
return jsonPlaceHolderClient.getPostById(id);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
server:
|
||||
port: 8762
|
||||
max-http-header-size: 1000000
|
||||
|
||||
app:
|
||||
id: mall-ebtp-cloud-demo
|
10
examples/feign-example/src/main/resources/application.yml
Normal file
10
examples/feign-example/src/main/resources/application.yml
Normal file
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: feign
|
||||
application:
|
||||
name: mall-ebtp-cloud-demo
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
@ -0,0 +1,39 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.feign.example;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
|
||||
import com.chinaunicom.mall.ebtp.cloud.feign.example.service.JSONPlaceHolderService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class FeignExampleTest {
|
||||
private @Autowired JSONPlaceHolderService jsonPlaceHolderService;
|
||||
|
||||
@Test
|
||||
public void whenGetPosts_thenListPostSizeGreaterThanZero() {
|
||||
List<Post> posts = jsonPlaceHolderService.getPosts();
|
||||
|
||||
System.out.println(posts.size());
|
||||
|
||||
assertFalse(posts.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetPostWithId_thenPostExist() {
|
||||
Post post = jsonPlaceHolderService.getPostById(1L);
|
||||
|
||||
System.out.println(post);
|
||||
|
||||
assertNotNull(post);
|
||||
}
|
||||
}
|
41
examples/mybatis-plus-example/pom.xml
Normal file
41
examples/mybatis-plus-example/pom.xml
Normal file
@ -0,0 +1,41 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>mybatis-plus-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>mybatis-plus-example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-jpa-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>p6spy</groupId>
|
||||
<artifactId>p6spy</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.mybatis.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.jpa.starter.JpaStarterConfiguration;
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(JpaStarterConfiguration.class)
|
||||
public class MybatisPlusExampleApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MybatisPlusExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.mybatis.example.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
||||
|
||||
@Mapper
|
||||
public interface DemoMapper extends BaseMapper<Demo> {
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.mybatis.example.model;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class Demo {
|
||||
|
||||
private String id;
|
||||
private String col1;
|
||||
private String col2;
|
||||
private String createBy;
|
||||
private Date createTime;
|
||||
private String updateBy;
|
||||
private Date updateTime;
|
||||
private int delFlag;
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
logging:
|
||||
level:
|
||||
com.chinaunicom.mall.ebtp.cloud.mybatis.example: debug
|
||||
|
||||
# database
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
url: jdbc:mysql://125.32.114.204:13306/ebtp-cloud?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
|
||||
username: mall3-ebtp-dev
|
||||
password: mall3-ebtp-dev
|
||||
filters: stat,wall,log4j
|
||||
maxActive: 20
|
||||
initialSize: 1
|
||||
maxWait: 60000
|
||||
minIdle: 1
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
validationQuery: select 'x'
|
||||
testWhileIdle: true
|
||||
testOnBorrow: false
|
||||
testOnReturn: false
|
||||
poolPreparedStatements: true
|
||||
maxOpenPreparedStatements: 20
|
||||
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=50000000
|
||||
|
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: db
|
||||
application:
|
||||
name: mall-ebtp-cloud-demo
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
@ -0,0 +1,21 @@
|
||||
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
|
||||
# 自定义日志打印
|
||||
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
|
||||
#日志输出到控制台
|
||||
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
|
||||
# 使用日志系统记录 sql
|
||||
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
|
||||
# 设置 p6spy driver 代理
|
||||
deregisterdrivers=true
|
||||
# 取消JDBC URL前缀
|
||||
useprefix=true
|
||||
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
|
||||
excludecategories=info,debug,result,commit,resultset
|
||||
# 日期格式
|
||||
dateformat=yyyy-MM-dd HH:mm:ss
|
||||
# 实际驱动可多个
|
||||
#driverlist=org.h2.Driver
|
||||
# 是否开启慢SQL记录
|
||||
outagedetection=true
|
||||
# 慢SQL记录标准 2 秒
|
||||
outagedetectioninterval=2
|
@ -0,0 +1,116 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.mybatis.example;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.mapper.DemoMapper;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MybatisPlusExampleTest {
|
||||
|
||||
private @Autowired DemoMapper mapper;
|
||||
|
||||
/**
|
||||
* 测试插入函数
|
||||
*/
|
||||
@Test
|
||||
public void testInsert() {
|
||||
Demo demo = new Demo();
|
||||
demo.setId("10");
|
||||
demo.setCol1("test");
|
||||
demo.setCol2("test01");
|
||||
|
||||
Assertions.assertThat(mapper.insert(demo)).isGreaterThan(0);
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * 测试删除函数
|
||||
// */
|
||||
// @Test
|
||||
// public void delete() {
|
||||
// // 根据属性删除记录
|
||||
// mapper.delete(new QueryWrapper<Demo>().lambda().eq(Demo::getCol1, "test"));
|
||||
//
|
||||
// Assertions.assertThat(mapper.selectCount(new QueryWrapper<Demo>().lambda().eq(Demo::getCol1, "test")))
|
||||
// .isEqualTo(0);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 测试修改函数
|
||||
// */
|
||||
// @Test
|
||||
// public void update() {
|
||||
// // 通过id修改对象信息
|
||||
// mapper.updateById(new Demo().setId("1").setCol1("abc"));
|
||||
//
|
||||
// Assertions.assertThat(mapper.selectById("1").getCol1()).isEqualTo("abc");
|
||||
//
|
||||
// // 使用条件修改和模板修改
|
||||
// mapper.update(new Demo().setCol1("12313"),
|
||||
// Wrappers.<Demo>lambdaUpdate().set(Demo::getCol2, "abc").eq(Demo::getCreateBy, "插入填充44"));
|
||||
//
|
||||
// Demo demo = mapper.selectOne(new QueryWrapper<Demo>().lambda().eq(Demo::getCreateBy, "插入填充44"));
|
||||
// Assertions.assertThat(demo.getCol1()).isEqualTo("12313");
|
||||
// Assertions.assertThat(demo.getCol2()).isEqualTo("abc");
|
||||
//
|
||||
// // 仅使用条件修改
|
||||
// mapper.update(null, Wrappers.<Demo>lambdaUpdate().set(Demo::getCol2, "col2").eq(Demo::getId, "2"));
|
||||
//
|
||||
// Assertions.assertThat(mapper.selectById("2").getCol2()).isEqualTo("col2");
|
||||
//
|
||||
// // 使用自定义查询器就行修改
|
||||
// mapper.update(new Demo().setCol2("test03"), new QueryWrapper<Demo>().lambda().eq(Demo::getId, "3"));
|
||||
//
|
||||
// Assertions.assertThat(mapper.selectById("3").getCol2()).isEqualTo("test03");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 测试查询方法
|
||||
// */
|
||||
// @Test
|
||||
// public void find() {
|
||||
//
|
||||
// // 读取指定的属性
|
||||
// mapper.selectList(Wrappers.<Demo>lambdaQuery().select(Demo::getId)).forEach(bean -> {
|
||||
// Assertions.assertThat(bean.getId()).isNotNull();
|
||||
// Assertions.assertThat(bean.getCol1()).isNull();
|
||||
// Assertions.assertThat(bean.getCol2()).isNull();
|
||||
// Assertions.assertThat(bean.getCreateBy()).isNull();
|
||||
// });
|
||||
//
|
||||
// // 另一种写法
|
||||
// mapper.selectList(new QueryWrapper<Demo>().select("id", "createBy")).forEach(bean -> {
|
||||
// Assertions.assertThat(bean.getId()).isNotNull();
|
||||
// Assertions.assertThat(bean.getCol1()).isNull();
|
||||
// Assertions.assertThat(bean.getCol2()).isNull();
|
||||
// Assertions.assertThat(bean.getCreateBy()).isNotNull();
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 测试排序
|
||||
// */
|
||||
// @Test
|
||||
// public void orderBy() {
|
||||
// List<Demo> list = mapper.selectList(Wrappers.<Demo>query().orderByDesc("id"));
|
||||
// Assertions.assertThat(list).isNotEmpty();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 使用map封装实体对象
|
||||
// */
|
||||
// @Test
|
||||
// public void selectMaps() {
|
||||
// List<Map<String, Object>> mapList = mapper.selectMaps(Wrappers.<Demo>query().select());
|
||||
// Assertions.assertThat(mapList).isNotEmpty();
|
||||
//
|
||||
// System.out.println(mapList.get(0));
|
||||
// }
|
||||
|
||||
}
|
33
examples/redis-example/pom.xml
Normal file
33
examples/redis-example/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>redis-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>redis-example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-redis-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,11 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RedisExampleApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RedisExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.example.service;
|
||||
|
||||
public interface RedisExampleService {
|
||||
|
||||
public void readAndWrite();
|
||||
public void transactionHandle();
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.example.service.impl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.example.service.RedisExampleService;
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.starter.handler.NumberHandler;
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.starter.handler.StringHandler;
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.starter.util.RedisUtil;
|
||||
|
||||
@Service
|
||||
public class RedisExampleServiceImpl implements RedisExampleService {
|
||||
/**
|
||||
* 演示工具类的基本使用方法
|
||||
*/
|
||||
@Override
|
||||
public void readAndWrite() {
|
||||
StringHandler sh = RedisUtil.getStringHandler();
|
||||
sh.set("example-key", "example-value");
|
||||
|
||||
System.out.println(sh.get("example-key"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示事务的使用方式
|
||||
*/
|
||||
@Override
|
||||
public void transactionHandle() {
|
||||
List execute = RedisUtil.getTransactionHandler(2).execute(handler -> {
|
||||
handler.watch("redis-string-handler-key", "redis-number-handler-key");// 监控这些key
|
||||
|
||||
// 开启事务
|
||||
handler.beginTransaction();
|
||||
|
||||
StringHandler stringHandler = handler.getStringHandler();
|
||||
stringHandler.set("redis-string-handler-key", "hello");
|
||||
stringHandler.append("redis-string-handler-key", "redis");
|
||||
stringHandler.append("redis-string-handler-key", "!");
|
||||
|
||||
// 获取对应事务数字助手
|
||||
NumberHandler numberHandler = handler.getNumberHandler();
|
||||
numberHandler.addLong("redis-number-handler-key", 100);
|
||||
numberHandler.incrementLong("redis-number-handler-key");
|
||||
numberHandler.incrementLong("redis-number-handler-key");
|
||||
numberHandler.incrementLong("redis-number-handler-key");
|
||||
|
||||
// 提交事务返回结果
|
||||
return handler.commit();
|
||||
});
|
||||
System.out.println(Arrays.toString(execute.toArray()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
server:
|
||||
port: 8762
|
||||
max-http-header-size: 1000000
|
||||
|
||||
spring:
|
||||
redis:
|
||||
database: 0
|
||||
host: 125.32.114.204
|
||||
password: redis@CC1234
|
||||
port: 16379
|
||||
timeout: 6000
|
||||
ssl: false
|
||||
lettuce:
|
||||
pool:
|
||||
max-wait: -1ms
|
||||
max-active: 8
|
||||
max-idle: 8
|
||||
min-idle: 0
|
10
examples/redis-example/src/main/resources/application.yml
Normal file
10
examples/redis-example/src/main/resources/application.yml
Normal file
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: redis
|
||||
application:
|
||||
name: mall-ebtp-cloud-demo
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.example;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.example.service.RedisExampleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RedisExampleTest {
|
||||
|
||||
private @Autowired RedisExampleService redisExampleService;
|
||||
|
||||
@Test
|
||||
public void testReadAndWrite() {
|
||||
redisExampleService.readAndWrite();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransaction() {
|
||||
redisExampleService.transactionHandle();
|
||||
}
|
||||
|
||||
}
|
25
examples/sharding-jdbc-example/pom.xml
Normal file
25
examples/sharding-jdbc-example/pom.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>sharding-jdbc-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sharding-jdbc-example</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
36
examples/swagger-example/pom.xml
Normal file
36
examples/swagger-example/pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>swagger-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>swagger-example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-mvc-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-swagger-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.swagger.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.swagger.starter.SwaggerStarterConfiguration;
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(SwaggerStarterConfiguration.class)
|
||||
public class SwaggerExampleApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SwaggerExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.swagger.example.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.swagger.example.model.User;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/swagger")
|
||||
@Api(value = "试Controller")
|
||||
public class SwaggerExampleController {
|
||||
|
||||
@ApiIgnore
|
||||
@GetMapping("/hello")
|
||||
public String hello() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@ApiOperation(value = "回声服务", notes = "服务直接将客户消息返回")
|
||||
@ApiImplicitParam(name = "message", value = "用户消息", required = true, dataType = "String", paramType = "path")
|
||||
@GetMapping("/echo/{message}")
|
||||
public String echo(@PathVariable("message") String message) {
|
||||
return message;
|
||||
}
|
||||
|
||||
@ApiOperation(value = "创建用户", notes = "创建新的用户信息, 服务器创建成功后返回结果")
|
||||
@ApiImplicitParam(name = "user", value = "用户基本信息", required = true, dataType = "User")
|
||||
@PostMapping("/createUser")
|
||||
public ResponseEntity<User> createUser(@RequestBody User user) {
|
||||
return ResponseEntity.ok(user);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "修改用户", notes = "修改用户信息, 服务器返回执行结果")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path"),
|
||||
@ApiImplicitParam(name = "user", value = "要修改的用户信息", required = true, dataType = "User") })
|
||||
@PutMapping("/updateUser/{id}")
|
||||
public ResponseEntity<Map<String, Object>> updateUser(@PathVariable("id") Long id, @RequestBody User user) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("result", "success");
|
||||
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.swagger.example.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class User {
|
||||
|
||||
private String username;
|
||||
private Integer age;
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
server:
|
||||
port: 8762
|
||||
max-http-header-size: 1000000
|
||||
|
||||
unifast:
|
||||
swagger:
|
||||
basePackage: com.chinaunicom.mall.ebtp.cloud.swagger.example
|
||||
|
||||
# swagger 的基本配置信息(已经在starter中配置好)
|
||||
#unifast.swagger.title=\u6d4b\u8bd5\u63a5\u53e3\u6587\u6863
|
||||
#unifast.swagger.contactName=\u5409\u6797\u9879\u76ee\u7ec4
|
||||
#unifast.swagger.contactUrl=http://chinaunicom.com
|
||||
#unifast.swagger.contactEmail=chinaunicom@chinaunicom.com
|
||||
#unifast.swagger.version=0.0.1
|
||||
#unifast.swagger.description=\u7840\u540e\u53f0\u529f\u80fd\u6846\u67b6
|
@ -0,0 +1,5 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: swagger
|
||||
application:
|
||||
name: mall-ebtp-cloud-demo
|
@ -0,0 +1,38 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.swagger.example;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
@ -49,6 +49,11 @@
|
||||
<artifactId>unifast-storage</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>p6spy</groupId>
|
||||
<artifactId>p6spy</artifactId>
|
||||
<version>3.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
|
Reference in New Issue
Block a user