一、什么是springcloud,有什么作用
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙地簡化了分布式系統基礎設施的開發,如服務發現注冊、配置中心、消息總線、負載均衡、斷路器、數據監控等,都可以用Spring Boot的開發風格做到一鍵啟動和部署。
二、springcloud的基本使用
1.服務的注冊與發現
①創建一個parent maven項目springcloud,然后在該maven項目下面創建兩個子項目eureka-server,eureka-client
Ⅰ、springcloud
<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.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>eureka-server</module> <module>eureka-clinet</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Ⅱ.eureka-server

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>eureka-server Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run( EurekaServerApplication.class, args ); } }
#eureka-server 配置文件application.properties #該服務名稱 spring.application.name=eurka-server #該服務的端口號 server.port=8000 #設置當前實例的主機名稱 eureka.instance.hostname=localhost #是否向服務注冊中心注冊自己 eureka.client.registerWithEureka=false #檢索服務 eureka.client.fetchRegistry=false #服務注冊中心的配置內容,指定服務注冊中心的位置(在單注冊中心的情況下,自己本身是注冊中心,那么不需要配置這個) #eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
Ⅲ.eureka-client

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>eureka-clinet</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>eureka-clinet Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.demo.eureka_client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ServiceClientApplication { public static void main(String[] args) { SpringApplication.run( ServiceClientApplication.class, args ); } }
server.port=8001 spring.application.name=eureka-client eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
將eureka-server和eureka-client啟動后,訪問localhost:8000將會看到如下頁面,說明服務 注冊成功

2.服務消費者(ribbon+restTemplate)
Ⅰ.還是用上面的例子中的erueka-server作為服務注冊中心;
Ⅱ.創建服務提供者eureka-echo
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>eureka-echo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>eureka-echo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>eureka-echo</finalName> </build> </project>
@SpringBootApplication @EnableEurekaClient @RestController public class ServiceEchoApplication { public static void main(String[] args) { SpringApplication.run( ServiceEchoApplication.class, args ); } @Value("${server.port}") String port; @RequestMapping("/echo") public String home(@RequestParam(value = "name") String name) { return "hello " + name + " ,this is port:" + port; } }
#application.properties spring.application.name=eureka-echo server.port=8001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
修改application.properties中的端口號,並啟動兩次eureka-echo兩次,這樣就相當於一個服務集群
Ⅲ.創建服務消費者consumer-ribbon,並啟動
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>consumer-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>consumer-ribbon Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies> </project>
package com.demo.consumer_ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //如果注冊中心是EUREKA則只需要@EnableEurekaClient即可,如果是其他服務注冊中心,則需要@EnableDiscoveryClient public class ConsumerRibbonApplication { public static void main(String[] args) { SpringApplication.run( ConsumerRibbonApplication.class, args ); } @Bean @LoadBalanced //實現負載均衡 RestTemplate restTemplate() { return new RestTemplate(); } }
package com.demo.consumer_ribbon.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.demo.consumer_ribbon.service.EchoService; @RestController public class EchoController { @Autowired EchoService echoService; @GetMapping(value = "/echo") public String hi(@RequestParam String name) { return echoService.echo( name ); } }
package com.demo.consumer_ribbon.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class EchoService { @Autowired RestTemplate restTemplate; public String echo(String name) { return restTemplate.getForObject("http://EUREKA-ECHO/echo?name="+name,String.class); } }
#application.properties spring.application.name=consumer-ribbon server.port=8003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
Ⅳ、通過http://localhost:8000/ 查看注冊的服務;訪問http://localhost:8003/echo?name=kyle 可以看到會間隔調用兩個eureka-echo服務,達到負載均衡的效果。

