From 826f9fb185aaca448f4a07debfab33ce1ca80d98 Mon Sep 17 00:00:00 2001 From: YoVinchen Date: Mon, 21 Aug 2023 20:36:38 +0800 Subject: [PATCH] =?UTF-8?q?Nacos=E9=80=9A=E8=BF=87OpenFeign=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- borrow-service/pom.xml | 4 +++ .../main/java/com/test/BorrowApplication.java | 2 ++ .../com/test/config/WebConfiguration.java | 30 ------------------- .../com/test/service/client/BookClient.java | 20 +++++++++++++ .../com/test/service/client/UserClient.java | 20 +++++++++++++ .../test/service/impl/BorrowServiceImpl.java | 13 ++++++-- .../src/main/resources/application.yml | 6 ++++ 7 files changed, 62 insertions(+), 33 deletions(-) delete mode 100644 borrow-service/src/main/java/com/test/config/WebConfiguration.java create mode 100644 borrow-service/src/main/java/com/test/service/client/BookClient.java create mode 100644 borrow-service/src/main/java/com/test/service/client/UserClient.java diff --git a/borrow-service/pom.xml b/borrow-service/pom.xml index 43143df..edbd89f 100644 --- a/borrow-service/pom.xml +++ b/borrow-service/pom.xml @@ -18,6 +18,10 @@ UTF-8 + + org.springframework.cloud + spring-cloud-starter-openfeign + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery diff --git a/borrow-service/src/main/java/com/test/BorrowApplication.java b/borrow-service/src/main/java/com/test/BorrowApplication.java index ee9da23..a2c5720 100644 --- a/borrow-service/src/main/java/com/test/BorrowApplication.java +++ b/borrow-service/src/main/java/com/test/BorrowApplication.java @@ -3,6 +3,7 @@ package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; /** @@ -13,6 +14,7 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E * @Create 2023/8/14 16:25 */ @EnableResourceServer +@EnableFeignClients @SpringBootApplication public class BorrowApplication { public static void main(String[] args) { diff --git a/borrow-service/src/main/java/com/test/config/WebConfiguration.java b/borrow-service/src/main/java/com/test/config/WebConfiguration.java deleted file mode 100644 index 55ce8e2..0000000 --- a/borrow-service/src/main/java/com/test/config/WebConfiguration.java +++ /dev/null @@ -1,30 +0,0 @@ -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.security.oauth2.client.OAuth2ClientContext; -import org.springframework.security.oauth2.client.OAuth2RestTemplate; -import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; - -import javax.annotation.Resource; - -/** - * ClassName: ma - * Package: com.test.config - * - * @author yovinchen - * @Create 2023/8/21 10:52 - */ -@Configuration -public class WebConfiguration { - - @Resource - OAuth2ClientContext context; - - @Bean - @LoadBalanced - public OAuth2RestTemplate restTemplate(){ - return new OAuth2RestTemplate(new ClientCredentialsResourceDetails(), context); - } -} 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..a92df91 --- /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/21 20:29 + */ +@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..c08a3b0 --- /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/21 20:29 + */ +@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 f96b65a..5b5cc1f 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 @@ -8,6 +8,8 @@ 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.security.oauth2.client.OAuth2RestTemplate; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -30,16 +32,21 @@ public class BorrowServiceImpl implements BorrowService { BorrowMapper mapper; @Resource - OAuth2RestTemplate template; + BookClient bookClient; + + @Resource + UserClient userClient; @Override public UserBorrowDetail getUserBorrowDetailByUid(int uid) { List borrow = mapper.getBorrowsByUid(uid); //这里通过调用getForObject来请求其他服务,并将结果自动进行封装 //获取User信息 - User user = template.getForObject("http://userservice/user/" + uid, User.class); + User user = userClient.getUserById(uid); //获取每一本书的详细信息 - List bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/" + b.getBid(), Book.class)).collect(Collectors.toList()); + 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 721e08e..ae38dc7 100644 --- a/borrow-service/src/main/resources/application.yml +++ b/borrow-service/src/main/resources/application.yml @@ -17,3 +17,9 @@ security: resource: #因为资源服务器得验证你的Token是否有访问此资源的权限以及用户信息,所以只需要一个验证地址 token-info-uri: http://localhost:8500/sso/oauth/check_token +feign: + oauth2: + #开启Oauth支持,这样就会在请求头中携带Token了 + enabled: true + #同时开启负载均衡支持 + load-balanced: true