一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 在idea環(huán)境下構(gòu)建springCloud項(xiàng)目

在idea環(huán)境下構(gòu)建springCloud項(xiàng)目

2021-02-03 11:47onpwerb Java教程

本篇文章主要介紹了在idea環(huán)境下構(gòu)建springCloud項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

 springCloud是基于springboot搭建的微服務(wù)。它利用Spring Boot的開發(fā)便利性巧妙地簡化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開發(fā),如服務(wù)發(fā)現(xiàn)注冊、配置中心、消息總線、負(fù)載均衡、斷路器、數(shù)據(jù)監(jiān)控等,都可以用Spring Boot的開發(fā)風(fēng)格做到一鍵啟動和部署。

spring cloud官方文檔:http://projects.spring.io/spring-cloud/

spring cloud 中文網(wǎng) : https://springcloud.cc/

最終搭建后的工程源代碼:https://github.com/onpwerb/SpringCloud

一、新建maven工程

根據(jù)spring cloud官方文檔,在pom.xml導(dǎo)入如下代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- 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導(dǎo)入如下配置:

?
1
2
3
4
5
6
7
8
<!-- @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文件夾,然后新建一個(gè)application

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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的相關(guān)服務(wù)

?
1
2
3
4
5
6
7
8
9
10
server:
 port: 8081
eureka:
 instance:
  hostname: localhost
 client:
  registerWithEureka: false
  fetchRegistry: false
  serviceUrl:
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

三、構(gòu)建一個(gè)服務(wù)A

新建一個(gè)名為service的module

1.在src/main/java目錄下新建service文件夾,然后新建一個(gè)application

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

?
1
2
3
4
5
6
7
8
9
spring:
 application:
  name: service.service
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8081/eureka/
server:
 port: 8082

四、構(gòu)建第二個(gè)服務(wù)B

新建一個(gè)名為service2的module

1.在src/main/java目錄下新建service2文件夾,然后新建一個(gè)application

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

?
1
2
3
4
5
6
7
8
9
spring:
 application:
  name: service2
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8081/eureka/
server:
 port: 8083

五、配置網(wǎng)關(guān)

新建名稱為 gateway 的 module

1.在該module下的pom.xml導(dǎo)入如下配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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文件夾,然后新建一個(gè)application

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

六、啟動服務(wù)

先啟動discovery模塊,再啟動其他模塊

在瀏覽器依次輸入:

http://localhost:8081/
http://localhost:8082/service
http://localhost:8083/service2
http://localhost:8084/service/service
http://localhost:8084/service2/service2

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/onpwerb/article/details/70196118

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产va免费精品高清在线观看 | 侮辱丰满美丽的人妻 | 全黄h全肉细节文在线观看 全彩成人18h漫画 | 国产日韩一区二区三区在线播放 | 五月天黄网| 亚洲免费精品视频 | 精品日本一区二区 | 好男人资源大全免费观看 | 亚洲乱码尤物193yw在线播放 | 成人欧美一区二区三区黑人 | 美女舒服好紧太爽了视频 | 国产精品九九免费视频 | 韩国办公室激情 | fuqer日本老师 | 污污的动态图合集 | 亚洲精品久久久久福利网站 | www射com| 国产精品思瑞在线观看 | 欧美日韩高清观看一区二区 | 电车痴汉中文字幕 | 大学第一次基本都没了 | 国产精品九九免费视频 | 国产日产国无高清码2020 | narutomanga玖辛奈本子 | 女人肮脏的交易中文字幕未删减版 | 青青国产成人久久激情911 | 国产精品国产香蕉在线观看网 | 德国高清freexxxx性 | 成人看片免费无限观看视频 | 日本人与黑人做爰视频网站 | 羞羞私人影院可以直接免费观影吗 | 无人区在线观看免费国语完整版 | 精品国产成人AV在线看 | 本土自拍 | 三级黄色片在线观看 | 美女乳液| 亚洲成人一区在线 | 亚洲精品动漫在线观看 | 91小视频在线观看免费版高清 | 精品视频一区在线观看 | 国产草|