您现在的位置是:群英 > 开发技术 > 编程语言
怎么用idea快速搭建spring-cloud?方法流程是什么?
Admin发表于 2022-10-12 17:46:43454 次浏览
在这篇文章中我们会学习到关于“怎么用idea快速搭建spring-cloud?方法流程是什么?”的知识,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望对大家学习或工作能有帮助。下面就请大家跟着小编的思路一起来学习一下吧。


package com.example.consumer;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@SpringBootApplication
 
//扫描所有包
@ComponentScan("com.test")
//声明为注册服务
@EnableEurekaClient
//把调用注册子模块接口引入到Spring容器中(不加此注解会出现找不到@FeignClient修饰的接口)
@EnableFeignClients("com.test")//包路径解决启动类在别的包下问题
 
public class ConsumerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
 
}

1、创建一个空的maven项目!

2、创建一个注册中心模块

3、配置注册中心

package com.example.eurekaserver;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
//声明为注册中心
@EnableEurekaServer
public class EurekaServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
 
}

配置文件改用yml,配置如下:

server:
  #运行端口
  port: 8888
eureka:
  instance:
    #注册ip
    hostname: localhost
  client:
    #禁止自己当做服务注册
    register-with-eureka: false
    #屏蔽注册信息
    fetch-registry: false
    #注册url
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动成功后,访问本地+端口即可看到注册中心页面,说明成功啦!

3、创建一个服务提供者(就是常写的spring-boot)

服务提供者配置,如下:

package com.example.provider;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
//声明为注册服务
@EnableEurekaClient
public class ProviderApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
 
}

 配置文件改用yml,配置如下:

eureka:
  client:
    serviceUrl:
      #服务注册地址
      defaultZone: http://localhost:8888/eureka/
server:
  #运行端口
  port: 8001
spring:
  application:
    #服务注册名称
    name: service-provider

 按照写springboot那样写一个查库接口

package com.example.provider;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
 
@SpringBootApplication
//声明为注册服务
@EnableEurekaClient
//扫描所有包
@ComponentScan("com.test")
//扫描mapper
@MapperScan("com.test.mapper")
public class ProviderApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
 
}

yml配置

eureka:
  client:
    serviceUrl:
      #服务注册地址
      defaultZone: http://localhost:8888/eureka/
server:
  #运行端口
  port: 8001
spring:
  application:
    #服务注册名称
    name: service-provider
  #数据库链接
  datasource:
    username: root
    password: yh996112
    url: jdbc:mysql://localhost:3306/yanghao?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.test.doman

OK,查库然后接口返回数据成功!

咱们的服务也在注册中心中注册成功啦! 

4、创建一个消费者服务

点击完成创建该模块

启动器配置

package com.example.consumer;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
 
@SpringBootApplication
//声明为注册服务
@EnableEurekaClient
//把调用注册子模块接口引入到Spring容器中(不加此注解会出现找不到@FeignClient修饰的接口)
@EnableFeignClients
public class ConsumerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
 
}

配置文件改用yml,配置如下:

eureka:
  client:
    serviceUrl:
      #服务注册地址
      defaultZone: http://localhost:8888/eureka/
server:
  #运行端口
  port: 8002
spring:
  application:
    #服务注册名称
    name: service-consumer
 

将服务提供者模块中的controller复制到消费者模块,如果涉及doman中文件就一并复制过去。

package com.example.consumer.service;
 
import com.example.consumer.doman.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
/**
 * @Description:使用@FeignClient注解调用注册子模块接口方法
 * @Author :y
 */
//注册子模块名称
@FeignClient("service-provider")
public interface TestService {
    //接口访问地址
    @GetMapping("index/index")
    public Test getTest(@RequestParam("id") Integer id);
}

接口调用该service

启动消费者,进行接口测试!

访问消费者接口没有问题,成功的调用了服务提供者的接口返回了数据!!!

以上就是idea快速部署springCloud的全部过程,其中发现了一个问题,在消费者模块中启动器貌似无法使用@ComponentScan注解扫描包,使用后启动会报错???具有原因没有了解,建议不要把启动类放在别的包。

2022-03-14:该问题解决!

问题描述:

在消费者模块中,当启动类在别的包下时,使用@ComponentScan扫描包来自动javaBean

但是因为service接口中的@FeignClient注解同样不在启动类的包下,所以仅用@ComponentScan扫描包而找不到@FeignClient同样会报错的。

所以在启动类的@EnableFeignClients注解应该指定包去扫描一下!!!

消费者模块配置如下:

 


到此这篇关于“怎么用idea快速搭建spring-cloud?方法流程是什么?”的文章就介绍到这了,感谢各位的阅读,更多相关怎么用idea快速搭建spring-cloud?方法流程是什么?内容,欢迎关注群英网络资讯频道,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-05-11 16:09:32 
摘要:在bootstrap中,data的意思是定义自定义属性,自定义属性一般使用“data-”开头,data属性的应用可以让HTML标签可以隐式的附带一些数据,而Javascript就可以对这些属性数据进行读写操作。
2022-09-01 17:26:14 
摘要:删除步骤:1、利用jQuery选择器获取指定div元素,语法“$("选择器")”,会返回一个包含指定div元素的jQuery对象;2、利用removeAttr()方法移除被选元素的class属性即可,语法“指定div元素对象.removeAttr("class");”。
2022-01-22 17:57:21 
摘要:Java的编码gbk不可映射字符怎么办?对于这个问题,有一些朋友不清楚要如何解决,对此这篇小编和大家一起来分析一下原因和了解解决方法,感兴趣的朋友就继续往下看吧。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部