Spring Cloud系列教程

介绍
在微服务架构中,业务被拆分为多个微服务,服务之间的通讯基于http restful。在spring cloud中,服务调用方式有两种 1.Ribbon+RestTemplate 2.Fegin

github地址:https://github.com/erlieStar/spring-cloud-learning

建一个服务生产者
示例项目:producer-simple(spring-cloud-ribbon)

1.项目配置如下
pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1
2
3
4
application.yaml

server:
  port: 8001

spring:
  application:
    name: producer-simple

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
1
2
3
4
5
6
7
8
9
10
11
2.启动类加上@EnableEurekaClient注解

@RestController
@EnableEurekaClient
@SpringBootApplication
public class ProducerSimple {

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

    @Value("${server.port}")
    private String port;

    @RequestMapping("hello")
    public String hello(@RequestParam String name) {
        return "hello, " + name  + " i am from port: " + port;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
启动eureka-service(spring-cloud-eureka)
接着启动2个producer-simple(spring-cloud-ribbon)
初始端口为8001,启动一个实例后,修改端口为8002,再启动一个实例

在idea中启动2个实例的方法如下

1.点击Edit Configurations

2.勾选Allow parallel run

3.依次点击如下2个按钮


访问 http://localhost:7001,可以看到有2个实例


建一个服务消费者
示例项目:consumer-ribbon(spring-cloud-ribbon)

1.项目配置如下

pom.xml

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
application.yaml

server:
  port: 9000

spring:
  application:
    name: consumer-ribbon

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
1
2
3
4
5
6
7
8
9
10
11
2.启动类加上@EnableEurekaClient注解

向程序注入RestTemplate,并通过@LoadBalanced注解开启客户端负载均衡

@RestController
@EnableEurekaClient
@SpringBootApplication
public class ConsumerRibbon {

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

    @Autowired
    private RestTemplate restTemplate;

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

    @GetMapping("hello")
    public String hello(@RequestParam String name) {
        return restTemplate.getForObject("http://PRODUCER-SIMPLE/hello?name=" + name, String.class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
访问http://localhost:9000/hello?name=xiaoshi

交替显示

hello xiaoshi, I am from port: 8001
hello xiaoshi, I am from port: 8002
1
2
参考博客
[1]https://www.fangzhipeng.com/springcloud/2018/08/02/sc-f2-ribbon.html
————————————————
版权声明:本文为CSDN博主「Java识堂」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zzti_erlie/article/details/104092792

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值