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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - 詳解利用SpringCloud搭建一個最簡單的微服務框架

詳解利用SpringCloud搭建一個最簡單的微服務框架

2021-02-03 11:56程序猿DD Java教程

這篇文章主要介紹了詳解利用SpringCloud搭建一個最簡單的微服務框架,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Spring Cloud是一個基于Spring Boot實現的云應用開發工具,它為基于JVM的云應用開發中的配置管理、服務發現、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會話和集群狀態管理等操作提供了一種簡單的開發方式。

Spring Cloud包含了多個子項目(針對分布式系統中涉及的多個不同開源產品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項目。

1.微服務

微服務主要包含服務注冊,服務發現,服務路由,服務配置,服務熔斷,服務降級等一系列的服務,而Spring Cloud為我們提供了個一整套的服務;

詳解利用SpringCloud搭建一個最簡單的微服務框架

本例子為你提供了最簡單的一個服務發現例子,包含服務注冊發現spingCloudEurekaServer、服務配置中心spingCloudConfServer、以及一個app應用springCloudApp

2.服務注冊與發現

spingCloudEurekaServer

pom.xml

?
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.caicongyang</groupId>
 <artifactId>spingCloudEurekaServer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <parent>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-parent</artifactId>
  <version>Angel.SR6</version>
 </parent>
 <dependencies>  
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

Application.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.caicongyang.eureka;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
/**
 * Spring could EurekaServer程序主入口
 *
 * @author Administrator
 *
 */
@SpringBootApplication
@EnableEurekaServer
public class Application {
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}

application.yml  (可用properties替代)

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

3.服務配置(全局配置中心)

pom.xml

?
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
27
28
29
30
31
32
33
34
35
36
37
38
39
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.caicongyang</groupId>
 <artifactId>spingCloudConfServer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 
 <parent>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-parent</artifactId>
  <version>Angel.SR6</version>
 </parent>
 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
  <!-- sping cloud 注冊服務 -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
  <defaultGoal>compile</defaultGoal>
 </build>
</project>

application.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.caiconyang.conf;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
 
/**
 * Spring could conf程序主入口
 * @author Administrator
 *
 */
@SpringBootApplication
@EnableConfigServer
public class Application {
 public static void main(String[] args) { 
  SpringApplication.run(Application.class,args); 
 
}

application.properties

?
1
2
3
4
5
server.port=8888
## App配置文件所在git地址
spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springCloudConfigRepo.git
spring.cloud.config.server.git.searchPaths=repo
spring.application.name=spingCloudConfServer

4.App 

pom.xml

?
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.caicongyang</groupId>
 <artifactId>springCloudApp</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 
 <parent>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-parent</artifactId>
  <version>Angel.SR6</version>
 </parent>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.7</java.version>
  <java.encoding>UTF-8</java.encoding>
  <springfox.swagger.version>2.2.2</springfox.swagger.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- sping cloud 監控 http://localhost:8080/health -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <!-- sping cloud 注冊服務 -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
   <!-- sping cloud 路由 -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>${springfox.swagger.version}</version>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>${springfox.swagger.version}</version>
  </dependency>
 </dependencies>
  
 
 <build>
  <finalName>spingcould</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>${java.version}</source>
     <target>${java.version}</target>
     <encoding>${java.encoding}</encoding>
     <showWarnings>true</showWarnings>
    </configuration>
   </plugin>
  </plugins>
 </build>
 
 
</project>

Application.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.caicongyang.springCloudApp.main;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
/**
 * Spring could web程序主入口
 * @author Administrator
 *
 */
@Configuration//配置控制
@EnableAutoConfiguration//啟用自動配置
@ComponentScan(value={"com.caicongyang.springCloudApp"})//組件掃描
@EnableDiscoveryClient
public class Application {
 public static void main(String[] args) { 
  //第一個簡單的應用, 
  SpringApplication.run(Application.class,args); 
 
}

SwaggerConfig.java

?
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
27
28
29
package com.caicongyang.springCloudApp.conf;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 *
 * @author caicongyang1
 * @version id: SwaggerConfig, v 0.1 16/4/22 下午4:12 caicongyang1 Exp $$
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
 
 @Value("${swagger.ui.enable}") //該配置項在配置中心管理
 private boolean environmentSpecificBooleanFlag;
  
 @Bean
 public Docket docketFactory() {
  return new Docket(DocumentationType.SWAGGER_2).apiInfo(
   new ApiInfo("接口文檔", "SpingCloud web接口列表", "1.0", "", "", "", "")).enable(environmentSpecificBooleanFlag);
 }
}

application.properties

?
1
2
3
4
5
6
7
8
server.port=8080
spring.cloud.config.uri=http://127.0.0.1:8888
spring.cloud.config.name=springCloudApp
spring.cloud.config.profile=${config.profile:dev}
#service discovery url
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
#service name
spring.application.name=springCloudApp

5.測試與驗證

順序啟動服務注冊發現spingCloudEurekaServer、服務配置中心spingCloudConfServer、以及一個app應用springCloudApp

測試與驗證

1.訪問http://localhost:9999/eureka/  app是否已經注冊上來

2.訪問 http://localhost:8080/swagger-ui.html 是否正常訪問,如果正常訪問說明爭取讀取到config配置中心的swagger.ui.enable配置項

6.源碼:以上所有源碼:springcloud.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.jianshu.com/p/599c74a9035e

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好硬好大好浪夹得好紧h | 国产乱码在线精品可播放 | 国产第一页在线视频 | 亚洲2017久无码 | 色老板在线视频观看 | 精品国产在天天线在线麻豆 | 亚洲精品一二三四 | 欧美兽皇另类 | 好大好硬好深好爽想要之黄蓉 | 扒开女人下面 | 青青草国产青春综合久久 | 欧美一区二区三区在线观看不卡 | 久久国产视频网站 | 把内裤拔到一边高h1v1 | 久久久这里有精品999 | 色多多视频在线 | 欧美日韩中文字幕在线视频 | 国产成人亚洲精品乱码在线观看 | 手机看片自拍自自拍日韩免费 | 国产精品人人视频 | 免费观看韩剧网站在线观看 | 亚洲欧美日韩一区成人 | 精品视频中文字幕 | 爆操| 美女被狂干 | 国产精品反差婊在线观看 | 国产成人精品福利色多多 | 拍拍叫痛的无挡视频免费 | 国产农村一一级特黄毛片 | 全黄毛片 | 成 人 免费 小说在线观看 | 精品日韩二区三区精品视频 | 亚洲天堂影院 | 日韩精品在线视频观看 | 日本人成年视频在线观看 | 丝袜护士强制脚足取精 | 国产一区二区在线观看视频 | 久久亚洲国产成人影院 | 国产农村一一级特黄毛片 | 娇妻终于接受了3p的调教 | 91禁漫 |