Spring Cloud 服務是一種分散式服務,包括配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態等公共元件。

一  註冊機, Spring Cloud使用erureka server。服務在啟動的時候,會將自己要釋出的服務註冊到服務註冊中心;執行時,如果需要呼叫其他微服務的介面,那麼就要先到註冊中心獲取服務提供者的地址,拿到地址後,通過微服務容器內部的簡單負載均衡期進行路由用。

pom.xml配置如下:

<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>
<parent>
<groupId>com.rgzx</groupId>
<artifactId>rgzx-platform-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>rg-register-server</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2 註解@EnableEurekaServer加在springboot工程的啟動類上

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class RegisterServerApplication {

public static void main(String[] args) {
SpringApplication.run(RegisterServerApplication.class, args);
}
}

3 配置註冊機監控

#註冊機監控 http://localhost:8090/
spring.application.name = registerServer
server.port = 8090
eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

二 介面API ,主要實現資料池配置,日誌跟蹤,資料庫配置等介面開發相關,主要配置如下

1 註解服務類

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RestController;

import com.sun.glass.ui.Application;
@EnableEurekaClient
@SpringBootApplication
@RestController
@MapperScan("org.com.xx.mapper") // 告訴Mapper所在的包名
public class MobileServerApplication {

public static void main(String[] args) {
SpringApplication.run(MobileServerApplication.class, args);
}
//②
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(Application.class);
}
}

2 服務介面配置

eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

3 pom.xml配置 

三 負載均衡服務,實現負載均衡,熔斷,服務介面狀態管理,主要使用feign.hystrix實現

1 hystrix配置

#http://localhost:8091/actuator/hystrix.stream
#http://localhost:8091/hystrix

eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

feign.hystrix.enabled=true

management.endpoints.web.exposure.include=*

feign.hystrix.httpclient.enabled=true
feign.hystrix.httpclient.max-connections=200
feign.hystrix.okhttp.enabled=false
feign.hystrix.httpclient.max-connections-per-route=50

feign.hystrix.threadpool.default.coreSize=60
feign.hystrix.threadpool.default.maximumSize=5000

2 註解服務

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import com.sun.glass.ui.Application;

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ProxyServerApplication {

public static void main(String[] args) {
SpringApplication.run(ProxyServerApplication.class, args);
}
//②
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(Application.class);
}

}

3 pom.xml 引用相關主要jar

<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>

四  註冊機服務監控

1application.properties

#http://localhost:8092
server.port=8092
spring.application.name= monitoradminserver
eureka.instance.preferIpAddress=true
eureka.instance.health-check-url-path: /actuator/health
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
eureka.client.registryFetchIntervalSeconds = 5
management.endpoints.web.exposure.include:"*"
management.endpoint.health.show-details: ALWAYS
management.endpoints.web.base-path=/
spring.boot.admin.client.url=http://localhost:8092
spring.profiles.active=prod

info.app.name="@[email protected]"
info.app.description="@[email protected]"
info.app.version="@[email protected]"
info.spring-boot-version="@[email protected]"
[email protected]@

spring.endpoints.health.sensitive= true
spring.endpoints.cors.allowed-methods=HEAD,GET,POST

spring.endpoints.shutdown.enabled=true

spring.endpoints.shutdown.sensitive=false

2 註解類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;

 

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class MonitorAdminServerApplication {

public static void main(String[] args) {
SpringApplication.run(MonitorAdminServerApplication.class, args);
}
@Profile("insecure")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()//
.and().csrf().disable();
}
}

@Profile("secure")
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;

public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}

protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");

http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf().disable();
// @formatter:on
}
}

}

五 鏈路跟蹤,跟蹤api的呼叫情況,使用Zipkin元件,要使用com.alibaba.druid.pool.DruidDataSource資料鏈接池,注意有個資料庫

1 application.properties

#http://localhost:8093/
server.port=8093
spring.application.name = 
management.metrics.web.server.auto-time-requests=false

#zipkin.storage.type=mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zipkindb?useUnicode=true&characterEncoding=UTF-8&useSSL=true
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

sleuth.enabled=false

2 註解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import zipkin.server.EnableZipkinServer;
import zipkin2.storage.mysql.v1.MySQLStorage;

@SpringBootApplication
@EnableZipkinServer
public class LinkServerApplication {

public static void main(String[] args) {
SpringApplication.run(LinkServerApplication.class, args);
}

@Bean
public MySQLStorage mySQLStorage(DataSource datasource) {
return MySQLStorage.newBuilder().datasource(datasource).executor(Runnable::run).build();
}
}

spring cloud 微服務實現了多應用分散式整套架構,從應用註冊到應用狀態監控,服務介面建立到資料鏈接監控,還包含了負載均衡實現,其中負載均衡還含有熔斷機制。滿足大部分運維使用。社群活躍,使用案例廣泛。