版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aodeng110/article/details/89042168
服务提供者(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 国际许可协议。转载请注明出处!