内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

JavaSpringCloud客戶服務(wù)創(chuàng)建方法是什么

這篇文章主要講解了“Java Spring Cloud客戶服務(wù)創(chuàng)建方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java Spring Cloud客戶服務(wù)創(chuàng)建方法是什么”吧!

成都創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)按需定制網(wǎng)站,是成都網(wǎng)站設(shè)計(jì)公司,為成都集裝箱提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開發(fā)等。成都網(wǎng)站制作熱線:13518219792

新建一個(gè)基本的 Spring Boot 工程,命名為 cloud-customer

配置文件如下,僅是改了服務(wù)名和端口號(hào):

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

創(chuàng)建一個(gè) Customer 的實(shí)體類

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

數(shù)據(jù)訪問層直接繼承 ReactiveCrudRepository 我們便有了基本的 CRUD 能力

public interface CustomerMongoReactiveRepository extends ReactiveCrudRepository<Customer, String> {
}

因?yàn)槲覀冎皇鞘纠蛔鰪?fù)雜的業(yè)務(wù)邏輯,所以省略了 Service 層,在 Controller 里邊直接將 CRUD 的操作代理給了 Repository。

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

到這里,我們的服務(wù)注冊(cè)中心和兩個(gè)微服務(wù)就都好了。但是,這兩個(gè)微服務(wù)之間還是完全獨(dú)立的,沒有相互間的服務(wù)調(diào)用。現(xiàn)在我們來實(shí)現(xiàn)之前說的需求:客戶服務(wù)與帳戶服務(wù)可以相互通信,以獲取客戶的所有帳戶,并通過客戶服務(wù) API 方法返回。

首先創(chuàng)建一個(gè) Java Config,這里我們不再使用 RestTemplate 來調(diào)用服務(wù),而是 WebClient。這個(gè)配置看起來和注冊(cè) RestTemplate 時(shí)差不多,但是要注意這里注冊(cè)的 Bean 是 WebClient.Builder。

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

除了這種寫法,還有一種寫法是

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

下邊的是錯(cuò)誤的寫法,會(huì)拋出異常

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

然后在 CustomerController 實(shí)現(xiàn)這個(gè)端點(diǎn):

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

這里需要在 cloud-customer 里創(chuàng)建一個(gè) DTO Account,因?yàn)楹?nbsp;cloud-account 里的完全一樣,就省略了。

感謝各位的閱讀,以上就是“Java Spring Cloud客戶服務(wù)創(chuàng)建方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java Spring Cloud客戶服務(wù)創(chuàng)建方法是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

文章名稱:JavaSpringCloud客戶服務(wù)創(chuàng)建方法是什么
網(wǎng)頁網(wǎng)址:http://www.rwnh.cn/article0/jsdjio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)網(wǎng)站收錄、微信小程序、搜索引擎優(yōu)化、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
济南市| 阿拉尔市| 灌阳县| 岗巴县| 西和县| 澎湖县| 微山县| 新绛县| 抚州市| 阿瓦提县| 仁寿县| 长汀县| 电白县| 紫云| 兴业县| 定州市| 哈尔滨市| 新乐市| 雅安市| 子长县| 武乡县| 谷城县| 阜新| 资源县| 台南县| 静安区| 五峰| 宁陕县| 游戏| 新泰市| 长白| 昆山市| 正宁县| 台东县| 普宁市| 呼图壁县| 韩城市| 东辽县| 四子王旗| 文昌市| 乌鲁木齐县|