前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >服务提供者(provider)与服务消费者(ribbon版本)-微服务架构

服务提供者(provider)与服务消费者(ribbon版本)-微服务架构

作者头像
低调小熊猫
发布2020-06-05 17:39:22
1.2K0
发布2020-06-05 17:39:22
举报
文章被收录于专栏:低调小熊猫

服务提供者(provider)与服务消费者(ribbon版本)-微服务架构

冬天手冷,能少写一句代码,就少写一句代码了,服务注册中心,还是用上一篇文章的注册中心,我这里重新搭建一个服务提供者,和服务消费者即可(哦,忘了点东西,提示一下下:注册中心这么关键的服务,如果是单点话,遇到故障就是毁灭性的,比如王者荣耀,要是炸了,如果我正在排位的话,是会骂人的,所以使用集群是很好的解决方案)

回顾

上一篇文章我介绍了eureka服务注册中心的搭建,提了一下注册服务,今天仔细研究了一下,才发现我开始理解错了,尴尬

整理一下:

Eureka的基本架构,由3个角色组成: 1、Eureka Server 提供服务注册和发现 2、Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 3、Service Consumer 服务消费方 从Eureka获取注册服务列表,从而能够消费服务 这就是微服务的思路了,一个提供注册中心的地方,服务提供者都将服务注册到注册中心,然后才是消费者来注册中心调用需要的服务

服务提供者(provider)

1.创建项目

代码语言:javascript
复制
<description>服务提供</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.开启提供者

代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient //注册服务到注册中心去
public class MicroService2EurekaProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroService2EurekaProviderApplication.class, args);
    }

}

3.配置yml文件,注册服务

代码语言:javascript
复制
#配置服务提供者
server:
  port: 8886
spring:
  application:
    name: eureka-provider
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4.创建controller

代码语言:javascript
复制
/**
 * @program:hope
 * @author:aodeng
 * @blog:低调小熊猫(https://aodeng.cc)
 * @微信公众号:低调小熊猫
 * @create:2019-01-14 21:25
 **/
@RestController
public class TestProviderController {
    @RequestMapping("/test")
    public String test(){
        return "my name is test 服务提供者";
    }
}

5.测试

代码语言:javascript
复制
访问  http://localhost:8761/ 看到eureka-provider服务表示注册成功

服务消费者(ribbon版本)

1.创建项目

代码语言:javascript
复制
<description>服务消费者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

2.开启消费者

代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient //服务消费者
public class MicroService3EurekaRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroService3EurekaRibbonApplication.class, args);
    }

}

3.配置yml文件,注册服务

代码语言:javascript
复制
#配置服务消费者
server:
  port: 8887
spring:
  application:
    name: eureka-ribbon
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4.创建controller

代码语言:javascript
复制
/**
 * @program:hope
 * @author:aodeng
 * @blog:低调小熊猫(https://aodeng.cc)
 * @微信公众号:低调小熊猫
 * @create:2019-01-14 21:46
 **/
@RestController
public class TestRibbonController {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/test")
    public String test(){
        return restTemplate.getForObject("http://eureka-provider/test",String.class);
    }
}

5.测试

代码语言:javascript
复制
访问  http://192.168.0.101:8887/test  页面返回my name is test 服务提供者 表示成功

源码链接

https://github.com/java-aodeng/hope

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 低调小熊猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 服务提供者(provider)与服务消费者(ribbon版本)-微服务架构
    • 回顾
      • 整理一下:
      • 服务提供者(provider)
      • 服务消费者(ribbon版本)
      • 源码链接
      相关产品与服务
      微服务引擎 TSE
      微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档