diff --git a/.run/BookApplication-01.run.xml b/.run/BookApplication-01.run.xml
new file mode 100644
index 0000000..2972282
--- /dev/null
+++ b/.run/BookApplication-01.run.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/BookApplication-02.run.xml b/.run/BookApplication-02.run.xml
new file mode 100644
index 0000000..2caae61
--- /dev/null
+++ b/.run/BookApplication-02.run.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/BorrowApplication.run.xml b/.run/BorrowApplication.run.xml
new file mode 100644
index 0000000..6d783f9
--- /dev/null
+++ b/.run/BorrowApplication.run.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/ConfigApplication.run.xml b/.run/ConfigApplication.run.xml
new file mode 100644
index 0000000..db12018
--- /dev/null
+++ b/.run/ConfigApplication.run.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/EurekaApplication-01.run.xml b/.run/EurekaApplication-01.run.xml
new file mode 100644
index 0000000..091e8ce
--- /dev/null
+++ b/.run/EurekaApplication-01.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/EurekaApplication-02.run.xml b/.run/EurekaApplication-02.run.xml
new file mode 100644
index 0000000..c35ea99
--- /dev/null
+++ b/.run/EurekaApplication-02.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/GatewayApplication.run.xml b/.run/GatewayApplication.run.xml
new file mode 100644
index 0000000..261f554
--- /dev/null
+++ b/.run/GatewayApplication.run.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/HystrixDashBoardApplication.run.xml b/.run/HystrixDashBoardApplication.run.xml
new file mode 100644
index 0000000..39a5ed3
--- /dev/null
+++ b/.run/HystrixDashBoardApplication.run.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/UserApplication-01.run.xml b/.run/UserApplication-01.run.xml
new file mode 100644
index 0000000..5b07266
--- /dev/null
+++ b/.run/UserApplication-01.run.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/UserApplication-02.run.xml b/.run/UserApplication-02.run.xml
new file mode 100644
index 0000000..c0110aa
--- /dev/null
+++ b/.run/UserApplication-02.run.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/UserApplication.run.xml b/.run/UserApplication.run.xml
new file mode 100644
index 0000000..816b15d
--- /dev/null
+++ b/.run/UserApplication.run.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/book-service/pom.xml b/book-service/pom.xml
index bbf3efd..0ac2674 100644
--- a/book-service/pom.xml
+++ b/book-service/pom.xml
@@ -19,6 +19,10 @@
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
org.springframework.boot
spring-boot-starter-web
diff --git a/book-service/src/main/java/com/test/controller/BookController.java b/book-service/src/main/java/com/test/controller/BookController.java
index bd78238..b8f6973 100644
--- a/book-service/src/main/java/com/test/controller/BookController.java
+++ b/book-service/src/main/java/com/test/controller/BookController.java
@@ -23,6 +23,7 @@ public class BookController {
@RequestMapping("/book/{bid}")
Book findBookById(@PathVariable("bid") int bid) {
+ System.out.println("调用图书服务");
return service.getBookById(bid);
}
}
diff --git a/book-service/src/main/resources/application.yml b/book-service/src/main/resources/application.yml
index fbfd6fd..bf4f6e7 100644
--- a/book-service/src/main/resources/application.yml
+++ b/book-service/src/main/resources/application.yml
@@ -6,3 +6,11 @@ spring:
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest
+ # 应用名称 bookservice
+ application:
+ name: bookservice
+ cloud:
+ nacos:
+ discovery:
+ # 配置Nacos注册中心地址
+ server-addr: localhost:8848
diff --git a/borrow-service/pom.xml b/borrow-service/pom.xml
index 35810a9..e18cec7 100644
--- a/borrow-service/pom.xml
+++ b/borrow-service/pom.xml
@@ -18,6 +18,19 @@
UTF-8
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-loadbalancer
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
org.springframework.boot
spring-boot-starter-web
diff --git a/borrow-service/src/main/java/com/test/BorrowApplication.java b/borrow-service/src/main/java/com/test/BorrowApplication.java
index 0a93b4d..3ed43f3 100644
--- a/borrow-service/src/main/java/com/test/BorrowApplication.java
+++ b/borrow-service/src/main/java/com/test/BorrowApplication.java
@@ -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) {
diff --git a/borrow-service/src/main/java/com/test/service/client/BookClient.java b/borrow-service/src/main/java/com/test/service/client/BookClient.java
new file mode 100644
index 0000000..06e84bb
--- /dev/null
+++ b/borrow-service/src/main/java/com/test/service/client/BookClient.java
@@ -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);
+}
diff --git a/borrow-service/src/main/java/com/test/service/client/UserClient.java b/borrow-service/src/main/java/com/test/service/client/UserClient.java
new file mode 100644
index 0000000..c167aa2
--- /dev/null
+++ b/borrow-service/src/main/java/com/test/service/client/UserClient.java
@@ -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);
+}
diff --git a/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java b/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
index 0cf1be6..c540dfc 100644
--- a/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
+++ b/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
@@ -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 = mapper.getBorrowsByUid(uid);
- //RestTemplate支持多种方式的远程调用
- RestTemplate template = new RestTemplate();
- //这里通过调用getForObject来请求其他服务,并将结果自动进行封装
- //获取User信息
- User user = template.getForObject("http://localhost:8101/user/" + uid, User.class);
- //获取每一本书的详细信息
- List bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
+ User user = userClient.getUserById(uid);
+ List bookList = borrow
+ .stream()
+ .map(b -> bookClient.getBookById(b.getBid()))
+ .collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
}
diff --git a/borrow-service/src/main/resources/application.yml b/borrow-service/src/main/resources/application.yml
index 1dfc74e..01930f0 100644
--- a/borrow-service/src/main/resources/application.yml
+++ b/borrow-service/src/main/resources/application.yml
@@ -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
diff --git a/pom.xml b/pom.xml
index fed1bfb..d9c2d7e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.boot
spring-boot-starter-parent
- 2.7.14
+ 2.6.6
com.example
@@ -43,6 +43,7 @@
com.mysql
mysql-connector-j
+ 8.0.32
@@ -53,6 +54,24 @@
mybatis-spring-boot-starter
2.2.0
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ 2021.0.1
+ pom
+ import
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-alibaba-dependencies
+ 2021.0.1.0
+ pom
+ import
+
diff --git a/user-service/pom.xml b/user-service/pom.xml
index cbd6116..3f23789 100644
--- a/user-service/pom.xml
+++ b/user-service/pom.xml
@@ -18,6 +18,10 @@
UTF-8
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
org.springframework.boot
spring-boot-starter-web
diff --git a/user-service/src/main/java/com/test/controller/UserController.java b/user-service/src/main/java/com/test/controller/UserController.java
index 7fba3b8..33cbe45 100644
--- a/user-service/src/main/java/com/test/controller/UserController.java
+++ b/user-service/src/main/java/com/test/controller/UserController.java
@@ -24,6 +24,7 @@ public class UserController {
//这里以RESTFul风格为例
@RequestMapping("/user/{uid}")
public User findUserById(@PathVariable("uid") int uid) {
+ System.out.println("调用图书服务");
return service.getUserById(uid);
}
}
diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml
index 6ca8900..6072a4f 100644
--- a/user-service/src/main/resources/application.yml
+++ b/user-service/src/main/resources/application.yml
@@ -6,3 +6,11 @@ spring:
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest
+ # 应用名称 userservice
+ application:
+ name: userservice
+ cloud:
+ nacos:
+ discovery:
+ # 配置Nacos注册中心地址
+ server-addr: localhost:8848