在两个独立的Linux(centos-6.5)中安装eureka,并形成集群
在一个独立的Linux中安装MongoDB
在一个独立的Linux中安装FastDFS
依赖
<parent>spring-boot-starter-parentparent>
<dependencyManagement>spring-cloud-dependenciesdependencyManagement>
依赖
<parent>livegoodsparent>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-zuulartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
配置
# 服务端口
server:
port:8080
# 服务名称
spring:
application:
name:livegoods-zuul
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
# 配置网关
zuul:
routes:
banner:
path:/banner
service-id:livegoods-banner-client
recommendation:
path:/recommendation
service-id:livegoods-recommendation-client
hotproduct:
path:/hotProduct
service-id:livegoods-hotproduct-client
search:
path:/search
service-id:livegoods-search-client
detail:
path:/detail
service-id:livegoods-detail-client
comment:
path:/comment
service-id:livegoods-comment-client
login:
path:/login
service-id:livegoods-login-client
order:
path:/order
service-id:livegoos-order-client
启动类
@SpringBootApplication
@EnableZuulProxy
public class ZuulApp{
public static void main(String[] args){
SpringApplication.run(ZuulApp.class, args);
}
}
依赖
<parent>livegoodsparent>
数据传输实体类
public class LivegoodsDTO implements Serializable{
//状态码
private int status;
//消息
private String msg;
//轮播图地址集合
private List<String> results;
//返回数据
private Object data;
//是否有更多数据
private Boolean hasMore;
//时间戳
private Long time;
...
}
轮播图实体类
public class Banner implements Serializable{
//图片地址
private String url;
//创建时间
private Date createTime;
//优先级
private int priority;
...
}
热门推荐实体类
public class Recommendation implements Serializable{
//主键
private String id;
//标题
private Stirng title;
//图片地址
private String img;
//页面地址
private String link;
//城市
private String city;
}
热销产品实体类
public class Product implements Serializable{
//主键
private String id;
//标题
private String title;
//价格-单位分
private Long price;
//出租类型
private String rentType;
//房源类型
private String houseType;
//详细信息
private Information info;
//照片
private List<String> imgs;
//出租次数-判断热销
private Long rentCounts;
//详情页面链接
private String link;
//城市
private String city;
//获取详情链接
public String getLink(){
return "http://localhost:8080/details?id="+id;
}
//获取单张图片
public String getImg(){
return imgs.get(0);
}
...
}
房源详情实体类
public class Information implements Serializable{
//房源修建年份
private String years;
//房源类型
private String type;
//房源楼层
private String level;
//房源装修风格
private String style;
//房源朝向
private String orientation;
}
elasticsearch映射类
@Document(indexName="livegoods-product", type="product", shards=1, replicas=0)
public class ProductVO extends Product{
}
用户评论实体类
public class Comment implements Serializable{
//用户名
private String username;
//评论内容
private String comment;
//星级
private String star;
//评论时间
private Date createTime;
//关联房源
private String productId;
}
用户实体类
public class User implements Serializable{
private String username;
private String password;
private Date createTime;
private Date loginTime;
}
订单实体类
public class Order implements Serializable{
//主键
private String id;
//标题
private String title;
//房源类型
private String houseType;
//价格
private Long price;
//出租类型
private String rentType;
//评论状态 0-未评论 1-已评论
private byte commentStatus;
//图片地址
private String img;
//关联房源
private String productId;
}
依赖
<artifactId>commonsartifactId>
<artifactId>fastdfs-client-javaartifactId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
Banner接口
public interface BannerDao{
//查询所有banner
public List<Banner> findBanners();
}
Banner接口实现类
@Repository
public class BannerDaoImpl implements BannerDao{
@Autowired
private MongoTemplate mongoTemplate;
//查询所有banner
@Override
public List<Banner> findBanners(){
Query query=new Query();
//按照priority降序排列
query.with(new Sort(Sort.Direction.DESC,"priority"));
return mongoTemplate.find(query,Banner.class);
}
}
Recommendation接口
public interface RecommendationDao{
List<Recommendation> getRecommendationsByCity(String city);
}
Recommendation接口实现类
@Repository
public interface RecommendationDaoImpl implements RecommendationDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Recommendation> getRecommendationsByCity(String city){
Query query=new Query();
//分页
//PageRequest pageRequest=PageRequest.of(1,4);
//query.with(pageRequest);
//根据城市查询
query.addCriteria(Criteria.where("city").is(city));
return mongoTemplate.find(query,Recommendation.class);
}
}
ProductDao接口
public interface ProductDao{
List<Product> findHotProductsByCity(String city);
Product findProductById(String id);
}
ProductDao接口实现类
@Repository
public interface ProductDaoImpl implements ProductDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Product> findHotProductsByCity(String city){
Query query=new Query();
//根据城市查询
query.addCriteria(Criteria.where("city").is(city));
//排序并分页
query.with(PageRequest.of(0,4,Sort.Direction.DESC,"rentCounts"));
return mongoTemplate.find(query,Recommendation.class);
}
@Override
public Product findProductById(Stirng id){
return mongoTemplate.findById(id);
}
}
SearchDao接口
public interface SearchDao{
List<Product> SearchProduct(String city,String keyword,int page);
}
SearchDao接口实现类
@Repository
public interface SearchDaoImpl implements SearchDao{
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public List<Product> searchProduct(String city,String keyword,int page){
QueryBuilder queryBuilder;
if("*".equals(city) && "*".equals(keyword)){
queryBuilder=QueryBuilders.matchAllQuery();
}else{
queryBuilder=QueryBuilders.boolQuery();
BoolQueryBuilder boolQueryBuilder=(BoolQueryBuilder)queryBuilder;
List<QueryBuilder> must=boolQueryBuilder.must();
must.add(QueryBuilders.matchQuery("city",city));
List<QueryBuilder> should=boolQueryBuilder.should();
should.add(QueryBuilders.matchQuery("title",keyword));
should.add(QueryBuilders.matchQuery("rentType",keyword));
should.add(QueryBuilders.matchQuery("houseType",keyword));
}
//分页
PageRequest pageRequest=PageRequest.of(page,2);
//高亮
SearchQuery searchQuery=new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withPageable(pageRequest)
.withHighlightFields(
new HighlightBuilder.Field("title").preTags("").postTages(""),
new HighlightBuilder.Field("rentType").preTags("").postTages(""),
new HighlightBuilder.Field("houseType").preTags("").postTages("")
)
.withSort(SortBuilders.fieldSort("id.keyword").order(SortOrder.ASC))
.build();
Page<ProductVO> pageList=elasticsearchTemplage.queryForPage(searchQuery,ProductVO.class);
List<Product> list=new ArrayList();
for(ProductVO productVO:pageList){
list.add(productVO);
}
return list;
}
}
public interface CommentDao{
List<Comment> findCommentsByProductId(String id);
}
@Repository
public class CommentDaoImpl implements CommentDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Comment> findCommentsByProductId(String id){
Query query=new Query();
query.addCriteria(Criteria.where("productId").is(id));
query.with(new Sort(Sort.Direction.DESC,"createTime"));
return mongoTemplage.find(query,Comment.class);
}
}
public interface UserDao{
//第一次登录
void saveUser(User user);
//查询
User findUserByUsername(String username);
//更新
void updateUserPassword(String username, String password);
void updateUserUpdateTime(String username, Date date);
}
@Repository
public class UserDaoImpl implements UserDao{
@Autowird
private MongoTemplate mongoTemplate;
@Override
public void saveUser(User user){
mongoTemplate.save(user);
}
@Override
public User findUserByUsername(String username){
Query query=new Query();
query.addCriteria(Criteria.where("username").is(username));
List<User> list=mongoTemplate.query(query,User.class);
if(1 != list.size()){
return null;
}else{
return list.get(0);
}
}
@Override
public void updateUserPassword(String username, Sting password){
Query query=new Query();
query.addCriteria(Criteria.where("username").is(username));
Update update=Update.update("password",password);
mongoTemplate.updateFirst(query,update,User.class);
}
@Override
public void updateUserLoginTime(String username, Date date){
Query query=new Query();
query.addCriteria(Criteria.where("username").is(username));
Update update=Update.update("loginTime",date);
mongoTemplate.updateFirst(query,update,User.class);
}
}
public interface OrderDao{
List<Order> findOrdersByUsername(String username);
}
public class OrderDaoImpl implements OrderDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Order> findOrderByUsername(String username){
Query query=new Query();
query.addCriteria(Criteria.where("username").is(username));
return mongoTemplate.find(query,Order.class);
}
}
fast DFS配置文件fdfs.conf
# 连接超时
connect_timeout=10
# 网络超时
network_timeout=30
# 请求字符集
charset=utf-8
# tracker端口号
http.tracker_http_port=8080
# fastFDS地址
tracker_server=192.168.54.133:22122
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface BannerServiceAPI{
//获取轮播图
@GetMapping("/banner")
LivegoodsDTO getBanner();
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>banner-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class BannerServiceController implements BannerServiceAPI{
@Autowired
private BannerDaoService bannerDaoService;
@Override
public LivegoodsDTO getBanner(){
return bannerDaoService.getBanner();
}
}
接口
public interface BannerDaoService{
public LivegoodsDTO getBanner();
}
接口实现类
@Service
public class BannderDaoServiceImpl implements BannerDaoService{
@Autowired
private BannerDao banneDao;
@Override
public LivegoodsDTO getBanner(){
//查询
List<Banner> list = bannerDao.findBanners();
//遍历url
List<String> url=new ArrayList();
for(Banner banner:list){
url.add(banner.getUrl());
}
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setResults(url);
return dto;
}
}
配置
# 服务端口
server:
port:8081
# 服务名称
spring:
application:
name:livegoods-banner-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class BannerServiceApp{
public static void main(String[] args){
SpringApplication.run(BannerServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>banner-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class BannerClientController{
@Autowired
private BannerClientService bannerClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getBanner(){
return bannerClientService.getBanner();
}
}
接口
@FeignClient("livegoods-banner-service")
public interface BannerClientService extends BannerServiceAPI{
}
配置
# 服务端口
server:
port:8082
# 服务名称
spring:
application:
name:livegoods-banner-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.banner.client.service"})
public class BannerClientApp{
public static void main(String[] args){
SpringApplication.run(BannerClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface RecommendationServiceAPI{
//获取推荐
@GetMapping("/recommendation")
LivegoodsDTO getRecommendationsByCity(@RequestParam("city")String city);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>recommendation-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class RecommendationServiceController implements RecommendationServiceAPI{
@Autowired
private RecommendationDaoService recommendationDaoService;
@Override
public LivegoodsDTO getRecommendationsByCity(String city){
return recommendationDaoService.getRecommendationsByCity(city);
}
}
接口
public interface RecommendationDaoService{
public LivegoodsDTO getRecommendationsByCity(String city);
}
接口实现类
@Service
public class RecommendationDaoServiceImpl implements RecommendationDaoService{
@Autowired
private RecommendationDao RecommendationDao;
@Override
public LivegoodsDTO getRecommendationsByCity(String city){
//查询
List<Recommendation> list = RecommendationDao.findRecommendationsByCity(city);
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setData(list);
return dto;
}
}
配置
# 服务端口
server:
port:8083
# 服务名称
spring:
application:
name:livegoods-recommendation-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class RecommendationServiceApp{
public static void main(String[] args){
SpringApplication.run(RecommendationServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>recommendation-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class RecommendationClientController{
@Autowired
private RecommendationClientService recommendationClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getRecommendationsByCity(@RequestParam(value="city",defaultValue="北京")String city){
return recommendationClientService.getRecommendationsByCity(city);
}
}
接口
@FeignClient("livegoods-recommendation-service")
public interface RecommendationClientService extends RecommendationServiceAPI{
}
配置
# 服务端口
server:
port:8084
# 服务名称
spring:
application:
name:livegoods-recommendation-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.recommendation.client.service"})
public class RecommendationClientApp{
public static void main(String[] args){
SpringApplication.run(RecommendationClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface HotProductServiceAPI{
//获取推荐
@GetMapping("/hotProduct")
LivegoodsDTO getHotProductionsByCity(@RequestParam("city")String city);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>hotproduct-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class HotproductServiceController implements HotproductServiceAPI{
@Autowired
private HotProductDaoService hotProductDaoService;
@Override
public LivegoodsDTO getHotProductsByCity(String city){
return productDaoService.getHotProducsByCity(city);
}
}
接口
public interface HotProductDaoService{
public LivegoodsDTO getHotProductsByCity(String city);
}
接口实现类
@Service
public class HotProdcutDaoServiceImpl implements HotProductDaoService{
@Autowired
private ProductDao productDao;
@Override
public LivegoodsDTO getHotProductsByCity(String city){
//查询
List<Product> list = productDao.findHotProductsByCity(city);
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setData(list);
return dto;
}
}
配置
# 服务端口
server:
port:8085
# 服务名称
spring:
application:
name:livegoods-hotproduct-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class HotProductServiceApp{
public static void main(String[] args){
SpringApplication.run(HotProductServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>hotproduct-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class HotProductClientController{
@Autowired
private HotProductClientService hotProductClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getHotProductsByCity(@RequestParam(value="city",defaultValue="北京")String city){
return hotProductClientService.getHotProductsByCity(city);
}
}
接口
@FeignClient("livegoods-hotproduct-service")
public interface HotProductClientService extends HotProductServiceAPI{
}
配置
# 服务端口
server:
port:8086
# 服务名称
spring:
application:
name:livegoods-hotprodut-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.hotproduct.client.service"})
public class HotProductClientApp{
public static void main(String[] args){
SpringApplication.run(HotProductClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface SearchServiceAPI{
//获取推荐
@GetMapping("/search")
LivegoodsDTO searchProduct(@RequestParam("city")String city, @RequestParam("keyword")String keyword, int page);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>search-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class SearchServiceController implements SearchServiceAPI{
@Autowired
private SearchDaoService SearchDaoService;
@Override
public LivegoodsDTO searchProduct(String city,String keyword,int page){
return SearchDaoService.searchProduct(city,keyword,page);
}
}
接口
public interface SearchDaoService{
public LivegoodsDTO searchProduct(String city,String keyword,int page);
}
接口实现类
@Service
public class SearchDaoServiceImpl implements SearchDaoService{
@Autowired
private SearchDao searchDao;
@Override
public LivegoodsDTO searchProduct(String city,String keyword,int page){
//查询
List<Product> list = searchDao.searchProduct(city,keyword,page);
if(0 == list.size()){
list=searchDao.searchProduct("*","*",page);
}
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
if(0 == list.size){
dto.setHasMore(false);
}else{
dto.setHasMore(true);
}
dto.setStatus(200);
dto.setData(list);
return dto;
}
}
配置
# 服务端口
server:
port:8087
# 服务名称
spring:
application:
name:livegoods-search-service
data:
elasticsearch:
cluster-name:elasticsearch
cluster-nodes:192.168.54.135:9300
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class SearchServiceApp{
public static void main(String[] args){
SpringApplication.run(SearchServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>search-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class SearchClientController{
@Autowired
private SearchClientService searchClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO searchProduct(@RequestParam(value="city",defaultValue="北京")String city,String keyword,int page){
return searchClientService.searchProduct(city,keyword,page);
}
}
接口
@FeignClient("livegoods-search-service")
public interface SearchClientService extends SearchServiceAPI{
}
配置
# 服务端口
server:
port:8088
# 服务名称
spring:
application:
name:livegoods-search-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.search.client.service"})
public class SearchClientApp{
public static void main(String[] args){
SpringApplication.run(SearchClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface DetailServiceAPI{
//获取推荐
@GetMapping("/detail")
LivegoodsDTO searchProductById(@RequestParam("id")String id);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>detail-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class DetailServiceController implements DetailServiceAPI{
@Autowired
private DetailDaoService DetailDaoService;
@Override
public LivegoodsDTO getProductById(String id){
return DetailDaoService.getProductById(id);
}
}
接口
public interface DetailDaoService{
public LivegoodsDTO getProductById(String id);
}
接口实现类
@Service
public class DetailDaoServiceImpl implements DetailDaoService{
@Autowired
private ProductDao productDao;
@Override
public LivegoodsDTO getProductById(String id){
//查询
Product product= productDao.findProductById(id);
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setData(product);
return dto;
}
}
配置
# 服务端口
server:
port:8089
# 服务名称
spring:
application:
name:livegoods-detail-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class DetailServiceApp{
public static void main(String[] args){
SpringApplication.run(DetailServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>detail-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class DetailClientController{
@Autowired
private DetailClientService detailClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getProductById(String id){
return detailClientService.getProductById(id);
}
}
接口
@FeignClient("livegoods-detail-service")
public interface DetailClientService extends DetailServiceAPI{
}
配置
# 服务端口
server:
port:8090
# 服务名称
spring:
application:
name:livegoods-detail-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.detail.client.service"})
public class DetailClientApp{
public static void main(String[] args){
SpringApplication.run(DetailClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface CommentServiceAPI{
//获取推荐
@GetMapping("/comment")
LivegoodsDTO getCommentsByProductId(@RequestParam("id")String id);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>comment-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class CommentServiceController implements CommentServiceAPI{
@Autowired
private CommentDaoService CommentDaoService;
@Override
public LivegoodsDTO getCommentsByProductId(String id){
return commentDaoService.findCommentsByProductId(id);
}
}
接口
public interface CommentDaoService{
public LivegoodsDTO getCommentsByProductId(String id);
}
接口实现类
@Service
public class CommentDaoServiceImpl implements CommentDaoService{
@Autowired
private CommentDao commentDao;
@Override
public LivegoodsDTO getCommentsByProductId(String id){
//查询
List<Comment> list=commentDao.getCommentsByProductId(id);
//构建返回数据
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setData(list);
return dto;
}
}
配置
# 服务端口
server:
port:8091
# 服务名称
spring:
application:
name:livegoods-comment-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class CommentServiceApp{
public static void main(String[] args){
SpringApplication.run(CommentServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>comment-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class CommentClientController{
@Autowired
private CommentClientService commentClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getCommentsByProductId(String id){
return commentClientService.getCommentsByProductId(id);
}
}
接口
@FeignClient("livegoods-comment-service")
public interface CommentClientService extends CommentServiceAPI{
}
配置
# 服务端口
server:
port:8092
# 服务名称
spring:
application:
name:livegoods-comment-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.comment.client.service"})
public class CommentClientApp{
public static void main(String[] args){
SpringApplication.run(CommentClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface CommentServiceAPI{
@PostMapping("/login")
LivegoodsDTO login(@RequestParam("username")String username, @RequestParam("password")String password);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>comment-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class LoginServiceController implements LoginServiceAPI{
@Autowired
private LoginDaoService LoginDaoService;
@Override
public LivegoodsDTO login(String username,String password){
return loginDaoServiec.login(username,password);
}
}
接口
public interface LoginDaoService{
LivegoodsDTO getCode(String username);
LivegoodsDTO login(String username,String password);
}
接口实现类
@Service
public class LoginDaoServiceImpl implements LoginDaoService{
@Autowired
private UserDao userDao;
@Override
public LivegoodsDTO login(String username,String password){
User user=userDao.findUserByUsername(username);
if(null == user){
//不存在就创建
User u=new User();
u.setUsername(u);
u.setCreateTime(new Date());
userDao.saveUser(user);
}
//获取验证码进行验证
...
//更新登录
userDao.updateUserLoginTime(username,new Date());
//返回传输对象
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setMsg("登录成功");
return dto;
}
}
配置
# 服务端口
server:
port:8093
# 服务名称
spring:
application:
name:livegoods-login-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class LoginServiceApp{
public static void main(String[] args){
SpringApplication.run(LoginServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>login-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class LoginClientController{
@Autowired
private LoginClientService loginClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO login(String username,String password){
return loginClientService.login(username,password);
}
}
接口
@FeignClient("livegoods-login-service")
public interface LoginClientService extends LoginServiceAPI{
}
配置
# 服务端口
server:
port:8094
# 服务名称
spring:
application:
name:livegoods-login-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.login.client.service"})
public class LoginClientApp{
public static void main(String[] args){
SpringApplication.run(LoginClientApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>commonsartifactId>
<artifactId>spring-boot-starter-webartifactId>
接口
public interface OrderServiceAPI{
@GetMapping("/order")
LivegoodsDTO findOrdersByUsername(@RequestParam("username")String username);
}
依赖
<parent>livegoodsparent>
<artifactId>daoartifactId>
<artifactId>order-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class OrderServiceController implements OrderServiceAPI{
@Autowired
private OrderDaoService orderDaoService;
@Override
public LivegoodsDTO findOrdersByUsername(String username){
return orederDaoServiec.findOrdersByUsername(username);
}
}
接口
public interface OrderDaoService{
LivegoodsDTO findOrdersByUsername(String username);
}
接口实现类
@Service
public class OrderDaoServiceImpl implements OrderDaoService{
@Autowired
private OrderDao orderDao;
@Override
public LivegoodsDTO findOrdersByUsername(String username){
List<Order> list=orderDao.findOrdersByUsername(username);
//返回传输对象
LivegoodsDTO dto=new LivegoodsDTO();
dto.setStatus(200);
dto.setData(list);
return dto;
}
}
配置
# 服务端口
server:
port:8095
# 服务名称
spring:
application:
name:livegoods-order-service
data:
mongodb:
database:livegoods
host:192.168.54.134
port:27017
username:admin
password:123
authentication-database:admin
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
public class OrderServiceApp{
public static void main(String[] args){
SpringApplication.run(OrderServiceApp.class, args);
}
}
依赖
<parent>livegoodsparent>
<artifactId>order-service-apiartifactId>
<artifactId>spring-boot-starter-webartifactId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
控制类
@RestController
public class OrderClientController{
@Autowired
private OrderClientService orderClientService;
@CrossOrigin
@GetMapping
public LivegoodsDTO getOrdersByUsername(String username){
return orderClientService.getOrdersByUsername(username);
}
}
接口
@FeignClient("livegoods-order-service")
public interface OrderClientService extends OrderServiceAPI{
}
配置
# 服务端口
server:
port:8096
# 服务名称
spring:
application:
name:livegoods-order-client
# 注册中心地址
eureka:
client:
service-url:
defaultZone:http://192.168.54.130:8761/eureka/, http://192.168.54.131:8761/erureka/
启动类
@SpringBootApplication
@EnableFeignClients(basePackages={
"cn.livegoods.order.client.service"})
public class OrderClientApp{
public static void main(String[] args){
SpringApplication.run(OrderClientApp.class, args);
}
}
问题:
在做轮播图模块的时候,向前端返回是有数据的,但是数据结果不显示,浏览器console发现提示Access-Control-Allow-Origin
原因:
本项目前端工程是异步Ajax请求,前端是80端口,而后端返回的是8080端口,所以存在跨源访问
解决:
在banner-client模块中的BannerClientController的对应方法上使用@CrossOrigin