spring boot-spring cloud
1.eureka
配置
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
yml
server:
port: 8761
eureka:
client:
register-with-eureka: false #是否将当前的Eureka Server服务作为客户端进行注册
fetch-registry: false #是否获取其他Eureka Server服务的数据
service-url:
defaultZone: http://localhost:8761/eureka/ #注册中心的访问地址
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @EnableEurekaServer:声明该类是一个 Eureka Server微服务,提供服务注册和服务发现功能,即注册中心
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
2.config
配置
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
yml
server:
port: 8888
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: https://github.com/southwind9801/aispringcloud.git
searchPaths: config
username: root
password: root
label: master
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
代码
启动类
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);
}
}
3.feign
配置
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
yml
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defalut: http://localhost:8761/eureka/
# Eureka服务注册中心会将自己作为客户端来尝试注册它自己,必須禁止 ***
# register-with-eureka: false
# fetch-registry: false
instance:
prefer-ip-address: true
#是否开启熔断器
feign:
hystrix:
enabled: true
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
/**
* @EnableFeignClients:
*/
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.haiyou"})
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class,args);
}
}
4.hystrix
配置
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--可视化界面,仪表盘-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
yml
server:
port: 8060
spring:
application:
name: hystrix
eureka:
client:
service-url:
defaultZoon: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
#开启熔断
feign:
hystrix:
enabled: true
#是通过hystrix.stream这个节点获取监控的请求数据,提供了可视化的监控界面
#去监控数据的时候访问这个节点,就会将数据都展示出来
#启动成功之后访问,可以监控到数据: http://localhost:8060/actuator/hystrix.stream
#访问此页面可以看到可视化监控界面,输入要监控的地址节点,即可看到该节点的可视化数据监控:http://localhost:8060/hystrix 将监控页面输入
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @EnableCircuitBreaker:声明启用数据监控
* @EnableHystrixDashboard:声明启用可视化数据监控
*/
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class,args);
}
}
5.ribbon
配置
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
yml
server:
port: 8040
spring:
application:
name: ribbon
eureka:
client:
service-url:
default: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @LoadBalanced :声明⼀个基于 Ribbon 的负载均衡。RestTemplate加上注解后,restTemplate对象就具有了负载均衡的能力
*/
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
6.resttemplate
配置
maven
<!--无-->
yml
#无
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(RestTemplateApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
7.网关
网关的特点:
1.安全,提供了统一访问入口,降低了服务器受攻击面积
2.提供了统一跨域解决方案
3.提供了统一日志记录操作,可以统一监控
4.提供了统一权限支持
5.提供了微服务限流功能,可以保护微服务,防止雪崩效应发生
6.微服务弯管的最主要作用是整合各个微服务功能,形成一套或者多套系统。
配置
maven
<!--无-->
yml
#无
代码
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(RestTemplateApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}