SpringCloudAlibaba + Nacos整合完成
This commit is contained in:
@@ -18,6 +18,19 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- 这里需要单独导入LoadBalancer依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@@ -2,6 +2,7 @@ package com.test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* ClassName: BorrowApplication
|
||||
@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
* @author yovinchen
|
||||
* @Create 2023/8/14 16:25
|
||||
*/
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
public class BorrowApplication {
|
||||
public static void main(String[] args) {
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package com.test.service.client;
|
||||
|
||||
import com.test.entity.Book;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* ClassName: BookClient
|
||||
* Package: com.test.service.client
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/8/16 10:55
|
||||
*/
|
||||
@FeignClient("bookservice")
|
||||
public interface BookClient {
|
||||
|
||||
@RequestMapping("/book/{bid}")
|
||||
Book getBookById(@PathVariable("bid") int bid);
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.test.service.client;
|
||||
|
||||
import com.test.entity.User;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* ClassName: UserClient
|
||||
* Package: com.test.service.client
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/8/16 10:55
|
||||
*/
|
||||
@FeignClient("userservice")
|
||||
public interface UserClient {
|
||||
|
||||
@RequestMapping("/user/{uid}")
|
||||
User getUserById(@PathVariable("uid") int uid);
|
||||
}
|
@@ -1,15 +1,15 @@
|
||||
package com.test.service.impl;
|
||||
|
||||
|
||||
|
||||
import com.test.entity.Book;
|
||||
import com.test.entity.Borrow;
|
||||
import com.test.entity.User;
|
||||
import com.test.entity.UserBorrowDetail;
|
||||
import com.test.mapper.BorrowMapper;
|
||||
import com.test.service.BorrowService;
|
||||
import com.test.service.client.BookClient;
|
||||
import com.test.service.client.UserClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
@@ -28,16 +28,20 @@ public class BorrowServiceImpl implements BorrowService {
|
||||
@Resource
|
||||
BorrowMapper mapper;
|
||||
|
||||
@Resource
|
||||
UserClient userClient;
|
||||
|
||||
@Resource
|
||||
BookClient bookClient;
|
||||
|
||||
@Override
|
||||
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
||||
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
||||
//RestTemplate支持多种方式的远程调用
|
||||
RestTemplate template = new RestTemplate();
|
||||
//这里通过调用getForObject来请求其他服务,并将结果自动进行封装
|
||||
//获取User信息
|
||||
User user = template.getForObject("http://localhost:8101/user/" + uid, User.class);
|
||||
//获取每一本书的详细信息
|
||||
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
|
||||
User user = userClient.getUserById(uid);
|
||||
List<Book> bookList = borrow
|
||||
.stream()
|
||||
.map(b -> bookClient.getBookById(b.getBid()))
|
||||
.collect(Collectors.toList());
|
||||
return new UserBorrowDetail(user, bookList);
|
||||
}
|
||||
}
|
||||
|
@@ -6,3 +6,11 @@ spring:
|
||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||
username: mac
|
||||
password: mactest
|
||||
# 应用名称 borrowservice
|
||||
application:
|
||||
name: borrowservice
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 配置Nacos注册中心地址
|
||||
server-addr: localhost:8848
|
||||
|
Reference in New Issue
Block a user