基础版本完成
This commit is contained in:
parent
9cf9f6b824
commit
fd494e7cb2
@ -19,6 +19,10 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
server:
|
server:
|
||||||
port: 8201
|
port: 8201
|
||||||
spring:
|
spring:
|
||||||
|
application:
|
||||||
|
name: bookservice
|
||||||
datasource:
|
datasource:
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
# 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册
|
||||||
|
service-url:
|
||||||
|
defaultZone: http://localhost:8888/eureka
|
||||||
|
@ -18,6 +18,10 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
22
borrow-service/src/main/java/com/test/config/BeanConfig.java
Normal file
22
borrow-service/src/main/java/com/test/config/BeanConfig.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package com.test.service.impl;
|
package com.test.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import com.test.entity.Book;
|
import com.test.entity.Book;
|
||||||
import com.test.entity.Borrow;
|
import com.test.entity.Borrow;
|
||||||
import com.test.entity.User;
|
import com.test.entity.User;
|
||||||
@ -28,16 +27,19 @@ public class BorrowServiceImpl implements BorrowService {
|
|||||||
@Resource
|
@Resource
|
||||||
BorrowMapper mapper;
|
BorrowMapper mapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RestTemplate template;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
||||||
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
||||||
//RestTemplate支持多种方式的远程调用
|
// //RestTemplate支持多种方式的远程调用
|
||||||
RestTemplate template = new RestTemplate();
|
// RestTemplate template = new RestTemplate();
|
||||||
// 这里通过调用getForObject来请求其他服务,并将结果自动进行封装
|
// 这里通过调用getForObject来请求其他服务,并将结果自动进行封装
|
||||||
//获取User信息
|
//获取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);
|
return new UserBorrowDetail(user, bookList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
server:
|
server:
|
||||||
port: 8301
|
port: 8301
|
||||||
spring:
|
spring:
|
||||||
|
application:
|
||||||
|
name: borrowservice
|
||||||
datasource:
|
datasource:
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
# 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册
|
||||||
|
service-url:
|
||||||
|
defaultZone: http://localhost:8888/eureka
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
20
eureka-service/src/main/java/com/test/EurekaApplication.java
Normal file
20
eureka-service/src/main/java/com/test/EurekaApplication.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
12
eureka-service/src/main/resources/application.yml
Normal file
12
eureka-service/src/main/resources/application.yml
Normal 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
|
@ -18,6 +18,10 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
server:
|
server:
|
||||||
port: 8101
|
port: 8101
|
||||||
spring:
|
spring:
|
||||||
|
application:
|
||||||
|
name: userservice
|
||||||
datasource:
|
datasource:
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
# 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册
|
||||||
|
service-url:
|
||||||
|
defaultZone: http://localhost:8888/eureka
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user