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

The method of remote calling data Dictionary of distributed Medical Registration system Nacos Micro-Service Feign

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "the method of remote calling data dictionary of distributed medical registration system Nacos micro-service Feign". In daily operation, it is believed that many people have doubts about the method of remote calling data dictionary of distributed medical registration system Nacos micro-service Feign. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "the method of remote calling data dictionary of distributed medical registration system Nacos microservice Feign"! Next, please follow the editor to study!

Requirements: the display function of making a hospital list. The list contains the hospital number, hospital grade, hospital address, status, and so on.

Analysis: first of all, it is determined to be a typical conditional query with paging. Because the level of the hospital needs to query the data dictionary part, this call is in different micro-service modules, which requires remote calls.

Step 1: register the microservice with the Nacos service center (1) introduce Nacos dependent com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery

(2) configure micro-service to Nacos 1. Configure service-hosp

# Service port server.port=8201# service name spring.application.name=service-hosp# Nacos service address spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

two。 Configure service-cmn

# Service port server.port=8202# service name spring.application.name=service-cmn# Nacos service address spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

3. Add each microservice module to the registry

Add @ EnableDiscoveryClient to the startup class of each microservice module to register the microservice with Nacos.

Step 2: make a remote call using Feign

Next, create a hospital list API (conditional query with paging) in service-hosp, and create a hospital level interface in service-cmn to query the name of the data dictionary according to the number. The remote call is then completed through Feign.

(1) service-hosp Hospital list API

Access path

/ admin/hosp/hospital/list/ {page} / {limit}

Controller:

@ RestController@RequestMapping ("/ admin/hosp/hospital") @ CrossOriginpublic class HospitalController {@ Autowired private HospitalService hospitalService / * Hospital list (conditional query with pagination) * @ param page * @ param limit * @ param hospitalQueryVo * @ return * / @ GetMapping ("list/ {page} / {limit}") public Result listHosp (@ PathVariable Integer page, @ PathVariable Integer limit, HospitalQueryVo hospitalQueryVo) {Page pageModel = hospitalService.selectHospPage (page) Limit,hospitalQueryVo) Return Result.ok (pageModel);}}

Service interface:

/ / list of hospitals (conditional query with paging) Page selectHospPage (Integer page, Integer limit, HospitalQueryVo hospitalQueryVo)

Service implementation class:

/ / list of hospitals (conditional query with paging) @ Override public Page selectHospPage (Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {/ / 1. Create the pageable object Pageable pageable = PageRequest.of (page-1, limit); / / 2. Create a conditional matcher ExampleMatcher matcher = ExampleMatcher.matching (). WithStringMatcher (ExampleMatcher.StringMatcher.CONTAINING). WithIgnoreCase (true); / / convert 3.hospitalQueryVo to a Hospital object Hospital hospital = new Hospital (); BeanUtils.copyProperties (hospitalQueryVo, hospital); / / 4. Create object Example example = Example.of (hospital, matcher); / / 5. Call the method to query Page pages = hospitalRepository.findAll (example, pageable); / / 6. Get a collection of all hospital information pages.getContent (). Stream (). ForEach (item-> {/ / this method performs a remote call to this.setHospitalHosType (item);}); return pages;}

Repository:

@ Repositorypublic interface HospitalRepository extends MongoRepository {/ * obtain records according to HosCode * @ param hoscode * @ return * / Hospital getHospitalByHoscode (String hoscode);} (2) service-cmn hospital grade / address interface

Because the hospital level and the provincial, municipal and regional address all obtain the value value of the data dictionary, the data dictionary name should be obtained according to the value value of the data dictionary when the hospital grade and address are displayed in the list. We need to write two interfaces here.

Query the hospital level. The access path is

/ admin/cmn/dict/getName/ {dictCode} / {value}

To query the address of the hospital, the access path is

/ admin/cmn/dict/getName/ {value}

Controller:

/ * * query data dictionary according to dictCode and value * @ param dictCode * @ param value * @ return * / @ GetMapping ("getName/ {dictCode} / {value}") public String getName (@ PathVariable String dictCode, @ PathVariable String value) {String dictName = dictService.getDictName (dictCode, value); return dictName } / * query data dictionary according to value * @ param value * @ return * / @ GetMapping ("getName/ {value}") public String getName (@ PathVariable String value) {String dictName = dictService.getDictName ("", value); return dictName;}

Service interface:

/ * * query the data dictionary according to dictCode and value * @ param dictCode * @ param value * @ return * / String getDictName (String dictCode, String value)

Service implementation class:

/ * * query data dictionary according to dictCode and value * @ param dictCode * @ param value * @ return * / @ Override public String getDictName (String dictCode, String value) {/ / if dictCode is empty, query directly according to value; otherwise query if (StringUtils.isEmpty (dictCode)) {QueryWrapper wrapper = new QueryWrapper () according to dictCode and value Wrapper.eq ("value", value); Dict dict = baseMapper.selectOne (wrapper); return dict.getName ();} else {/ / query the dict object according to dictcode to get the id value of dict Dict codeDict = this.getDictByDictCode (dictCode); Long parent_id = codeDict.getId () / / query based on parent_id and value Dict finalDict = baseMapper.selectOne (new QueryWrapper () .eq ("parent_id", parent_id) .eq ("value", value)); return finalDict.getName ();}}

The data access layer is completed by Mybatis-plus.

(3) introducing Feign dependency org.springframework.cloud spring-cloud-starter-openfeign provided

Create a service-cmn-clientMave project separately for remote call.

(4) the caller scans the Feign through the packet

On the startup class on the calling side, add the @ EnableFeignClients (basePackages = "com.gql") annotation.

(5) remote call

The method in the interface is the method signature of the module to be called. Note that the access path is the full path, and the parameter name should be added after the parameter is annotated.

1. Remote call interface

/ / the name of the microservice to be called @ FeignClient ("service-cmn") @ Repositorypublic interface DictFeignClient {/ * query the data dictionary according to dictCode and value * @ param dictCode * @ param value * @ return * / @ GetMapping ("/ admin/cmn/dict/getName/ {dictCode} / {value}") public String getName (@ PathVariable ("dictCode") String dictCode @ PathVariable ("value") String value) / * * query data dictionary according to value * @ param value * @ return * / @ GetMapping ("/ admin/cmn/dict/getName/ {value}") public String getName (@ PathVariable ("value") String value);}

two。 Perform a remote call

@ Autowired private DictFeignClient dictFeignClient; / / gets the query list collection, traverses the hospital level encapsulation private Hospital setHospitalHosType (Hospital hospital) {/ / encapsulates the hospital level String hostypeString = dictFeignClient.getName ("Hostype", hospital.getHostype ()); hospital.getParam (). Put ("hostypeString", hostypeString); / / encapsulates hospital provinces and cities String provinceString = dictFeignClient.getName (hospital.getProvinceCode ()) String cityString = dictFeignClient.getName (hospital.getCityCode ()); String districtString = dictFeignClient.getName (hospital.getDistrictCode ()); hospital.getParam () .put ("fullAddress", provinceString + cityString + districtString); return hospital;} step 3: test with swagger

After adding the request parameters to the swagger page, click the execute button.

Successfully obtain the level and address of the hospital through remote call:

At this point, the study of "distributed medical registration system Nacos micro-service Feign remote call data dictionary method" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report