微服务
单体应用架构:
特点:体积庞大,部署时间长,技术更新慢,不利于扩展
微服务架构:
特点:
整个系统由一系列独立运行的微服务组成
每个微服务只关注特定的业务
微服务之间通过RestFul接口进行调用
微服务可以使用不同的技术,独立,独立部署
组件:
整合Netflix的开源组件:
Eureka 服务治理组件
Ribbon 客户端负载均衡组件
Hystrix 容错管理组件
Feign 声明式服务调用组件
Zuul 服务网关组件
Config 分布式配置中心(自带的)
Eureka入门
1. 分为服务端和客户端
2. 创建微服务项目作为服务端
Eureka服务端
配置SpringCloud的依赖版本管理(依赖于springboot )
<!--配置1-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.6.RELEASE</version> <relativePath/>
</parent>
<!--配置2-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加spring-cloud-starter-netflix-eureka-server依赖
<!--配置6:注这里不需要配置版本,已经在父类配置好了-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
注:这里需要注意的是必须继承父类配置
在启动类上添加@EnaleEurekaServer注解
/** * 配置7:注册中心服务器 * 添加注解 */
@EnableEurekaServer
@SpringBootApplication
public class DemoEurekaserviceApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaserviceApplication.class, args);
}
}
配置Eureka参数,定义注册中心地址
#配置8:端口i
server.port=8808
#配置服务名称
spring.application.name=eureka-server
#配置是否注册到Eureka服务(这里本身就是,所有false)
eureka.client.register-with-eureka=false
#配置是否从Eureka 上查询服务
eureka.client.fetch-registry=false
#配置主机名(localhost)
eureka.instance.hostname=127.0.0.1
#注册中心的url(serviceUrl不要横线) eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
注:启动成功后(Eureka后台管理页面),有:当前系统状态,当前注册上来的客户端等其他信息
3. 商品微服务和订单微服务注册到Eureka上
Eureka客户端(商品微服务)
添加spring-cloud-starter-netflix-eureka-client依赖
<!--配置1:注这里不需要配置版本,已经在父类配置好了-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置2:子继父-->
<parent>
<groupId>com.cdz.springcloud</groupId>
<artifactId>demo_microservice</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.2.5.RELEASE</version>-->
<relativePath/>
<!-- lookup parent from repository-->
</parent>
注:这里需要注意的是必须继承父类配置
在启动类上添加@EnableEurekaClient注解
/**
* 配置3:添加注解
*启动客户端的注解
*/
@EnableEurekaClient
@SpringBootApplication
public class DemoGoodserviceApplication {
public static void main(String[] args) {
SpringApplication.run(DemoGoodserviceApplication.class, args);
}
}
配置Eureka参数,注册到注册中心上去
#配置4:
server.port=8801 spring.appl
#配置是否注册到Eureka服务
eureka.client.register-with-eureka=true
#配置是否从Eureka 上查询服务
eureka.client.fetch-registry=true
#注册中心的url(serviceUrl不要横线) eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8808/eureka/
Eureka客户端(订单微服务)
4. 订单微服务通过Eureka上注册的服务名调用商品微服务
只需在订单微服务上修改:
@Configuration
public class RestTemplateConfig {
//负载均衡
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@Service
public class GoodsService {
@Autowired
private RestTemplate restTemplate;
public Goods findGoodsById(Long id){
//调用商品微服务 //调用本机不同服务器,(不同机器改ip地址) //String
host="http://127.0.0.1:8080/goods/";
String host="http://goods-service/goods/";
//参数(主机+查询id,返回类型)
Goods goods = restTemplate.getForObject(host + id, Goods.class); return goods;
}
}
优点:订单微服务ip的改变,只要注册到服务中心上,都能找到他
自我保护机制
失效易除
客户端默认每隔30秒向注册中心发送心跳包,注册中心默认每隔60秒搜索一遍将注册清单中超过90秒没有续约的服务剔除掉
自我保护机制
注册中心统计心跳出现大范围失败(15分钟内超过85%),会将注册清单中的信息保护起来,不剔除他们。
配置自我保护机制
只需要在注册中心配置
#关闭自我保护
eureka.server.enable-self-preservation=false
重启注册中心服务器
THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
安全配置
均在注册服务中心配置
添加依赖spring-boot-starter-sercurity
<!--配置安全管理依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加安全配置类
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
配置账号和密码
#账号
spring.security.user.name=admin
#密码
spring.security.user.password=123456
在defaultZone中添加账号密码
#注册中心的url(serviceUrl不要横线) eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
Eureka集群
高可用:也就是不只有一个注册服务中心
在注册服务中心上配置三个文件
这是共有的
Server1
注:如何启动这两个服务需要配置: