Spring Cloud 微服务项目实现总架构一
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="@project.name@"
info.app.description="@project.description@"
info.app.version="@project.version@"
info.spring-boot-version="@project.parent.version@"
info.version=@project.version@
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 微服务实现了多应用分布式整套架构,从应用注册到应用状态监控,服务接口建立到数据链接监控,还包含了负载均衡实现,其中负载均衡还含有熔断机制。满足大部分运维使用。社区活跃,使用案例广泛。
Spring Cloud 微服务项目实现总架构一的更多相关文章
- 【spring colud】spring cloud微服务项目搭建【spring boot2.0】
spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...
- Spring Cloud微服务下的权限架构调研
随着微服务架构的流行,系统架构调整,项目权限系统模块开发提上日程,需要对权限架构进行设计以及技术选型.所以这段时间看了下相关的资料,做了几个对比选择. 一.架构图 初步设想的架构如下,结构很简单:eu ...
- spring cloud微服务项目的发布与部署
普通的javaweb项目要发布的话,一般就三种方法: 1.把项目直接放在tomcat的webApps下启动tomcat即可. 2.把项目打包成war包放在webApps下,启动tomcat,自动解压w ...
- 一张图了解Spring Cloud微服务架构
Spring Cloud作为当下主流的微服务框架,可以让我们更简单快捷地实现微服务架构.Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟.经得起实际考验的服务框架组合起来 ...
- Dubbo和Spring Cloud微服务架构比较
Dubbo 出生于阿里系,是阿里巴巴服务化治理的核心框架,并被广泛应用于中国各互联网公司:只需要通过 Spring 配置的方式即可完成服务化,对于应用无入侵,设计的目的还是服务于自身的业务为主. 微服 ...
- Dubbo 和 Spring Cloud微服务架构 比较及相关差异
你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构. 微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务, ...
- 全链路实践Spring Cloud 微服务架构
Spring Cloud 微服务架构全链路实践Spring Cloud 微服务架构全链路实践 阅读目录: 网关请求流程 Eureka 服务治理 Config 配置中心 Hystrix 监控 服务调用链 ...
- Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议
一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入spri ...
- Spring Cloud 微服务架构解决方案
1 理解微服务 1.1 软件架构演进 软件架构的发展经历了从单体结构.垂直架构.SOA架构到微服务架构的过程. 1.1.1 单体架构 特点: 1.所有的功能集成在一个项目工程中. 2.所有的功能打一个 ...
随机推荐
- EOS开发经验总结——不定期持续更新中
一.新手安装mysql乱码问题 1.数据库安装时设置默认编码格式为UTF8或者打开mysql安装目录下my.ini,变更default-character-set=utf8: 2.打开EOS的Gove ...
- Qt中常用知识点
1:QRegExp 正则表达式 QRegExp regExp("[a-zA-Z][1-9][0-9]{0,2}"); xxx->setValidator(new QRegEx ...
- OpenGL学习--02--绘制一个红色三角形
The OpenGL buffer is created, bound, filled and configured with the standard functions (glGenBuffers ...
- 如何使用 adb 命令实现自动化测试
如何使用 adb 命令实现自动化测试 一.前提: 1.打开手机调试模式,确保手机已正常连接电脑,可在电脑上通过adb devices命令查看,结果如下说明连接成功: List of devices a ...
- windows 删除删除不掉的文件
DEL /F /A /Q \\?\%1RD /S /Q \\?\%1 windows下删除删除不掉的文件: 1.打开记事本,把上面的命令复制进去 2.保存,后缀名改为.bat,ok 3.把想要删除的文 ...
- 异度之刃 Xenoblade 后感
WII版重置的N3DS劣化版异度之刃终于通关了.在出色的自制系统的快乐NTR的帮助下,充分体验到了神作的剧情史诗感. 关于游戏的玩法系统,从现在来看8年前的游戏,缺点显而易见,特别是跑地图这回事,地图 ...
- shrio的简单认识
博客讲解; shrio的知识储备 shrio的简单认识 笔记整理地址: Shrio.pdf 下载 Shrio理论.doc 下载 shrio知识储备.doc 下载
- 风险管理,未雨绸缪——《代码之殇》读书笔记II
这次的内容主要是关于软件开发过程中的风险管理,包括项目用时估计.产品的发布与更新.承诺兑现的重要性. ①项目用时估计 有人会质疑项目用时估计的可靠性,因为就事而言,这次的任务可能和上次不一样了,开发环 ...
- 最常见到的runtime exception 异常
ArithmeticException, 算术异常ArrayStoreException, 将数组类型不兼容的值赋值给数组元素时抛出的异常BufferOverflowException, 缓冲区溢出异 ...
- apache软件no_ssl和openssl两种类型的区别
apache软件同一版本有两种类型:no_ssl和openssl: openssl多了个ssl安全认证模式,它的协议是HTTPS而不是HTTP,这就是带有SSL的服务器与一般网页服务器的区别了. 一般 ...