diff --git a/book-service/src/main/java/com/test/BookApplication.java b/book-service/src/main/java/com/test/BookApplication.java index 7c25a8d..446bef3 100644 --- a/book-service/src/main/java/com/test/BookApplication.java +++ b/book-service/src/main/java/com/test/BookApplication.java @@ -3,6 +3,8 @@ package com.test; import com.apple.eawt.Application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * ClassName: BookApplication @@ -12,6 +14,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @Create 2023/8/14 16:23 */ @SpringBootApplication +@EnableEurekaClient public class BookApplication { public static void main(String[] args) { SpringApplication.run(BookApplication.class,args); 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 db4d32b..38fa384 100644 --- a/book-service/src/main/java/com/test/controller/BookController.java +++ b/book-service/src/main/java/com/test/controller/BookController.java @@ -1,12 +1,13 @@ package com.test.controller; import com.test.service.BookService; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; -import java.awt.print.Book; +import com.test.entity.Book; /** * ClassName: BookController @@ -16,6 +17,7 @@ import java.awt.print.Book; * @Create 2023/8/14 17:11 */ @RestController +@EnableEurekaClient public class BookController { @Resource diff --git a/book-service/src/main/java/com/test/mapper/BookMapper.java b/book-service/src/main/java/com/test/mapper/BookMapper.java index 977705a..6dea629 100644 --- a/book-service/src/main/java/com/test/mapper/BookMapper.java +++ b/book-service/src/main/java/com/test/mapper/BookMapper.java @@ -1,9 +1,9 @@ package com.test.mapper; +import com.test.entity.Book; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; -import java.awt.print.Book; /** * ClassName: BookMapper diff --git a/book-service/src/main/java/com/test/service/BookService.java b/book-service/src/main/java/com/test/service/BookService.java index 0fc33fb..3b18654 100644 --- a/book-service/src/main/java/com/test/service/BookService.java +++ b/book-service/src/main/java/com/test/service/BookService.java @@ -1,6 +1,5 @@ package com.test.service; - -import java.awt.print.Book; +import com.test.entity.Book; /** * ClassName: BookService diff --git a/book-service/src/main/java/com/test/service/impl/BookServiceImpl.java b/book-service/src/main/java/com/test/service/impl/BookServiceImpl.java index 9a98617..a595169 100644 --- a/book-service/src/main/java/com/test/service/impl/BookServiceImpl.java +++ b/book-service/src/main/java/com/test/service/impl/BookServiceImpl.java @@ -5,7 +5,7 @@ import com.test.service.BookService; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.awt.print.Book; +import com.test.entity.Book; /** * ClassName: BookServiceImpl diff --git a/book-service/src/main/resources/application.yml b/book-service/src/main/resources/application.yml index 1aef1bf..c54b3c3 100644 --- a/book-service/src/main/resources/application.yml +++ b/book-service/src/main/resources/application.yml @@ -10,6 +10,8 @@ spring: password: mactest eureka: client: - # 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册 - service-url: + serviceUrl: defaultZone: http://localhost:8888/eureka + instance: + prefer-ip-address: true + instance-id: ${spring.cloud.client.ip-address}:${server.port} diff --git a/borrow-service/src/main/java/com/test/BorrowApplication.java b/borrow-service/src/main/java/com/test/BorrowApplication.java index 0a93b4d..52829ce 100644 --- a/borrow-service/src/main/java/com/test/BorrowApplication.java +++ b/borrow-service/src/main/java/com/test/BorrowApplication.java @@ -2,6 +2,8 @@ package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * ClassName: BorrowApplication @@ -11,6 +13,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @Create 2023/8/14 16:25 */ @SpringBootApplication +@EnableEurekaClient public class BorrowApplication { public static void main(String[] args) { SpringApplication.run(BorrowApplication.class, args); diff --git a/borrow-service/src/main/java/com/test/controller/BorrowController.java b/borrow-service/src/main/java/com/test/controller/BorrowController.java index eddbfa3..52b81ea 100644 --- a/borrow-service/src/main/java/com/test/controller/BorrowController.java +++ b/borrow-service/src/main/java/com/test/controller/BorrowController.java @@ -4,6 +4,7 @@ import com.test.entity.UserBorrowDetail; import com.test.service.BorrowService; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; 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 92a3cc5..1bda6b9 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 @@ -33,12 +33,10 @@ public class BorrowServiceImpl implements BorrowService { @Override public UserBorrowDetail getUserBorrowDetailByUid(int uid) { List borrow = mapper.getBorrowsByUid(uid); -// //RestTemplate支持多种方式的远程调用 -// RestTemplate template = new RestTemplate(); -// 这里通过调用getForObject来请求其他服务,并将结果自动进行封装 - //获取User信息 + + //这里不用再写IP,直接写服务名称userservice User user = template.getForObject("http://userservice/user/" + uid, User.class); - //获取每一本书的详细信息 + //这里不用再写IP,直接写服务名称bookservice 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 88de828..75f6c28 100644 --- a/borrow-service/src/main/resources/application.yml +++ b/borrow-service/src/main/resources/application.yml @@ -10,6 +10,8 @@ spring: password: mactest eureka: client: - # 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册 - service-url: + serviceUrl: defaultZone: http://localhost:8888/eureka + instance: + prefer-ip-address: true + instance-id: ${spring.cloud.client.ip-address}:${server.port} diff --git a/eureka-service/src/main/resources/application.yml b/eureka-service/src/main/resources/application.yml index 0a74a32..2004eea 100644 --- a/eureka-service/src/main/resources/application.yml +++ b/eureka-service/src/main/resources/application.yml @@ -1,12 +1,10 @@ server: port: 8888 eureka: - # 开启之前需要修改一下客户端设置(虽然是服务端 client: - # 由于我们是作为服务端角色,所以不需要获取服务端,改为false,默认为true fetch-registry: false - # 暂时不需要将自己也注册到Eureka register-with-eureka: false - # 将eureka服务端指向自己 - service-url: + serviceUrl: defaultZone: http://localhost:8888/eureka + server: + enable-self-preservation: false diff --git a/user-service/src/main/java/com/test/UserApplication.java b/user-service/src/main/java/com/test/UserApplication.java index d5cae82..4e6cda6 100644 --- a/user-service/src/main/java/com/test/UserApplication.java +++ b/user-service/src/main/java/com/test/UserApplication.java @@ -2,6 +2,8 @@ package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * ClassName: UserApplication @@ -11,6 +13,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @Create 2023/8/14 16:26 */ @SpringBootApplication +@EnableEurekaClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); 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..b02a8db 100644 --- a/user-service/src/main/java/com/test/controller/UserController.java +++ b/user-service/src/main/java/com/test/controller/UserController.java @@ -4,6 +4,7 @@ import com.test.entity.User; import com.test.service.UserService; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml index c20c701..f67ae27 100644 --- a/user-service/src/main/resources/application.yml +++ b/user-service/src/main/resources/application.yml @@ -10,7 +10,8 @@ spring: password: mactest eureka: client: - # 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册 - service-url: + serviceUrl: defaultZone: http://localhost:8888/eureka - + instance: + prefer-ip-address: true + instance-id: ${spring.cloud.client.ip-address}:${server.port}