3.服務消費者(feign)
Ⅰ.繼續使用上面eureka-server,eureka-echo
Ⅱ.創建消費者consumer-feign,並啟動

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>consumer-feign</groupId> <artifactId>consumer-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>consumer-feign Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
package com.demo.consumer_feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run( ConsumerFeignApplication.class, args ); } }
package com.demo.consumer_feign.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.demo.consumer_feign.service.EchoService; @RestController public class EchoController { @Autowired private EchoService echoService; @GetMapping(value = "/echo") public String sayHi(@RequestParam String name) { return echoService.echo( name ); } }
package com.demo.consumer_feign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "eureka-echo")//value值是指定調用哪個服務 public interface EchoService { //value值是指定調用那個方法 @RequestMapping(value = "/echo",method = RequestMethod.GET) String echo(@RequestParam(value = "name") String name); }
spring.application.name=consumer-feign server.port=8004 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
Ⅲ.訪問http://localhost:8004/echo?name=kyle 可以看出在兩種結果之間切換
4.斷路器
Ⅰ.改造erueka-ribbon,增加熔斷功能
①pom文件增加
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
②在啟動類ConsumerRibbonApplication上面增加注解@EnableHystrix
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //如果注冊中心是EUREKA則只需要@EnableEurekaClient即可,如果是其他服務注冊中心,則需要@EnableDiscoveryClient @EnableHystrix public class ConsumerRibbonApplication { public static void main(String[] args) { SpringApplication.run( ConsumerRibbonApplication.class, args ); } @Bean @LoadBalanced //實現負載均衡 RestTemplate restTemplate() { return new RestTemplate(); } }
③在Service方法上增加 @HystrixCommand(fallbackMethod = "error"),並新增出錯后調用的方法error();
@Service public class EchoService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "error") public String echo(String name) { return restTemplate.getForObject("http://EUREKA-ECHO/echo?name="+name,String.class); } public String error(String name) { return "hi,"+name+",Service is error!"; } }
④調用http://localhost:8004/echo?name=kyle,發現可以在兩個服務之間切換。然后停掉其中一個eureka-echo服務,剛開始可能會返回"hi,kyle,Service is error!",然后不會再去訪問該服務,而去調用集群中的其他服務。
Ⅱ.改造consumer-feign,增加熔斷功能。
①feign自帶熔斷功能,只需要在application.properties中添加feign.hystrix.enabled=true,就可以開啟。
spring.application.name=consumer-feign server.port=8004 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ feign.hystrix.enabled=true
②在EchoService接口的注解上增加fallback屬性,並且屬性值為EchoService的實現類。
package com.demo.consumer_feign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "eureka-echo",fallback = EchoServiceHystric.class)//value值是指定調用哪個服務 public interface EchoService { //value值是指定調用那個方法 @RequestMapping(value = "/echo",method = RequestMethod.GET) String echo(@RequestParam(value = "name") String name); }
③創建EchoServiceHystric 類
package com.demo.consumer_feign.service; import org.springframework.stereotype.Component; @Component public class EchoServiceHystric implements EchoService{ @Override public String echo(String name) { return "sorry service is error;"; } }
④把eureka-echo服務停掉之后就會返回 sorry service is error;
5.路由網關
Ⅰ.創建eureka-zuul

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>erueka-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>erueka-zuul Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> <build> <finalName>erueka-zuul</finalName> </build> </project>
package com.demo.erueka_zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @EnableDiscoveryClient public class EurekaZuulApplication { public static void main(String[] args) { SpringApplication.run( EurekaZuulApplication.class, args ); } }
spring.application.name=eureka-zuul server.port=8005 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ #api-a,api-b這個可以隨意配置 zuul.routes.api-a.path=/ribbon/** zuul.routes.api-a.serviceId=consumer-ribbon zuul.routes.api-b.path=/feign/** zuul.routes.api-b.serviceId=consumer-feign
Ⅱ.訪問http://localhost:8005/ribbon/echo?name=kyle ,根據路由配置,會去調用consumer-ribbon的服務
Ⅲ.訪問http://localhost:8005/feign/echo?name=kyle,根據路由配置,會去調用consumer-feign的服務
6.分布式配置中心
由於在分布式系統中服務數量很多,為了方便統一管理服務配置文件並且實時更新,就需要用到分布式配置中心。在spring cloud config 組件中,分兩個角色,一是config server,二是config client
Ⅰ.創建config-server

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>config-server Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
package com.demo.config_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
spring.application.name=config-server server.port=9000 spring.cloud.config.server.git.uri=https://github.com/kyleInJava/gitworkspace/ spring.cloud.config.server.git.searchPaths= spring.cloud.config.label=master spring.cloud.config.server.git.username= spring.cloud.config.server.git.password=
Ⅱ.創建config-client

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>config-client Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> </project>
package com.demo.config_client; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${name}") String name; @RequestMapping(value = "/getName") public String hi(){ return name; } }
spring.application.name=config-client server.port=9001 spring.cloud.config.label=master spring.cloud.config.profile=pro spring.cloud.config.uri= http://localhost:9000/
Ⅲ、分布式配置中心的使用說明
①在啟動config-server后,訪問http://localhost:9000/kyle/pro/master 如果得到下面的返回,則說明配置服務中心可以從遠程程序獲取配置信息。

