前言:
由于公司使用的分布式框架太老,慢慢转移使用SpringBoot微服务框架,后台框架存在很多问题,为了优化底层服务,现采取如下措施:
0、Nexus搭建Maven私服 (集中管理jar包版本)
1、分布式配置管理:springcloud-config
2、持续集成:jenkins (实现自动化部署)
3、服务治理:Eureka (类似zookeeper服务注册,服务分发)
4、容错管理:Hystrix (实现断路器模式,帮助服务以来中出现的延迟和为故障提供强大的容错能力)
5、网关:Zuul(提供智能路由,访问过滤等功能)
6、性能监控:logstash(日志监控),elasticsearch(数据检索),kibana(图形化分析)
说明:
服务注册中心:scmd-eurekaserver
服务提供者:hello-service 不同端口号启动服务如:8081,8082
服务消费者:ribbon-cousumer 直接从服务注册中心调服务
一、服务注册中心:scmd-eurekaserver-------------------------------------------------------------------------
1,application-peer1.yml:
spring:
application:
name: scmd-eurekaserver
server:
port: 1111
eureka.instance.hostname: peer1
eureka.client.serviceUrl.defaultZone: http://peer2:1112/eureka/
2,application-peer2.yml:
spring:
application:
name: scmd-eurekaserver
server:
port: 1112
eureka.instance.hostname: peer2
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/
3,pom.xml:
EurekaserverApplication.java:
package com.scmd;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
4,高可用配置:application-peer1.yml,application-peer2.yml
mvn clean install -P beta
进入target目录:进入DOS窗口执行命令:
java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer1
java -jar scmd-eurekaserver-1.0.jar --spring.profiles.active=peer2
高可用:互相注册服务,一个服务挂了,另一个服务还可保障继续运行
一、服务注册中心:scmd-eurekaserver-------------------------------------------------------------------------
二、服务提供者:hello-service-------------------------------------------------------------------------
mvn clean install -P beta
使用如下命令启动服务
java -jar hello-service-1.0.jar --server.port=8081
java -jar hello-service-1.0.jar --server.port=8082
1,pom.xml
2,application.yml
spring:
application:
name: hello-service
server:
port: 1113
context-path: /hello/
eureka.client.serviceUrl.defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
3,Controller
@RestController
@RequestMapping("/hello")
public class ELKController {
@GetMapping("hello")
public ServiceResponse hello(){
System.out.println("呵呵呵呵呵呵呵呵呵呵呵呵");
return new ServiceResponse().setData("hello");
}
}
服务提供者向两个服务注册中心注册服务,再由注册中心发布服务
二、服务提供者:hello-service-------------------------------------------------------------------------
三、服务消费者:ribbon-cousumer -------------------------------------------------------------------------
1,pom.xml
2,application.yml
spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,运行类--RibbonConsumerApplication
@EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//负载均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
4,向注册中心调用服务
Controller:
@RestController
@RequestMapping("/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping("ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody();
}
}
调用消费者ribbon接口
http://localhost:1115/ribbon/ribbon/ribbon
此时从服务注册中心调用HELLO-SERVICE中hello接口服务(多次调用)
由于使用ribbon实现了负载均衡,可在HELLO-SERVICE 8081和8082服务控制台看到交替日志,正面负载均衡实现成功
三、服务消费者:ribbon-cousumer -------------------------------------------------------------------------
1,pom.xml
2,application.yml
spring:
application:
name: ribbon-cousumer
server:
port: 1115
context-path: /ribbon/
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
3,运行类--RibbonConsumerApplication
@EnableDiscoveryClient
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaserverApplication {
//负载均衡配置ribbon
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
//SpringApplication.run(EurekaserverApplication.class, args);
SpringApplication app = new SpringApplication(EurekaserverApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
4,向注册中心调用服务
Controller:
@RestController
@RequestMapping("/ribbon")
public class ELKController {
@Autowired
RestTemplate restTemplate;
@GetMapping("ribbon")
public String hello(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello/hello/hello",String.class).getBody();
}
}
调用消费者ribbon接口
http://localhost:1115/ribbon/ribbon/ribbon
此时从服务注册中心调用HELLO-SERVICE中hello接口服务(多次调用)
由于使用ribbon实现了负载均衡,可在HELLO-SERVICE 8081和8082服务控制台看到交替日志,正面负载均衡实现成功
三、服务消费者:ribbon-cousumer -------------------------------------------------------------------------
欢迎加入Java猿社区