java编程开发之快速构建springCloud的步骤
使用springCloud项目进行开发,如果使用多模块进行操作的话,可以在项目中分布各个组件,本文是爱站技术频道小编和大家介绍的java编程开发之快速构建springCloud的步骤 ,希望本文的介绍能为大家带来参考。
一、新建maven工程
根据spring cloud官方文档,在pom.xml导入如下代码
org.springframework.boot spring-boot-starter-parent 1.4.5.RELEASE org.springframework.cloud spring-cloud-dependencies Camden.SR6 pom import org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka
二、建立注册中心
新建名称为 discovery 的 module
1.在该module下的pom.xml导入如下配置:
org.springframework.cloud spring-cloud-netflix-eureka-server
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模块,再启动其他模块
以上就是爱站技术频道介绍的java编程开发之快速构建springCloud的步骤 ,项目的每一个操作步骤都是不一样的,希望能够对你们有所帮助。
上一篇:探讨网络爬虫的实现代码
下一篇:如何检测JAVA编程的异常代码