②訪問http://localhost:9001/getName 會返回如下文件中的值

③請求地址和對應資源的映射關系

上面的映射關系中
{application} 對應的就是下面的spring.application.name
{profile} 對應的是spring.cloud.config.profile,表示運行環境:dev表示開發環境,test表示測試環境,pro表示生產環境。
{label} 對應的就是下面的spring.cloud.config.label 表示資源文件的分支,默認為master
所以下面的config-client的配置文件對應的資源文件為config-client-pro.properties

Ⅳ.配置高可用分布式配置中心
只需要將config-server和config-client在服務注冊中心進行注冊,這樣就可以直接通過服務名稱來代替uri進行訪問。
①config-server 和 config-client 的pom文件中引入jar包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
②config-server 的application.properties配置文件
spring.application.name=config-server server.port=9002 spring.cloud.config.server.git.uri=https://github.com/kyleInJava/gitworkspace/ spring.cloud.config.server.git.searchPaths= spring.cloud.config.label=master spring.cloud.config.server.git.username= spring.cloud.config.server.git.password= #增加下面一行,進行服務注冊 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
③config-client 的配置文件bootstrap.properties 配置文件
spring.application.name=config-client server.port=9001 spring.cloud.config.label=master spring.cloud.config.profile=pro #spring.cloud.config.uri= http://localhost:9000/ #增加下面三行,注釋掉上面的spring.cloud.config.uri,用服務名來訪問服務 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server
7.服務鏈路追蹤(Spring Cloud Sleuth)
Ⅰ.構建server-zipkin,只需要從網上下載jar包運行即可。
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

Ⅱ.改造前面的eureka-echo和consumer-ribbon
在pom文件中引入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
在application.properties文件中加入spring.zipkin.base-url=http://localhost:9411
改造完成,分別啟動eureka-server,eureka-echo,consumer-ribbon,然后調用http://localhost:8003/echo?name=kyle ,會得到如下的效果顯示

8.構建高可用服務注冊中心
Ⅰ.創建eureka-server和eureka-server2,具體構建和上面的erueka-server創建一樣,只是application.properties需要改動,配置如下:
#eureka-server 配置文件application.properties
#該服務名稱
spring.application.name=eurka-server
#該服務的端口號
server.port=8000
#設置當前實例的主機名稱
eureka.instance.hostname=peer1
#是否向服務注冊中心注冊自己
eureka.client.registerWithEureka=false
#檢索服務
eureka.client.fetchRegistry=false
#服務注冊中心的配置內容,指定服務注冊中心的位置
eureka.client.serviceUrl.defaultZone=http://peer2:8009/eureka/
#eureka-server2 配置文件application.properties
#該服務名稱
spring.application.name=eurka-server
#該服務的端口號
server.port=8009
#設置當前實例的主機名稱
eureka.instance.hostname=peer2
#是否向服務注冊中心注冊自己
eureka.client.registerWithEureka=false
#檢索服務
eureka.client.fetchRegistry=false
#服務注冊中心的配置內容,指定服務注冊中心的位置
eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/
提示:需要在hosts文件中配置peer1 和peer2映射到127.0.0.1
Ⅱ.訪問http://localhost:8000/和http://localhost:8009/可以看出兩個注冊服務都相互在對方有注冊,並且其他服務只需要在某一台上注冊,就會在兩台上面注冊。


