基础版本完成

This commit is contained in:
YoVinchen 2023-08-15 10:27:06 +08:00
parent 9cf9f6b824
commit fd494e7cb2
11 changed files with 97 additions and 7 deletions

View File

@ -19,6 +19,10 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@ -1,8 +1,15 @@
server:
port: 8201
spring:
application:
name: bookservice
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest
eureka:
client:
# 跟上面一样需要指向Eureka服务端地址这样才能进行注册
service-url:
defaultZone: http://localhost:8888/eureka

View File

@ -18,6 +18,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@ -0,0 +1,22 @@
package com.test.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* ClassName: BeanConfiguration
* Package: com.test.config
*
* @author yovinchen
* @Create 2023/8/15 10:08
*/
@Configuration
public class BeanConfig {
@Bean
@LoadBalanced
RestTemplate template(){
return new RestTemplate();
}
}

View File

@ -1,7 +1,6 @@
package com.test.service.impl;
import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
@ -28,16 +27,19 @@ public class BorrowServiceImpl implements BorrowService {
@Resource
BorrowMapper mapper;
@Resource
RestTemplate template;
@Override
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
//RestTemplate支持多种方式的远程调用
RestTemplate template = new RestTemplate();
// //RestTemplate支持多种方式的远程调用
// RestTemplate template = new RestTemplate();
// 这里通过调用getForObject来请求其他服务并将结果自动进行封装
//获取User信息
User user = template.getForObject("http://localhost:8101/user/" + uid, User.class);
User user = template.getForObject("http://userservice/user/" + uid, User.class);
//获取每一本书的详细信息
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
}

View File

@ -1,8 +1,15 @@
server:
port: 8301
spring:
application:
name: borrowservice
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest
eureka:
client:
# 跟上面一样需要指向Eureka服务端地址这样才能进行注册
service-url:
defaultZone: http://localhost:8888/eureka

View File

@ -15,7 +15,7 @@
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

View File

@ -0,0 +1,20 @@
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* ClassName: com.test.EurekaApplication
* Package: PACKAGE_NAME
*
* @author yovinchen
* @Create 2023/8/15 09:52
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

View File

@ -0,0 +1,12 @@
server:
port: 8888
eureka:
# 开启之前需要修改一下客户端设置(虽然是服务端
client:
# 由于我们是作为服务端角色所以不需要获取服务端改为false默认为true
fetch-registry: false
# 暂时不需要将自己也注册到Eureka
register-with-eureka: false
# 将eureka服务端指向自己
service-url:
defaultZone: http://localhost:8888/eureka

View File

@ -18,6 +18,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@ -1,8 +1,16 @@
server:
port: 8101
spring:
application:
name: userservice
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest
eureka:
client:
# 跟上面一样需要指向Eureka服务端地址这样才能进行注册
service-url:
defaultZone: http://localhost:8888/eureka