springCloud入门

时间:2021-08-23 16:51:21   收藏:0   阅读:19

1、SpringCloud是什么

技术分享图片
技术分享图片

2、SpringCloud与SpringBoot关系

技术分享图片

3、SpringCloud与Dubbo的区别

技术分享图片

4、入门案例

  <!--springcloud-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Greenwich.SR1</version>
      <type>pom</type>
      <scope>import</scope>
  </dependency>
  <!--springboot-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.4.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
  </dependency>
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    =================================================
    //使用 RestTemplate 远程访问http服务,需要先注入到spring
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://localhost:8081";

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        System.out.println(dept);
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }

技术分享图片

5、Eureka注册中心

//依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>


//application.yml配置
server:
  port: 7003
#Eureka配置
eureka:
  instance:
    hostname: eureka7003 #服务端名称
  client:
    register-with-eureka: false #是否向 eureka 注册
    fetch-registry: false #是否向 eureka 获取信息
    service-url:
      defaultZone: http://localhost:7001/eureka/
//依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

//提供方 application.yml配置
spring:
  application:
    name: springcloud-provide
#Eureka配置,服务注册
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

//消费方 application.yml配置
#eureka配置
eureka:
  client:
    register-with-eureka: false #不向eureka注册
    service-url:
     defaultZone: http://localhost:7001/eureka/

//消费方使用**服务名**调用服务
    //eureka 通过服务名访问
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDE";

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        System.out.println(dept);
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }

原文:https://www.cnblogs.com/lq-404/p/15176223.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!