Spring Cloud Config 使用本地配置文件

一、简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。 二、配置 2.1 Spring Cloud Config Server项目 1 pom.xml中导入Config Server需要的包 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> 2 在Application类中添加@EnableConfigServer注解 package com.sunbufu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 3 修改配置文件application.yml,指定本地客户端配置文件的路径 spring: profiles: active: native cloud: config: server: native: searchLocations: F:/conf 4 准备客户端配置文件 client-dev.yml文件的内容: server: #设置成0,表示任意未被占用的端口 port: 8081 nickName: world 2....

June 13, 2018 · 1 min · sunbufu

Spring Cloud 保证微服务内安全

一、简介 在微服务的架构下,我们需要把系统的业务划分成多个单一的微服务。每个微服务都会提供接口供其他微服务调用,在Dubbo中可以通过rmi、nio等实现,Spring Cloud中是通过http调用的。但有些时候,我们只希望用户通过我们的网关调用微服务,不允许用户直接请求微服务。这时我们就可以借助Spring Security来保障安全。 二、使用步骤 2.1 在提供接口的微服务项目中配置Spring Security 1 首先在pom.xml引入Spring Security的相关配置,如下 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 2 在qpplication.yml中配置账号密码,如下 security: basic: enabled: true user: name: sunbufu password: 123456 3 此时访问接口发现已经需要认证了。 输入正确的账号和密码后就可以访问了。 2.2在调用微服务项目中配置Feign的账号密码 1 在application.yml中配置账号密码 security: user: name: sunbufu password: 123456 2 添加Feign的配置文件 package com.sunbufu.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.auth.BasicAuthRequestInterceptor; @Configuration public class FeignConfiguration { @Value("${security.user.name}") private String userName; @Value("${security.user.password}") private String passWord; @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){ return new BasicAuthRequestInterceptor(userName, passWord); } } 3 这样完成后,就可以正常的访问了。 三、实例 源码地址...

June 13, 2018 · 1 min · sunbufu

Spring StateMachine介绍

一、状态机 有限状态机是一种用来进行对象行为建模的工具,其作用主要是描述对象在它的生命周期内所经历的状态序列,以及如何响应来自外界的各种事件。在电商场景(订单、物流、售后)、社交(IM消息投递)、分布式集群管理(分布式计算平台任务编排)等场景都有大规模的使用。 状态机的要素: 状态机可归纳为4个要素,现态、条件、动作、次态。“现态”和“条件”是因,“动作”和“次态”是果。 现态:指当前所处的状态 条件:又称“事件”,当一个条件被满足,将会触发一个动作,或者执行一次状态的迁移 动作:条件满足后执行的动作。动作执行完毕后,可以迁移到新的状态,也可以仍旧保持原状态。动作不是必须的,当条件满足后,也可以不执行任何动作,直接迁移到新的状态。 次态:条件满足后要迁往的新状态。“次态”是相对于“现态”而言的,“次态”一旦被激活,就转换成“现态”。 状态机动作类型: 进入动作:在进入状态时进行 退出动作:在退出状态时进行 输入动作:依赖于当前状态和输入条件进行 转移动作:在进行特定转移时进行 二、spring statemachine spring statemachine是使用 Spring框架下的状态机概念创建的一种应用程序开发框架。它使得状态机结构层次化,简化了配置状态机的过程。 官方文档:https://docs.spring.io/autorepo/docs/spring-statemachine/1.0.0.M3/reference/htmlsingle/#sm-statecontext 例子一:简单订单状态 源码已经上传到github。 如下所示: 1 引入依赖 <!--spring statemachine--> <dependency> <groupId>org.springframework.statemachine</groupId> <artifactId>spring-statemachine-core</artifactId> <version>2.0.1.RELEASE</version> </dependency> 2 创建订单状态枚举类和状态转换枚举类 /** * 订单状态 * * @author sunbufu */ public enum OrderState { /** 待支付 */ WAIT_PAYMENT, /** 待发货 */ WAIT_DELIVER, /** 待收货 */ WAIT_RECEIVE, /** 完结 */ FINISH; } /** * 订单事件 * * @author sunbufu */ public enum OrderEvent { /** 支付 */ PAYED, /** 发货 */ DELIVERY, /** 收货 */ RECEIVED; } 3 添加配置 /** * 订单状态机配置 * * @author sunbufu */ @Configuration @EnableStateMachine(name = "orderStateMachine") public class OrderStateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> { @Override public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception { states....

June 13, 2018 · 3 min · sunbufu