SpringCloud微服务在SpringBoot项目中的搭建

需要注意的点:

一、所有的子模块都依赖于父模块,所以子模块的pom文件中不需要太多的依赖,除非是只有自己用的依赖才需要单独引用。

二、父模块的pom文件中应引入子模块信息,用modules标签包裹,每个子模块用module包上。

三、共有依赖管理配置(spring cloud微服务的核心),如下:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

以上,即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建一个新的Spring Boot项目 首先,我们需要在IDEA创建一个新的Spring Boot项目。在IDEA的欢迎页面选择“Create New Project”,然后选择“Spring Initializr”。 在Spring Initializr页面,我们需要填写一些项目的基本信息,包括项目名称、描述、包名、Java版本、Spring Boot版本等。在这里,我们选择使用JDK 8,Spring Boot 2.2.2版本。 2. 添加Spring Cloud依赖 在创建完项目后,我们需要添加Spring Cloud的依赖。在pom.xml文件添加以下依赖: ``` <dependencies> <!-- Spring Cloud --> <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-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Others --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies> ``` 这些依赖包括了Spring Cloud Eureka、Spring Cloud Config、Spring Cloud OpenFeign、Spring Cloud Zipkin等。 3. 配置Eureka注册心 在application.yml文件添加以下配置: ``` spring: application: name: service-demo cloud: config: uri: http://localhost:8888 profile: dev zipkin: base-url: http://localhost:9411 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 这里我们使用了Eureka作为服务注册心,将服务的注册地址配置为http://localhost:8761/eureka/。 4. 创建服务接口 我们创建一个简单的服务接口,用于测试Spring Cloud的服务调用功能。 ``` public interface HelloService { @GetMapping("/hello") String hello(@RequestParam("name") String name); } ``` 5. 创建服务实现 创建服务实现类并使用@FeignClient注解标记为Feign客户端。 ``` @Service @FeignClient("service-provider") public class HelloServiceImpl implements HelloService { @Override public String hello(String name) { return "Hello " + name + "!"; } } ``` 6. 启用服务发现 在应用主类上添加@EnableDiscoveryClient注解以启用服务发现。 ``` @SpringBootApplication @EnableDiscoveryClient public class ServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(ServiceConsumerApplication.class, args); } } ``` 7. 测试服务调用 在测试类注入HelloService并进行调用测试。 ``` @RunWith(SpringRunner.class) @SpringBootTest public class HelloServiceTest { @Autowired private HelloService helloService; @Test public void testHello() { String result = helloService.hello("world"); System.out.println(result); Assert.assertEquals("Hello world!", result); } } ``` 8. 启动服务并注册到Eureka 将服务启动后,可以在Eureka的管理页面查看到该服务的注册信息。 至此,一个简单的Spring Cloud微服务项目就已经搭建完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值