SpringCloud-Eureka 服務註冊中心搭建--高可用以及負載均衡配置例項
阿新 • • 發佈:2018-11-11
前言:
由於公司使用的分散式框架太老,慢慢轉移使用SpringBoot微服務框架,後臺框架存在很多問題,為了優化底層服務,現採取如下措施: 0、Nexus搭建Maven私服 (集中管理jar包版本) 1、分散式配置管理:springcloud-config 2、持續整合:jenkins (實現自動化部署) 3、服務治理:Eureka (類似zookeeper服務註冊,服務分發) 4、容錯管理:Hystrix (實現斷路器模式,幫助服務以來中出現的延遲和為故障提供強大的容錯能力)
5、閘道器:Zuul(提供智慧路由,訪問過濾等功能)
6、效能監控:logstash(日誌監控),elasticsearch(資料檢索),kibana(圖形化分析)
說明:
服務註冊中心:scmd-eurekaserver 服務提供者:hello-service 不同埠號啟動服務如:8081,8082 服務消費者:ribbon-cousumer 直接從服務註冊中心調服務
一、服務註冊中心:scmd-eurekaserver -------------------------------------------------------------------------
1,application-peer1.yml:
spring:
application:
name: scmd-eurekaserver
server:
port: 1111
eureka.instance.hostname: peer1
eureka.client.serviceUrl.defaultZone: http://peer2:1112/eureka/
2,application-peer2.yml: spring:
application:
name: scmd-eurekaserver
server:
port: 1112
eureka.instance.hostname: peer2
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/
3,pom.xml: <dependencies>
<!-- eureka server 依賴 --> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>LATEST</version>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
EurekaserverApplication.java: package com.scmd ;
import org.springframework.boot.Banner ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration ;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer ;
@EnableEurekaServer
@EnableAutoConfiguration( exclude={DataSourceAutoConfiguration. class})
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class) ;
app.setBannerMode(Banner.Mode. OFF) ;
app.run(args) ;
}
}
4,高可用配置:application-peer1.yml,application-peer2.yml mvn clean install -P beta 進入target目錄:進入DOS視窗執行命令: java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer1 java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer2 高可用:互相註冊服務,一個服務掛了,另一個服務還可保障繼續執行
一、服務註冊中心:scmd-eurekaserver-------------------------------------------------------------------------
二、服務提供者:hello-service-------------------------------------------------------------------------
mvn clean install -P beta 使用如下命令啟動服務 java -jar hello-service-1.0.jar --server.port=8081 java -jar hello-service-1.0.jar --server.port=8082
1,pom.xml <dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-starter-eureka </artifactId>
<version>LATEST </version>
</dependency> <!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-dependencies </artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1 </version>
<type>pom </type>
<scope>import </scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: hello-service
server:
port: 1113
context-path: /hello/
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
3,Controller @RestController
@RequestMapping( "/hello")
public class ELKController {
@GetMapping( "hello")
public ServiceResponse hello(){
System. out.println( "呵呵呵呵呵呵呵呵呵呵呵呵") ;
return new ServiceResponse().setData( "hello") ;
}
}
服務提供者向兩個服務註冊中心註冊服務,再由註冊中心釋出服務
二、服務提供者:hello-service-------------------------------------------------------------------------
三、服務消費者:ribbon-cousumer ------------------------------------------------------------------------- 1,pom.xml <dependencies> <!-- eureka server 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,執行類--RibbonConsumerApplication @EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//負載均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class) ;
app.setBannerMode(Banner.Mode. OFF) ;
app.run(args) ;
}
}
4,向註冊中心呼叫服務 Controller: @RestController
@RequestMapping( "/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping( "ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody() ;
}
}
呼叫消費者ribbon介面 http://localhost:1115/ribbon/ribbon/ribbon 此時從服務註冊中心呼叫HELLO-SERVICE中hello介面服務(多次呼叫) 由於使用ribbon實現了負載均衡,可在HELLO-SERVICE 8081和8082服務控制檯看到交替日誌,正面負載均衡實現成功
三、服務消費者:ribbon-cousumer -------------------------------------------------------------------------
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,執行類--RibbonConsumerApplication
@EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//負載均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main (String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class ) ;
app.setBannerMode(Banner.Mode. OFF ) ;
app.run(args) ;
}
}
4,向註冊中心呼叫服務 Controller: @RestController
@RequestMapping( "/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping( "ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody() ;
}
}
呼叫消費者ribbon介面 http://localhost:1115/ribbon/ribbon/ribbon 此時從服務註冊中心呼叫HELLO-SERVICE中hello介面服務(多次呼叫) 由於使用ribbon實現了負載均衡,可在HELLO-SERVICE 8081和8082服務控制檯看到交替日誌,正面負載均衡實現成功
三、服務消費者:ribbon-cousumer -------------------------------------------------------------------------
由於公司使用的分散式框架太老,慢慢轉移使用SpringBoot微服務框架,後臺框架存在很多問題,為了優化底層服務,現採取如下措施: 0、Nexus搭建Maven私服 (集中管理jar包版本) 1、分散式配置管理:springcloud-config 2、持續整合:jenkins (實現自動化部署) 3、服務治理:Eureka (類似zookeeper服務註冊,服務分發) 4、容錯管理:Hystrix (實現斷路器模式,幫助服務以來中出現的延遲和為故障提供強大的容錯能力)
服務註冊中心:scmd-eurekaserver 服務提供者:hello-service 不同埠號啟動服務如:8081,8082 服務消費者:ribbon-cousumer 直接從服務註冊中心調服務
一、服務註冊中心:scmd-eurekaserver
1,application-peer1.yml:
spring:
application:
name: scmd-eurekaserver
server:
port: 1111
eureka.instance.hostname:
eureka.client.serviceUrl.defaultZone: http://peer2:1112/eureka/
2,application-peer2.yml: spring:
application:
name: scmd-eurekaserver
server:
port: 1112
eureka.instance.hostname: peer2
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/
3,pom.xml: <dependencies>
<!-- eureka server 依賴 --> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>LATEST</version>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
EurekaserverApplication.java: package com.scmd ;
import org.springframework.boot.Banner ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration ;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer ;
@EnableEurekaServer
@EnableAutoConfiguration( exclude={DataSourceAutoConfiguration. class})
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class) ;
app.setBannerMode(Banner.Mode. OFF) ;
app.run(args) ;
}
}
4,高可用配置:application-peer1.yml,application-peer2.yml mvn clean install -P beta 進入target目錄:進入DOS視窗執行命令: java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer1 java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer2 高可用:互相註冊服務,一個服務掛了,另一個服務還可保障繼續執行
一、服務註冊中心:scmd-eurekaserver-------------------------------------------------------------------------
二、服務提供者:hello-service-------------------------------------------------------------------------
mvn clean install -P beta 使用如下命令啟動服務 java -jar hello-service-1.0.jar --server.port=8081 java -jar hello-service-1.0.jar --server.port=8082
1,pom.xml <dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-starter-eureka </artifactId>
<version>LATEST </version>
</dependency> <!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-dependencies </artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1 </version>
<type>pom </type>
<scope>import </scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: hello-service
server:
port: 1113
context-path: /hello/
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
3,Controller @RestController
@RequestMapping( "/hello")
public class ELKController {
@GetMapping( "hello")
public ServiceResponse hello(){
System. out.println( "呵呵呵呵呵呵呵呵呵呵呵呵") ;
return new ServiceResponse().setData( "hello") ;
}
}
服務提供者向兩個服務註冊中心註冊服務,再由註冊中心釋出服務
二、服務提供者:hello-service-------------------------------------------------------------------------
三、服務消費者:ribbon-cousumer ------------------------------------------------------------------------- 1,pom.xml <dependencies> <!-- eureka server 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,執行類--RibbonConsumerApplication @EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//負載均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class) ;
app.setBannerMode(Banner.Mode. OFF) ;
app.run(args) ;
}
}
4,向註冊中心呼叫服務 Controller: @RestController
@RequestMapping( "/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping( "ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody() ;
}
}
呼叫消費者ribbon介面 http://localhost:1115/ribbon/ribbon/ribbon 此時從服務註冊中心呼叫HELLO-SERVICE中hello介面服務(多次呼叫) 由於使用ribbon實現了負載均衡,可在HELLO-SERVICE 8081和8082服務控制檯看到交替日誌,正面負載均衡實現成功
三、服務消費者:ribbon-cousumer -------------------------------------------------------------------------
1,pom.xml <!-- eureka server 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- eureka server 依賴 -->
</dependencies>
<!-- eureka server -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Finchley.RC1</version>-->
<!--<version>${spring-cloud.version}</version>-->
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka server -->
2,application.yml spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,執行類--RibbonConsumerApplication
@EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//負載均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main (String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication. class ) ;
app.setBannerMode(Banner.Mode. OFF ) ;
app.run(args) ;
}
}
4,向註冊中心呼叫服務 Controller: @RestController
@RequestMapping( "/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping( "ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody() ;
}
}
呼叫消費者ribbon介面 http://localhost:1115/ribbon/ribbon/ribbon 此時從服務註冊中心呼叫HELLO-SERVICE中hello介面服務(多次呼叫) 由於使用ribbon實現了負載均衡,可在HELLO-SERVICE 8081和8082服務控制檯看到交替日誌,正面負載均衡實現成功
三、服務消費者:ribbon-cousumer -------------------------------------------------------------------------