简介
解决庞杂的系统带来的臃肿和耦合过高等的问题。如果一个系统耦合性比较高,
那么容灾性就可能比较差。spring-clound可以把一个繁杂的系统,切分成一个一个的
小系统,相当于一个由一个小的服务,也就是业界比较流行的微服务架构。
学习spring-clound应该了解如下内容:
spring-clound中的微服务架构原则【待理解】
注册中心(服务发现&注册、kv配置中心)【这个有get到】
rpc(网络模块、协议)【完全不知道是什么】
限流熔断(触发策略、实现算法)【一点掉理解】
api网关(api网关作用、原理)【no理解,一脸懵逼】
统一日志【字面意思上的理解】、链路监控【二脸懵逼】
微服务基于容器的部署【有用过docker部署,但是docker 不理解】。
这里的eureka是一个服务注册和发现模块。
主要讲解基于eureka的服务注册于发现,虽然本司用的是基于consul的服务注册于发现
【之所以不用eureka,想来是因为后面spring团队不再在eureka上迭代开发了吧】。
maven 工程的创建,这里就不展开了。
本工程的目录结构大致如下:
工程传送门:
https://github.com/unnunique/SpringCloundDakaLearning/tree/master/Chapter-1
工程大致结构
Spring-Clound[springcloundlearning]
–Chapter1[chapter-1]
----Client[client]
----Server[server]
–Chpter2[chapter-2]
首先, maven parent 工程pom 文件如下:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.sydney.dream.springcloundgroupId>
<artifactId>parentartifactId>
<packaging>pompackaging>
<version>1.0.0version>
<modules>
<module>chapter-1module>
<module>Chapter-2module>
modules>
project>
pom 文件如下
<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">
<parent>
<artifactId>parentartifactId>
<groupId>com.sydney.dream.springcloundgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>chapter1artifactId>
project>
Server 代表注册中心,pom文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.sydney.dream.chapter1
server
0.0.1-SNAPSHOT
server
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
client pom 文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.sydney.dream.chapter1
client
0.0.1-SNAPSHOT
client
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false ##eureka 把eureka.client.register-with-eureka 以及eureka.client.fetch-registry 置为false 来标识这个服务是注册中心
eureka.client.fetch-registry=false
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka/
App
package com.sydney.dream.chapter1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
application.properties
server.port=8762
spring.application.name=srvice-clientdemo
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka/
App
package com.sydney.dream.chapter1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
注册中心地址: http://localhost:8761/
可以看到, 此时注册中心已经启动,但是注册中心还没有注册有服务。
启动后,在注册中心的显示如下。可以看到两个服务已经注册上来了。
http://localhost:8762/hi?name=didi
响应如下:
hi didi,i am from port:8762