热门推荐
commons
创建recommendation实体类
public class recommendation implements Serializable{
private String id;//主键
private String title;//标题
private String img;//图片
private String link;//链接
}
dao
serviceapi
1.创建recommendationserviceapi目录
2.引入依赖
spring-boot-starter-web
commons
3.创建RecommentdationServiceAPI接口
public interface Re{
@GetMapping
LivegoodsDTO getRecommendationByCity(@RequestParam("city")String city);
}
service
1.创建recommendationservice目录
2.引入依赖
spring-boot-starter-web
spring-cloud-starater-openfeign
dao
3.创建RecommentdationServiceController类
public class RecommentdationServiceController implements RecommentdationServiceAPI{
@Override
public LivegoodsDTO getRecommendationByCity(String city){
LivegoodsDTO dto=new LivegoodsDTO();
//创建临时数据
List<Recommendation> list=new ArrayList();
Recommendation re=new Recommendation("1","良辰美景","xx.png","http://www.baidu.com");
list.add(re);
dto.setData(list);
dto.setStatus(200);
return dto;
}
}
4.创建配置文件
server:port: 9003
spring:application:name: recommendation-service
eureka:client:service-url:defaultZone:
5.创建启动类
@SpringBootApplication
public class RecommendationServiceApp{
public static void main(String[] args){
SpringApplication.run(RecommendationServiceApp.class,args);
}
}
client
1.创建recommendationclient目录
2.引入依赖
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
spring-cloud-starater-openfeign
dao
3.创建RecommendationClientService接口
@FeginClient("Recommendation-service")
public class RecommentdationClientService extends RecommentdationServiceAPI{
}
4.创建RecommendationClientController类
@RestController
public class RecommentdationClientController{
@Autowried
private RecommentdationClientService service;
@GetMapping
@CrossOrigin
public LivegoodsDTO getRecommendationByCity(String city){
return this.service.getRecommendationByCity(city);
}
}
5.创建配置文件
server:
port: 9004
spring:
application:
name: recommendation-client
eureka:
client:
service-url:
defaultZone:
6.创建启动类
@SpringBootApplication
@EnableFeginClients(basePackages={"cn.khue.recommendation.client.service"})
public class RecommendationClientApp{
public static void main(String[] args){
SpringApplication.run(RecommendationClientApp.class,args);
}
}
zuul
1.添加配置
zuul:
routes:
recommendation:
path: /recommendation
service-id: recommendation-service
连接MongoBD
1.在Linux中启动mongdb
# 关闭防火墙
service iptables stop
# 创建/修改启动配置文件
vim /usr/local/mongodb/etc/mongdb.conf
# 文件内容修改为:
# dbpath=/usr/local/mongodb/data/db
# logpath=/usr/local/mongodb/log/mongodb.log
# port=27017
# auth=true
# bind_ip_all=true
# 后置启动
# 之前已经配置过mongodb的环境变量了
mongod --config /usr/local/mongodb/etc/mongodb.conf
# 进入mongodb命令行
mongo
# 进入admin库登录用户
use admin
db.auth("root","root");
# 查看库
show databases;
# 退出
quit();
2.SpringBoot测试
1.创建测试实体类
@Data
public class User implements Serializable{
private Integer id;
private String name;
private Integer age;
}
2.创建dao接口
public interface MongoDao{
//新增
User add(User user);
//查询
List<User> queryAll();
//删除
Long remove(Query query);
}
3.创建接口实现类
@Repository
public class MongoDaoImpl implements MongoDaoImpl{
@AutoWried
private MongoTemplate template;
@Overrde
public User add(User user){
return this.template.save(user);
}
@Override
public List<User> queryAll(){
return this.template.findAll(User.class);
}
@Override
public Long remove(Query query){
return this.template.remove(query,User.class).getDeleteCount();
}
}
4.创建启动类
@SpringBootApplication
public class MongoApp{
public static void main(String[] args){
SpringApplication.run(MongoApp.class,args);
}
}
5.创建配置文件
spring:
data:
mongodb:
host: 192.168.54.120
port: 27017
username: root
password: root
database: test
authentication-database: admin
# 可以简写
spring:
data:
mongodb:
uri: mongodb://root:root@192.168.54.130:27017
database: test
authentication-database: admin
6.创建测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes={DaoApp.class})
public class MongoTest{
@Autowired
private MongoDao dao;
@Test
public void addTest(){
User user=new User(1,"khue",25);
System.out.println(this.dao.add(user));
}
@Test
public void queryTest(){
List<User> users=this.dao.queryAll();
for(User user:users){
System.out.println(user);
}
}
@Test
public void removeTest(){
Query query=Query.query(Criteria.where("id").is(1));
System.out.println(this.dao.remove(query));
}
}
整合FastDFS实现文件上传
1.在Linux中启动Tracker、Storage、Nginx
因为是单机版FastDFS,Tracker和Storage各在一台机器,Nginx和Storage在同一台机器
1.启动Tracker
# 关闭防火墙
service iptables stop
# 启动Tracker
/etc/init.d/fdfs_trackerd start
2.启动Storage
# 关闭防火墙
service iptables stop
# 启动Storage
/etc/init.d/fdfs_storaged start
# 查看上传的文件(如果存储在该位置)
ll /fastdfs/storage/store/data/00/00
3.启动Nginx
/usr/local/nginx/sbin/nginx
2.在SpringBoot中实现文件上传
1.相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.khue</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
2.创建启动类
package cn.khue.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApp {
public static void main(String[] args) {
SpringApplication.run(TestApp.class,args);
}
}
3.创建配置文件
connect_timeout=10
network_connect_timeout=30
charset=UTF-8
http.tracker_http_port=8080
tracker_server=192.168.54.110:22122
4.创建测试类
package cn.khue.test;
import cn.khue.test.TestApp;
import org.csource.common.MyException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.csource.fastdfs.*;
import java.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApp.class)
public class FdfsTest {
//上传文件
@Test
public void uploadTest() throws IOException, MyException {
ClientGlobal.init("fdfs.conf");
TrackerClient client=new TrackerClient();
TrackerServer server=client.getConnection();
StorageServer storageServer=client.getStoreStorage(server);
StorageClient storageClient=new StorageClient(server,storageServer);
String[] result = storageClient.upload_file("C:/Users/Khue/Desktop/test.jpg", "jpg", null);
System.out.println("group: "+result[0]);
System.out.println("path: "+result[0]);
}
}