9.斷路監控儀表盤
Ⅰ.改造erueka-ribbon,改造后的配置如下

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>consumer-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>consumer-ribbon Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> </project>
package com.demo.consumer_ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //如果注冊中心是EUREKA則只需要@EnableEurekaClient即可,如果是其他服務注冊中心,則需要@EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker public class ConsumerRibbonApplication { public static void main(String[] args) { SpringApplication.run( ConsumerRibbonApplication.class, args ); } @Bean @LoadBalanced //實現負載均衡 RestTemplate restTemplate() { return new RestTemplate(); } }
package com.demo.consumer_ribbon.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.demo.consumer_ribbon.service.EchoService; @RestController public class EchoController { @Autowired EchoService echoService; @GetMapping(value = "/echo") public String hi(@RequestParam String name) { return echoService.echo( "ribbon:"+name ); } }
package com.demo.consumer_ribbon.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class EchoService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "error") public String echo(String name) { return restTemplate.getForObject("http://EUREKA-ECHO/echo?name="+name,String.class); } public String error(String name) { return "hi,"+name+",Service is error!"; } }
spring.application.name=consumer-ribbon server.port=8003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ spring.zipkin.base-url=http://localhost:9411 management.endpoints.web.exposure.include=* management.endpoints.web.cors.allowed-origins=* management.endpoints.web.cors.allowed-methods=*
Ⅱ.訪問http://localhost:8003/actuator/hystrix.stream 得到下面的效果,則說明監控成功

Ⅲ.訪問http://localhost:8003/hystrix 得到下圖

輸入http://localhost:8003/actuator/hystrix.stream ,就能看到儀表盤監控

10.斷路器聚合監控(Hystrix Turbine)
Ⅰ.構建eureka-turbine,來進行聚合監控

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>eureka-trubine</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>eureka-trubine Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> </dependencies> </project>
package com.demo.eureka_trubine; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker @EnableTurbine public class EurekaTurbineApplication { public static void main(String[] args) { SpringApplication.run( EurekaTurbineApplication.class, args ); } }
#####application.properties 配置文件 spring.application.name=eureka-turbine server.port=8008 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ management.endpoints.web.exposure.include=* management.endpoints.web.cors.allowed-origins=* management.endpoints.web.cors.allowed-methods=* #監控這兩個服務 turbine.app-config=consumer-ribbon,consumer-feign turbine.aggregator.clusterConfig=default turbine.clusterNameExpression="default" turbine.combine-host=true turbine.instanceUrlSuffix.default=actuator/hystrix.stream
Ⅱ.改造前面的eureka-ribbon和eureka-feign
①改造后的eureka-ribbon配置如下:
package com.demo.consumer_ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //如果注冊中心是EUREKA則只需要@EnableEurekaClient即可,如果是其他服務注冊中心,則需要@EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker //新增的注解 public class ConsumerRibbonApplication { public static void main(String[] args) { SpringApplication.run( ConsumerRibbonApplication.class, args ); } @Bean @LoadBalanced //實現負載均衡 RestTemplate restTemplate() { return new RestTemplate(); } }
②改造eureka-feign
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>eureka-feign</groupId> <artifactId>eureka-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>eureka-feign Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
package com.demo.consumer_feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker //新增注解
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run( ConsumerFeignApplication.class, args );
}
}
#####application.properties
spring.application.name=consumer-feign
server.port=8004
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
feign.hystrix.enabled=true
management.endpoints.web.exposure.include=* management.endpoints.web.cors.allowed-origins=* management.endpoints.web.cors.allowed-methods=*
Ⅲ.分別訪問http://localhost:8003/actuator/hystrix.stream 、 http://localhost:8004/actuator/hystrix.stream 以及http://localhost:8008/turbine.stream,能夠得到類似下面的返回值則說明改造成功

Ⅳ.訪問http://localhost:8008/hystrix,然后輸入http://localhost:8008/turbine.stream,進行服務監控


11.消息總線
安裝rabbitmq不成功,環境一直配不好,以后再弄
