springCloud学习笔记2(服务发现) (2)

  首先在文件夹file:///D:/configFolder/organizationservice下创建两个配置文件:organizationservice.yml,organizationservice-dev.yml,内容分别为:

#organizationservice-dev.yml server: port: 10012 #organizationservice.yml spring: application: name: organizationservice

  主要 POM 配置如下:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>

  然后修改配置文件,bootstrap.yml

spring: application: #指定名称,以便spring cloud config客户端知道查找哪个配置 name: organizationservice profiles: #指定环境 active: dev cloud: config: enabled: true eureka: instance: prefer-ip-address: true client: register-with-eureka: true fetch-registry: true service-url: defaultZone: :8761/eureka/

  最后在启动类加入注解@EnableDiscoveryClient,启动。

3、使用服务发现来查找服务

  现在已经有两个注册服务了,现在来让许可证服务调用组织服务,获取组织信息。首先在 organizationservice 服务中的 controller 包中加入一个 controller 类,让它能够响应请求:

//OrganizationController.java @RestController public class OrganizationController { @GetMapping(value = "/organization/{orgId}") public Object getOrganizationInfo(@PathVariable("orgId") String orgId) { Map<String, String> data = new HashMap<>(2); data.put("id", orgId); data.put("name", orgId + "公司"); return data; } }

  接下来让许可证服务通过 Eureka 来找到组织服务的实际位置,然后调用该接口。为了达成目的,我们将要学习使用 3 个不同的 Spring/Netflix 客户端库,服务消费者可以使用它们来和 Ribbon 进行交互。从最低级别到最高级别,这些库包含了不同的与 Ribbon 进行交互的抽象封装层次:

Spring DiscoveryClient

启用了 RestTemplate 的 Spring DiscoveryClient

Neflix Feign 客户端

a、使用 Spring DiscoveryClient

  该工具提供了对 Ribbon 和 Ribbon 中缓存的注册服务最低层次的访问,可以查询通过 Eureka 注册的所有服务以及这些服务对应的 URL。

  首先在 licensingservice 的启动类中加入@EnableDiscoveryClient注解来启用 DiscoveryClient 和 Ribbon 库。

  然后在 service 包下创建 OrganizationService.java

@Service public class OrganizationService { private static final String SERVICE_NAME = "organizationservice"; private DiscoveryClient discoveryClient; @Autowired public OrganizationService(DiscoveryClient discoveryClient) { this.discoveryClient = discoveryClient; } /** * 使用Spring DiscoveryClient查询 * * @param id * @return */ public Organization getOrganization(String id) { RestTemplate restTemplate = new RestTemplate(); List<ServiceInstance> instances = discoveryClient.getInstances(SERVICE_NAME); if (instances.size() == 0) { throw new RuntimeException("无可用的服务"); } String serviceUri = String.format("%s/organization/%s", instances.get(0).getUri().toString(), id); ResponseEntity<Organization> responseEntity = restTemplate.exchange(serviceUri, HttpMethod.GET , null, Organization.class, id); return responseEntity.getBody(); } }

  接着在 controller 包中新建 LicensingController.java

@RestController public class LicensingController { private OrganizationService organizationService; @Autowired public LicensingController(OrganizationService organizationService) { this.organizationService = organizationService; } @GetMapping("/licensing/{orgId}") public Licensing getLicensing(@PathVariable("orgId") String orgId) { Licensing licensing = new Licensing(); licensing.setValid(false); licensing.setOrganization(organizationService.getOrganization(orgId)); return licensing; } }

  启动所有项目,访问localhost:10011/licensing/12,可以看到返回如下结果:

{ "organization": { "id": "12", "name": "12公司" }, "valid": false }

  在实际开发中,基本上是用不到这个的,除非是为了查询 Ribbon 以获取某个服务的所有实例信息,才会直接使用。如果直接使用它存在以下两个问题:

没有利用 Ribbon 的客户端负载均衡

和业务无关的代码写得太多

b、使用带 Ribbon 功能的 Spring RestTemplate 调用服务

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjwws.html