Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of creating Java Spring Cloud customer service

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要讲解了"Java Spring Cloud客户服务创建方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java Spring Cloud客户服务创建方法是什么"吧!

新建一个基本的 Spring Boot 工程,命名为 cloud-customer。

配置文件如下,仅是改了服务名和端口号:

spring: application: name: cloud-customerserver: port: 8200eureka: client: service-url: defaultZone: http://localhost:8000/eureka/

创建一个 Customer 的实体类

@Data@AllArgsConstructor@NoArgsConstructor@Document(collection = "customers")public class Customer { @Id private String id; private String name; private String mobile;}

数据访问层直接继承 ReactiveCrudRepository 我们便有了基本的 CRUD 能力

public interface CustomerMongoReactiveRepository extends ReactiveCrudRepository {}

因为我们只是示例,不做复杂的业务逻辑,所以省略了 Service 层,在 Controller 里边直接将 CRUD 的操作代理给了 Repository。

@RestController@RequestMapping("/customer")public class CustomerController { @Autowired private CustomerMongoReactiveRepository repository; @Autowired private WebClient.Builder webClientBuilder; @GetMapping("") public Flux list() { return repository.findAll(); } @GetMapping("/{id}") public Mono get(@PathVariable String id) { return repository.findById(id); } @PostMapping("") public Mono create(@RequestBody Customer customer) { return repository.save(customer); } @PutMapping("/{id}") public Mono update(@PathVariable("id") String id, @RequestBody Customer customer) { customer.setId(id); return repository.save(customer); } @DeleteMapping("/{id}") public Mono delete(@PathVariable String id) { return repository.deleteById(id); }}

到这里,我们的服务注册中心和两个微服务就都好了。但是,这两个微服务之间还是完全独立的,没有相互间的服务调用。现在我们来实现之前说的需求:客户服务与帐户服务可以相互通信,以获取客户的所有帐户,并通过客户服务 API 方法返回。

首先创建一个 Java Config,这里我们不再使用 RestTemplate 来调用服务,而是 WebClient。这个配置看起来和注册 RestTemplate 时差不多,但是要注意这里注册的 Bean 是 WebClient.Builder。

@Configurationpublic class WebClientConfig { @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); }}

除了这种写法,还有一种写法是

public class MyClass { @Autowired private LoadBalancerExchangeFilterFunction lbFunction; public Mono doOtherStuff() { return WebClient.builder().baseUrl("http://cloud-account/account") .filter(lbFunction) .build() .get() .uri("") .retrieve() .bodyToMono(String.class); }}

下边的是错误的写法,会抛出异常

@Bean@LoadBalancedpublic WebClient loadBalancedWebClient() { return WebClient.builder().baseUrl("http://cloud-account/account").build();}

然后在 CustomerController 实现这个端点:

@GetMapping("/{id}/account")public Flux getAllAccounts(@PathVariable String id) { return webClientBuilder.baseUrl("http://cloud-account/account/").build() .get().uri("/customer/" + id) .retrieve() .bodyToFlux(Account.class);}

这里需要在 cloud-customer 里创建一个 DTO Account,因为和 cloud-account 里的完全一样,就省略了。

感谢各位的阅读,以上就是"Java Spring Cloud客户服务创建方法是什么"的内容了,经过本文的学习后,相信大家对Java Spring Cloud客户服务创建方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report