diff --git a/book-service/pom.xml b/book-service/pom.xml index bbf3efd..23f9cbc 100644 --- a/book-service/pom.xml +++ b/book-service/pom.xml @@ -19,6 +19,10 @@ + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + org.springframework.boot spring-boot-starter-web diff --git a/book-service/src/main/resources/application.yml b/book-service/src/main/resources/application.yml index fbfd6fd..1aef1bf 100644 --- a/book-service/src/main/resources/application.yml +++ b/book-service/src/main/resources/application.yml @@ -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 diff --git a/borrow-service/pom.xml b/borrow-service/pom.xml index 35810a9..074c854 100644 --- a/borrow-service/pom.xml +++ b/borrow-service/pom.xml @@ -18,6 +18,10 @@ UTF-8 + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + org.springframework.boot spring-boot-starter-web diff --git a/borrow-service/src/main/java/com/test/config/BeanConfig.java b/borrow-service/src/main/java/com/test/config/BeanConfig.java new file mode 100644 index 0000000..1279fa2 --- /dev/null +++ b/borrow-service/src/main/java/com/test/config/BeanConfig.java @@ -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(); + } +} 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..92a3cc5 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,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 = mapper.getBorrowsByUid(uid); - //RestTemplate支持多种方式的远程调用 - RestTemplate template = new RestTemplate(); - //这里通过调用getForObject来请求其他服务,并将结果自动进行封装 +// //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 bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList()); + List bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/" + b.getBid(), Book.class)).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..88de828 100644 --- a/borrow-service/src/main/resources/application.yml +++ b/borrow-service/src/main/resources/application.yml @@ -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 diff --git a/eureka-service/pom.xml b/eureka-service/pom.xml index 4b83614..21f95d0 100644 --- a/eureka-service/pom.xml +++ b/eureka-service/pom.xml @@ -15,7 +15,7 @@ org.springframework.cloud - spring-cloud-starter-netflix-eureka-client + spring-cloud-starter-netflix-eureka-server diff --git a/eureka-service/src/main/java/com/test/EurekaApplication.java b/eureka-service/src/main/java/com/test/EurekaApplication.java new file mode 100644 index 0000000..f80bc0c --- /dev/null +++ b/eureka-service/src/main/java/com/test/EurekaApplication.java @@ -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); + } +} diff --git a/eureka-service/src/main/resources/application.yml b/eureka-service/src/main/resources/application.yml new file mode 100644 index 0000000..0a74a32 --- /dev/null +++ b/eureka-service/src/main/resources/application.yml @@ -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 diff --git a/user-service/pom.xml b/user-service/pom.xml index cbd6116..fd840a7 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -18,6 +18,10 @@ UTF-8 + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + org.springframework.boot spring-boot-starter-web diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml index 6ca8900..c20c701 100644 --- a/user-service/src/main/resources/application.yml +++ b/user-service/src/main/resources/application.yml @@ -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 +