当前位置: 移动技术网 > IT编程>软件设计>架构 > Spring Cloud 微服务项目实现总架构一

Spring Cloud 微服务项目实现总架构一

2018年12月29日  | 移动技术网IT编程  | 我要评论

 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 微服务实现了多应用分布式整套架构,从应用注册到应用状态监控,服务接口建立到数据链接监控,还包含了负载均衡实现,其中负载均衡还含有熔断机制。满足大部分运维使用。社区活跃,使用案例广泛。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网