欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在idea环境下构建springCloud项目

程序员文章站 2024-04-01 22:32:52
 springcloud是基于springboot搭建的微服务。它利用spring boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置...

 springcloud是基于springboot搭建的微服务。它利用spring boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用spring boot的开发风格做到一键启动和部署。

spring cloud官方文档:

spring cloud 中文网 :

最终搭建后的工程源代码: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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。