Nacos通过OpenFeign实现负载均衡远程调用

This commit is contained in:
YoVinchen 2023-08-21 20:36:38 +08:00
parent 60512849e8
commit 826f9fb185
7 changed files with 62 additions and 33 deletions

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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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> borrow = mapper.getBorrowsByUid(uid);
//这里通过调用getForObject来请求其他服务并将结果自动进行封装
//获取User信息
User user = template.getForObject("http://userservice/user/" + uid, User.class);
User user = userClient.getUserById(uid);
//获取每一本书的详细信息
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
List<Book> bookList = borrow.stream()
.map(b -> bookClient.getBookById(b.getBid()))
.collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
}

View File

@ -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