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