资讯专栏INFORMATION COLUMN

SpringCloud(第 005 篇)电影微服务,注册到 EurekaServer 中,通过 Ht

nifhlheimr / 3089人阅读

摘要:第篇电影微服务,也注册到中,通过协议访问已注册到生态圈中的用户微服务一大致介绍在服务治理框架中,微服务与微服务之间通过协议进行通信用户微服务作为消费方电影微服务作为提供方都注册到中在电影微服务层通过的硬编码配置方式实现服务之间的通信二实现

SpringCloud(第 005 篇)电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务

-

一、大致介绍

</>复制代码

  1. 1、在 eureka 服务治理框架中,微服务与微服务之间通过 Http 协议进行通信;
  2. 2、用户微服务作为消费方、电影微服务作为提供方都注册到 EurekaServer 中;
  3. 3、在电影微服务Web层通过 application.yml 的硬编码配置方式实现服务之间的通信;
二、实现步骤 2.1 添加 maven 引用包

</>复制代码

  1. 4.0.0
  2. springms-consumer-movie
  3. 1.0-SNAPSHOT
  4. jar
  5. com.springms.cloud
  6. springms-spring-cloud
  7. 1.0-SNAPSHOT
  8. org.springframework.boot
  9. spring-boot-starter-web
  10. org.springframework.boot
  11. spring-boot-starter-actuator
  12. org.springframework.cloud
  13. spring-cloud-starter-eureka
2.2 添加应用配置文件(springms-consumer-moviesrcmainresourcesapplication.yml)

</>复制代码

  1. spring:
  2. application:
  3. name: springms-consumer-movie
  4. server:
  5. port: 7901
  6. user:
  7. userServicePath: http://localhost:7900/simple/
  8. eureka:
  9. client:
  10. # healthcheck:
  11. # enabled: true
  12. serviceUrl:
  13. defaultZone: http://admin:admin@localhost:8761/eureka
  14. instance:
  15. prefer-ip-address: true
  16. instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
2.3 添加实体用户类User(springms-consumer-moviesrcmainjavacomspringmscloudentityUser.java)

</>复制代码

  1. package com.springms.cloud.entity;
  2. import java.math.BigDecimal;
  3. public class User {
  4. private Long id;
  5. private String username;
  6. private String name;
  7. private Short age;
  8. private BigDecimal balance;
  9. public Long getId() {
  10. return this.id;
  11. }
  12. public void setId(Long id) {
  13. this.id = id;
  14. }
  15. public String getUsername() {
  16. return this.username;
  17. }
  18. public void setUsername(String username) {
  19. this.username = username;
  20. }
  21. public String getName() {
  22. return this.name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public Short getAge() {
  28. return this.age;
  29. }
  30. public void setAge(Short age) {
  31. this.age = age;
  32. }
  33. public BigDecimal getBalance() {
  34. return this.balance;
  35. }
  36. public void setBalance(BigDecimal balance) {
  37. this.balance = balance;
  38. }
  39. }
2.4 添加电影Web访问层Controller(springms-consumer-moviesrcmainjavacomspringmscloudcontrollerMovieController.java)

</>复制代码

  1. package com.springms.cloud.controller;
  2. import com.springms.cloud.entity.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. @RestController
  10. public class MovieController {
  11. @Autowired
  12. private RestTemplate restTemplate;
  13. @Value("${user.userServicePath}")
  14. private String userServicePath;
  15. @GetMapping("/movie/{id}")
  16. public User findById(@PathVariable Long id) {
  17. return this.restTemplate.getForObject(this.userServicePath + id, User.class);
  18. }
  19. }
2.5 添加电影微服务启动类(springms-consumer-moviesrcmainjavacomspringmscloudMsConsumerMovieApplication.java)

</>复制代码

  1. package com.springms.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * 电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务。
  9. *
  10. * @author hmilyylimh
  11. *
  12. * @version 0.0.1
  13. *
  14. * @date 2017/9/17
  15. *
  16. */
  17. @SpringBootApplication
  18. @EnableEurekaClient
  19. public class MsConsumerMovieApplication {
  20. @Bean
  21. public RestTemplate restTemplate(){
  22. return new RestTemplate();
  23. }
  24. public static void main(String[] args) {
  25. SpringApplication.run(MsConsumerMovieApplication.class, args);
  26. System.out.println("【【【【【【 电影微服务 】】】】】】已启动.");
  27. }
  28. }
三、测试

</>复制代码

  1. /****************************************************************************************
  2. 一、电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务:
  3. 1、启动 springms-discovery-eureka 模块服务,启动1个端口;
  4. 2、启动 springms-provider-user 模块服务,启动1个端口;
  5. 3、启动 springms-consumer-movie 模块服务,启动1个端口;
  6. 4、在浏览器输入地址 http://localhost:7900/simple/1 可以看到信息成功的被打印出来,说明用户微服务正常;
  7. 5、在浏览器输入地址 http://localhost:8761 并输入用户名密码 admin/admin 进入Eureka微服务显示在网页中,验证用户微服务、电影微服务确实注册到了 eureka 服务中;
  8. 6、在浏览器输入地址http://localhost:7901/movie/1 可以看到用户信息成功的被打印出来;
  9. ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70444.html

相关文章

  • SpringCloud 003 服务发现服务EurekaServer服务

    摘要:第篇服务发现服务端微服务一大致介绍众所周知,在现在互联网开发中,访问地址的和端口号是动态的,一个服务停掉再重新启用后和端口就可能发生了改变,所以用硬编码是肯定不行了。再对外提供服务的时候便不再使用挂掉的服务提供者的和端口。 SpringCloud(第 003 篇)服务发现服务端EurekaServer微服务 - 一、大致介绍 1、众所周知,在现在互联网开发中,访问地址的IP和端口号是...

    wuyumin 评论0 收藏0
  • SpringCloud 051 EurekaServer集群高可用注册心以及简单的安全认证

    SpringCloud(第 051 篇)EurekaServer集群高可用注册中心以及简单的安全认证 - 一、大致介绍 1、前面章节分析了一下 Eureka 的源码,我们是不是在里面注意到了 Peer 节点的复制,为什么要复制节点同步信息呢,其实就是为了同一个集群之间的EurekaServer一致性方案的一个实现; 2、于是我们在本章节就真正的来通过代码来实现一下EurekaServer之间的高...

    coordinate35 评论0 收藏0
  • SpringCloud 004 )用户服务类(添加服务注册,将用户服务注册 EurekaS

    摘要:接收参数对象添加用户微服务启动类用户服务类添加服务注册,将用户微服务注册到中。 SpringCloud(第 004 篇)用户服务类(添加服务注册,将用户微服务注册到 EurekaServer 中) - 一、大致介绍 通过添加注解 EnableEurekaClient,将用户微服务注册到 EurekaServer 中。 二、实现步骤 2.1 添加 maven 引用包 4.0....

    Darkgel 评论0 收藏0
  • SpringCloud 049 )Netflix Eureka 源码深入剖析(上)

    摘要:在应用启动后,将会向发送心跳默认周期为秒,如果在多个心跳周期没有收到某个节点的心跳,将会从服务注册表中把这个服务节点移除默认秒。进入类看看,看这个类的名字,见名知意,应该就是的启动类了。。。分析一由于是我们刚刚打断点 SpringCloud(第 049 篇)Netflix Eureka 源码深入剖析(上) - 一、大致介绍 1、鉴于一些朋友的提问并提议讲解下eureka的源码分析,由此...

    niuxiaowei111 评论0 收藏0

发表评论

0条评论

nifhlheimr

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<