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

栏目: Java · 发布时间: 5年前

内容简介:冬天手冷,能少写一句代码,就少写一句代码了,服务注册中心,还是用上一篇文章的注册中心,我这里重新搭建一个服务提供者,和服务消费者即可(哦,忘了点东西,提示一下下:注册中心这么关键的服务,如果是单点话,遇到故障就是毁灭性的,比如王者荣耀,要是炸了,如果我正在排位的话,是会骂人的,所以使用集群是很好的解决方案)上一篇文章我介绍了eureka服务注册中心的搭建,提了一下注册服务,今天仔细研究了一下,才发现我开始理解错了,尴尬Eureka的基本架构,由3个角色组成:

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

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

回顾

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

整理一下:

Eureka的基本架构,由3个角色组成:

1、Eureka Server

提供服务注册和发现

2、Service Provider

服务提供方

将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

服务消费方

从Eureka获取注册服务列表,从而能够消费服务

这就是微服务的思路了,一个提供注册中心的地方,服务提供者都将服务注册到注册中心,然后才是消费者来注册中心调用需要的服务\

服务提供者(provider)

1.创建项目

<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.开启提供者

@SpringBootApplication
@EnableDiscoveryClient //注册服务到注册中心去
public class MicroService2EurekaProviderApplication {

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

}

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

#配置服务提供者
server:
  port: 8886
spring:
  application:
    name: eureka-provider
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4.创建controller

/**
 * @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.测试

访问  http://localhost:8761/ 看到eureka-provider服务表示注册成功

服务消费者(ribbon版本)

1.创建项目

<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.开启消费者

@SpringBootApplication
@EnableDiscoveryClient //服务消费者
public class MicroService3EurekaRibbonApplication {

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

}

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

#配置服务消费者
server:
  port: 8887
spring:
  application:
    name: eureka-ribbon
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4.创建controller

/**
 * @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.测试

访问  http://192.168.0.101:8887/test  页面返回my name is test 服务提供者 表示成功

源码链接

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

转载保留版权

我的博客:https://aodeng.cc 我的公众号:低调小熊猫 我的QQ群:756796932

  • 本文作者:低调小熊猫
  • 本文链接:https://aodeng.cc/archives/ribbon
  • 版权声明:本博客所有文章除特别声明外,均采用 知识共享署名 4.0 国际许可协议 。转载请注明出处!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

一胜九败

一胜九败

柳井正 / 徐静波 / 中信出版社 / 2011-1-19 / 28.00元

优衣库成长的过程,就是一个历经了无数次失败的过程。他们经历过无法从银行融资的焦灼,经历过“衣服因低价热销,但人们买回去之后立即把商标剪掉”的难堪,经历过为上市冲刺而拼命扩张店铺的疯狂,也经历过被消费者冷落、疏离的苦痛……但正是从这些失败中学到的经验与教训,让柳井正走向了成功。 《一胜九败:优衣库风靡全球的秘密》就像是柳井正的错误集,在这里,他毫不隐晦地将公司业绩低迷的原因、进军海外失败的因素......一起来看看 《一胜九败》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具