SpringCloud入门学习

SpringCloud基于SpringBoot,是一系列框架组件的集合,为实现基于微服务的架构设计的。这里简单的使用Spring Cloud Eureka来实现服务治理。

1.在pom.xml中添加依赖

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

2.在springboot工程的启动application类上加@EnableEurekaServer 注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3.在application.yml配置文件中添加registerWithEureka和fetchRegistry配置信息

rver:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.SpringCloudAlibaba基于SpringCloud,服务间调用采用Feign,首先需要新建一个FeignService接口。其@FeignClient注解的值必须为nacos已存在的微服务,容器才能找的到该服务进行调用。其方法申明的注解请求方式和路径,必须跟Controller的对应得上,而Controller里面的该方法还需要加上@inside注解表示可跨服务调用。参数前加上@PathVariable和@RequestBody表示为跨服务http请求,真正的查询其实在Controller里面的该方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值