【微服务架构】springcloud微服务架构搭建
要会⽤,⾸先要了解。图懒得画,借鉴⽹上⼤⽜的图吧,springcloud组建架构如图:
微服务架构的应⽤场景:
1、系统拆分,多个⼦系统
2、每个⼦系统可部署多个应⽤,应⽤之间负载均衡实现
3、需要⼀个服务注册中⼼,所有的服务都在注册中⼼注册,负载均衡也是通过在注册中⼼注册的服务来使⽤⼀定策略来实现。
4、所有的客户端都通过同⼀个⽹关地址访问后台的服务,通过路由配置,⽹关来判断⼀个URL请求由哪个服务处理。请求转发到服务上的时候也使⽤负载均衡。
5、服务之间有时候也需要相互访问。例如有⼀个⽤户模块,其他服务在处理⼀些业务的时候,要获取⽤户服务的⽤户数据。
6、需要⼀个断路器,及时处理服务调⽤时的超时和错误,防⽌由于其中⼀个服务的问题⽽导致整体系统的瘫痪。
7、还需要⼀个监控功能,监控每个服务调⽤花费的时间等。
Spring Cloud的优势
产出于spring⼤家族,spring在企业级开发框架中⽆⼈能敌,来头很⼤,可以保证后续的更新、完善。⽐如dubbo现在就差不多死了
有spring Boot 这个独⽴⼲将可以省很多事,⼤⼤⼩⼩的活spring boot都搞的挺不错。
作为⼀个微服务治理的⼤家伙,考虑的很全⾯,⼏乎服务治理的⽅⽅⾯⾯都考虑到了,⽅便开发开箱即⽤。
Spring Cloud 活跃度很⾼,教程很丰富,遇到问题很容易到解决⽅案
轻轻松松⼏⾏代码就完成了熔断、均衡负责、服务中⼼的各种平台功能
废话少说,看代码:
项⽬架构:
⼀、discovery服务注册发现
①、l
<?xml version="1.0"?>
<project
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd" xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId&</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>discovery</artifactId>
<name>discovery</name>
<url>www.popumusic</url>
<properties>
<start-class>com.wisely.discovery.DiscoveryApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>springboot框架的作用
②、DiscoveryApplication
package com.wisely.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloudflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //1
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
③、l
server:
port: 8761
endpoints:
shutdown:
enabled: true
sensitive: false
eureka:
instance:
prefer-ip-address: true  #启⽤IP⽅式
ip-address: 127.0.0.1
client:
register-with-eureka: false #指向其他注册中⼼地址
fetch-registry: false
service-url:
defualtZone: 127.0.0.1:8762/eureka/
⼆、monitor监控服务
①、l
<?xml version="1.0"?>
<project
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd" xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId&</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.poputar</groupId>
<artifactId>popumonitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>popumonitor</name>
<url></url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
②、PopumonitorApplication
package com.poputar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloudflix.eureka.EnableEurekaClient;
import org.springframework.cloudflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloudflix.turbine.EnableTurbine;
/**
* 监控服务
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class PopumonitorApplication
{
public static void main( String[] args )
{
SpringApplication.run(PopumonitorApplication.class, args);
}
}
③、l
server:
port: 8989
④、l
spring:
application:
name: monitor
eureka:
instance:
nonSecurePort: ${server.port:8989}
client:
serviceUrl:
defaultZone: ${eureka.host:localhost}:${eureka.port:8761}/eureka/
三、配置服务
①、l

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。