我的整理:
1. 建立父项目
- pom如下,只留 空文件夹 和 pom
<packaging>pom</packaging>
<modules>
<module>discovery</module>
<module>person</module>
<!--<module>ui</module>
<module>monitor</module>-->
</modules>
E版本,现在H版,2020 4,5版。2021,0版,1版。
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Edgware.SR2</version>
<relativePath/>
</parent>
cloud (因为是老版,还是引入为好)。 Web actuator test
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
maven 编译
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. discovery (eureka 和 配置中心)
2.1 pom 如下
<parent>
<groupId>com.example</groupId>
<artifactId>ch12</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
注意这个是 server ,不带server的 是eureka client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.2 注解 开启
@EnableConfigServer
@EnableEurekaServer
2.3 application.yml 配置 eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
- 配置完毕,访问一下 eureka
http://localhost:8761/
2.4 bootstrap.yml 配置 config server
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/config
#eureka:
# instance:
# non-secure-port: ${server.port:8888} #3 如果配置中心端口8888读不到,默认一个
# metadata-map: 注册中心特殊的配置。
# instanceId: ${spring.application.name}:${random.value} #4
resources的 config 目录下
person-dev.yml # 不加Dev 也可以
spring:
jpa:
database: HSQL
server:
port: 8086
nickName: dsfsdfds
- 测试配置中心
http://localhost:8761/person-dev.yml #有自己的配置,代表成功
http://localhost:8761/config/person-dev.yml #也可以
- 至此 配置中心 和 注册中心 配置完毕。
3. 数据库交互项目 person
3.1 pom引用
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
注册中心 和 配置中心 客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
//两个数据库,postgre 暂时不用。
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
3.2 注册中心 和 配置中心 开启
@EnableDiscoveryClient
//配置中心客户端,没有注解吧,就默认开启了
3.3 极简操作数据库的代码
@RestController
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public List<Person> savePerson(@RequestBody String personName) {
Person p = new Person(personName);
personRepository.save(p);
//查找所有
List<Person> people = personRepository.findAll(PageRequest.of(0, 10)).getContent();
return people;
}
@Value("${nickName}") //1
private String message;
@RequestMapping(value = "/getSome")
public String getSome(){
return message;
}
}
public interface PersonRepository extends JpaRepository<Person, Long> {
}
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
public Person(String name) {
super();
this.name = name;
}
}
3.4 加载 配置文件
bootstrap.yml
- 配置中心 提前拉取 。往注册中心 注册。
spring:
application:
name: person
cloud:
config:
enabled: true
# discovery:
# enabled: true 使用eureka模式,拉去配置的,一并都注释了。
# service-id: CONFIG #因为config项目 和 注册中心是 同一个。这个就不用了。
uri: http://127.0.0.1:8761 #就使用 简单的 http拉取。
profile: dev
label: master
eureka:
instance:
non-secure-port: ${server.port:8086}
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
application.yaml
#使用的数据库 端口 自定义信息,配置在了 配置中心
#jpa自动生成,放在了 本项目配置。
spring:
jpa:
hibernate:
ddl-auto: update
注册中心上线
:person:8086
http://localhost:8086/getSome 得到自己的消息。
- 至此 eureka 和 config 服务端和客户端 完成。
4. 使用下 feign 和 ribbon
- eureka 客户端 配置 和 person 项目一样。不再重复
4.1 引入 pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
4.2 注解开启
@EnableDiscoveryClient // 或 @EnableEurekaClient
@EnableFeignClients //1
@EnableCircuitBreaker //2 断路器
- 服务器端口 可以配置成80 等
4.3 feign接口 和 service
@FeignClient("person")
public interface PersonService {
@RequestMapping(method = RequestMethod.POST, value = "/save",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
List<Person> save(@RequestBody String name);
}
@Service
public class PersonHystrixOrRibbonService {
@Autowired
PersonService personService;
@HystrixCommand(fallbackMethod = "fallbackSave") //1
public List<Person> save(String name) {
return personService.save(name);
}
public List<Person> fallbackSave(String name){
List<Person> list = new ArrayList<>();
Person p = new Person(name+"没有保存成功,Person Service 故障");
list.add(p);
return list;
}
@Autowired
RestTemplate restTemplate; //1
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@HystrixCommand(fallbackMethod = "fallbackSome") //2
public String getSome() {//restTemplate 用项目名请求的,不行
return restTemplate.getForObject("http://localhost:8086/getSome", String.class);
}
public String fallbackSome(){
return "some service模块故障";
}
}
4.4 action 和 model
public class Person {
private Long id;
private String name;
}
@RestController
public class UiController {
@Autowired
private PersonHystrixOrRibbonService p;
@RequestMapping("/save")
public List<Person> sendMessage(@RequestBody String personName) {
return p.save(personName);
}
@RequestMapping(value = "/getSome", produces = {MediaType.TEXT_PLAIN_VALUE})
public String getSome() {
return p.getSome();
}
}
4.5 测试 feign 和 ribbion
http://localhost/getSome
http://localhost/save
json 就传递中文:李四 其他都不传递。
[
{
"id": 1,
"name": "张三"
},
{
"id": 2,
"name": "李四"
}
]
5. 加上 监控 断路器的功能
pom 和 配置
- 此时在 person上加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
@EnableHystrixDashboard
@EnableTurbine
进行测试
http://localhost:8761/hystrix.stream
填入:http://localhost/hystrix.stream (已经把UI换成80端口)
请求一次:http://localhost/save 即可监控到数据。 person打断点,进入 回退方法。监控到故障。
原文
基本命令
查看配置中心的配置
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.yml
- /{label}/{application}-{profile}.properties
/{application}-{profile}.yml
http://localhost:8888/config/some-.yaml
http://localhost:8888/config/some-docker.yaml
http://localhost:8888/person-dev.yml ,对应person.yml 也可以
开启各种服务
@Enable Config Server
@Enable Eureka Server 和 @Enable Eureka Client 或 @Enable Discovery Client
@Enable Zull Proxy
@Feign Client //Rest Template
@Enable Circuit Breaker 和 @HystrixCommand的 fallbackMethod
@Enable Hystrix Dashboard
Circuit
英 /ˈsɜːkɪt/ 美 /ˈsɜːrkɪt/ 全球(美国)
简明 牛津 新牛津 韦氏 柯林斯 例句 百科
n. 电路,回路;巡回活动,巡回地;环道,环线;巡回赛;赛车道;(英国)巡回审判区,巡回上诉法院;(一套)体操动作;统一经营的连锁剧院(或影院)
v. (绕……)环行
breaker
英 /ˈbreɪkə(r)/ 美 /ˈbreɪkər/ 全球(美国)
简明 牛津 新牛津 韦氏 柯林斯 例句 百科
n. [电] 断路器;打破者;碎浪
实例
1. 父类pom
<project xmlns="http://">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wisely</groupId>
<artifactId>ch12</artifactId>
<packaging>pom</packaging> //注意这个是pom
<version>1.0.0-SNAPSHOT</version>
<name>Cloud Native App</name>
<modules>
<module>config</module>
<module>discovery</module>
<module>ui</module>
<module>person</module>
<module>some</module>
<module>monitor</module>
</modules>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<!--<version>Edgware.SR2</version>-->
<version>Angel.SR3</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.9</version>
<configuration>
<skipDockerBuild>true</skipDockerBuild>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2. dsicover 注册中心
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://">
<artifactId>discovery</artifactId>
<packaging>jar</packaging> //打包方式为jar
<name>discovery</name>
<parent>
<groupId>com.wisely</groupId>
<artifactId>ch12</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<start-class>com.wisely.discovery.DiscoveryApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</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>
@EnableEurekaServer //1
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
3. 配置中心
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId> //引client更好吧
</dependency>
</dependencies>
@SpringBootApplication
@EnableConfigServer //1
@EnableEurekaClient //2
bootstrap.yml
spring:
application:
name: config #1
profiles:
active: native #2 本地配置放在 application.yml 否则 报错(默认git了)
eureka:
instance:
non-secure-port: ${server.port:8888} #3
metadata-map:
instanceId: ${spring.application.name}:${random.value} #4
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ #5
application.yml
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config #1
server:
port: 8888
config目录
- 开发环境
src/main/resources/config/person.yml
spring:
jpa:
database: HSQL
- 生产 docker 环境
person-docker.yml
spring:
jpa:
database: POSTGRESQL
datasource:
platform: postgres
url: jdbc:postgresql://postgres:5432/postgres
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS)
some.yml
my:
message: Message from Development
some-dokcer.yaml
my:
message: Message from Production
4. 服务模块 person 服务
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency> //config-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> //eureka
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency> //开发 hsqldb ,docker生产用 post gre SQL
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
controller
@EnableEurekaClient
@RestController
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public List<Person> savePerson(@RequestBody String personName) {
Person p = new Person(personName);
personRepository.save(p);
//查找所有
List<Person> people = personRepository.findAll(new PageRequest(0, 10)).getContent();
return people;
}
}
public interface PersonRepository extends JpaRepository<Person, Long>{
}
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
}
bootstrap.yml
spring:
application:
name: person
cloud:
config:
enabled: true
discovery:
enabled: true
service-id: CONFIG #1
# uri: http://127.0.0.1:8888 也一样
profile: dev
label: master
eureka:
instance:
non-secure-port: ${server.port:8086}
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
application.yml
server:
port: 8086
spring:
jpa:
hibernate:
ddl-auto: update
5. 服务模块 Some服务
action 和 pom
- pom 和 person 项目差不多(去掉 jpa)
@EnableDiscoveryClient
@RestController
public class SomeApplication {
@Value("${my.message}") //1
private String message;
@RequestMapping(value = "/getsome")
public String getsome(){
return message;
}
}
bootstrap.yml
spring:
application:
name: some
application.yml
server:
port: 8083
6. 界面模块 UI
pom
<dependencies>
<artifactId>spring-cloud-starter</artifactId>
<artifactId>spring-cloud-config-client</artifactId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angularjs</artifactId>
<version>1.3.15</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angular-ui-router</artifactId>
<version>0.2.13</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
</dependency>
</dependencies>
开启配置
@EnableEurekaClient
@EnableFeignClients //1
@EnableCircuitBreaker //2
@EnableZuulProxy //3
spring:
application:
name: ui
server:
port: 80
feign 和 使用
@FeignClient("person")
public interface PersonService {
@RequestMapping(method = RequestMethod.POST, value = "/save",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody List<Person> save(@RequestBody String name);
}
@Service
public class PersonHystrixService {
@Autowired
PersonService personService;
@HystrixCommand(fallbackMethod = "fallbackSave") //1
public List<Person> save(String name) {
return personService.save(name);
}
public List<Person> fallbackSave(String name){
List<Person> list = new ArrayList<>();
Person p = new Person(name+"没有保存成功,Person Service 故障");
list.add(p);
return list;
}
}
restTemplate
@Service
public class SomeHystrixService {
@Autowired
RestTemplate restTemplate; //1
@HystrixCommand(fallbackMethod = "fallbackSome") //2
public String getSome() {
return restTemplate.getForObject("http://some/getsome", String.class);
}
public String fallbackSome(){
return "some service模块故障";
}
}
bootstrap.yml
spring:
application:
name: ui
application.yml
server:
port: 8098
7. 断路器监控 monitor
- DashBoard
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
</dependencies>
@EnableHystrixDashboard
@EnableTurbine
spring:
application:
name: monitor
server:
port: 8989
8. 启动
启动 注册中心
启动 配置 中心
http://localhost:8098/
http://localhost:8761/
http://localhost:8989/hystrix.stream
填入:http://localhost/hystrix.stream (已经把UI换成80端口)