在idea环境下构建springCloud项目
springCloud是基于springboot搭建的微服务。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。
spring cloud官方文档:http://projects.spring.io/spring-cloud/
spring cloud 中文网 : https://springcloud.cc/
最终搭建后的工程源代码:https://github.com/onpwerb/SpringCloud
一、新建maven工程
根据spring cloud官方文档,在pom.xml导入如下代码
<!-- spring cloud 配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
二、建立注册中心
新建名称为 discovery 的 module
1.在该module下的pom.xml导入如下配置:
<!-- @EnableEurekaServer -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<!--<version>1.1.6.RELEASE</version>-->
</dependency>
</dependencies>
2.在src/main/java目录下新建discovery文件夹,然后新建一个application
package discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplicaion {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplicaion.class, args);
}
}
3.在该module下的src/main/resources文件夹下,新建文件application.yml,配置注册中心eureka的相关服务
server:
port: 8081
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
三、构建一个服务A
新建一个名为service的module
1.在src/main/java目录下新建service文件夹,然后新建一个application
package service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceApplication {
@GetMapping("/service")
public String service(){
return "service";
}
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
2.在该module下的src/main/resources文件夹下,新建文件application.yml
spring: application: name: service.service eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8082
四、构建第二个服务B
新建一个名为service2的module
1.在src/main/java目录下新建service2文件夹,然后新建一个application
package service2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Service2Application {
@RequestMapping("/service2")
public String service2(){
return "service2";
}
public static void main(String[] args) {
SpringApplication.run(Service2Application.class, args);
}
}
2.在该module下的src/main/resources文件夹下,新建文件application.yml
spring: application: name: service2 eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8083
五、配置网关
新建名称为 gateway 的 module
1.在该module下的pom.xml导入如下配置:
package gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
2.在src/main/java目录下新建gateway文件夹,然后新建一个application
eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ spring: application: name: gateway server: port: 8084 zuul: routes: service: /service/** service2: /service2/**
3.在该module下的src/main/resources文件夹下,新建文件application.yml
六、启动服务
先启动discovery模块,再启动其他模块
在浏览器依次输入:
http://localhost:8081/
http://localhost:8082/service
http://localhost:8083/service2
http://localhost:8084/service/service
http://localhost:8084/service2/service2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringBoot+Vue前后端分离实现请求api跨域问题
这篇文章主要介绍了SpringBoot+Vue前后端分离实现请求api跨域问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-06-06
Java Apollo环境搭建以及集成SpringBoot案例详解
这篇文章主要介绍了Java Apollo环境搭建以及集成SpringBoot案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08
mybatis mybatis-plus-generator+clickhouse自动生成代码案例详解
这篇文章主要介绍了mybatis mybatis-plus-generator+clickhouse自动生成代码案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08
IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案
面对IDEA项目启动时报出的Flyway迁移校验和不匹配问题,核心在于保持迁移脚本的一致性、正确管理和理解Flyway的工作机制,本文介绍IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案,感兴趣的朋友一起看看吧2024-01-01
@Transactional跟@DS动态数据源注解冲突的解决
这篇文章主要介绍了@Transactional跟@DS动态数据源注解冲突的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09


最新评论