springboot+springcloud config

参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅。

1. springconfig server

1.1. pom.xml

<!-- 父项目以来 -->

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
1.2 启动类
@EnableConfigServer
@SpringBootApplication
public class TomatoApplication {
    public static void main(String[] args) {
        SpringApplication.run(TomatoApplication.class, args);
    }
}

1.3 配置

server: 
  port: 55590
spring:
  application:
    name: asfood-tomato
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          # 配置git仓库的地址  #访问地址: http://localhost:55590/{filename}/{env}/{branch}
          uri: https://github.com/molyjao/mlims
          # git仓库地址下的相对地址,可以配置多个,用,分割。
          #search-paths: asfoodconfig
          # git仓库的账号
          #username: #jiu_shaan@163.com
          # git仓库的密码
          #password: 

#配置之后访问git配置需要输入用户名密码
security:
  user:
    name: tomato
    password: tomato

启动工程,如果可以使用 http://localhost:55590/{文件名不带后面环境}/{环境}/{git分支}访问 ,并可以展示里面内容即可。

2. springcloud client
2.1 pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-actuator</artifactId>  
        </dependency>  
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.2 启动类

@SpringBootApplication
public class KetchupApplication {
    public static void main(String[] args) {
        SpringApplication.run(KetchupApplication.class, args);  
    } 
}

控制层:

@RestController
@RefreshScope  //刷新配置使用的注解,
public class KetchupController {
    
    //qqname为配置文件的内容的一个key,':'后面是默认值
    @Value("${qqname:defaultqqname}")  
    private String str;

    @RequestMapping("/ketchup")
    String hello() { 
        return "Hello " + str + "!";  
    }
}
2.3 配置文件:两部分bootstrap.yml和application.yml文件,由于bootstrap.yml加载最早,所以需要加载服务端配置文件内的内容需要优先加载。

bootstrap.yml

spring:
  cloud:
    config:
      name: asfood
      profile: dev
      label: master
      uri: http://localhost:55590/
      #discovery:
        enabled: true                        # 默认false,设为true表示使用注册中心中的configserver配置而不自己配置configserver的uri
        serviceId: asfood-tomato            # 指定config server在服务发现中的serviceId,默认为:configserver
      #由于服务端配置了访问需要用户名和密码,所以此处也需要配置
      username: tomato
      password: tomato

application.yml

server:
  port: 55591
  
spring: 
  application: 
    name: asfood-ketchup
  profiles:
    active: dev

#日志
logging:
  file: ./logs/ketchup.log

management:
  security: 
    enabled: false   #actuator是否需要安全保证 默认为true  不加会报错

现在启动服务端,后启动客户端,可以访问就正常了,

如果客户端启动报错:找不到所配置的读取的文件中的key,  xxx placeholder ${xxx}  这个错误就是没有找到配置文件(保证不会手误,key写的不一样),如果此时你的服务端的页面访问配置文件,不能访问到配置文件中的内容,这个需要再次百度,如果是服务端可以访问到配置文件中的内容,这个时候需要检查客户端的服务端地址等的配置,检查服务端和客户端启动类的注释,一个是server一个是client还有问题百度吧,我也初学。。。还有个好网站,stackoverflow。

自动刷新配置文件访问: 客户端ip:port/refresh

 

有需要可以参考这里(在asfood-ketchup-config-client和asfood-tomato-config-server中):  https://github.com/molyjao/mlims.git 

转载于:https://www.cnblogs.com/moly/p/7978269.html

在 Spring Boot + Spring Cloud Alibaba Seata 中配置 TCC 模式,需要进行以下步骤: 1. 引入 Seata TCC 的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.0.3.RELEASE</version> </dependency> ``` 2. 配置 Seata TCC 的相关参数: ```yaml spring: application: name: demo-service # 应用名称 seata: service: group: my_test_tx_group # 事务分组名称 vgroup-mapping.my_test_tx_group: default # 分组所在的虚拟组 enable-degrade: false # 是否开启降级模式,默认为 false use-jdk-proxy: false # 是否使用 JDK 代理,默认为 false config: type: nacos # 配置中心类型,可以是 file、nacos、apollo、zk nacos: namespace: seata # 命名空间 server-addr: localhost:8848 # Nacos 服务地址 group: SEATA_GROUP # 配置组 username: nacos # 用户名 password: nacos # 密码 file: name: file.conf # 配置文件名 registry: type: nacos # 注册中心类型,可以是 file、nacos、eureka、consul、zk nacos: server-addr: localhost:8848 # Nacos 服务地址 namespace: seata # 命名空间 group: SEATA_GROUP # 注册组 username: nacos # 用户名 password: nacos # 密码 tx-service-group: my_test_tx_group # 事务分组名称 ``` 3. 在需要使用 TCC 模式的方法上使用 @Tcc 注解: ```java @Service public class DemoServiceImpl implements DemoService { @Autowired private AccountService accountService; @Autowired private StorageService storageService; /** * TCC 模式下的分布式事务实现 */ @Override @GlobalTransactional(timeoutMills = 300000, name = "demo-service-tx") public void tccTransaction(String userId, String commodityCode, Integer count) { // 第一步:减少库存 storageService.decrease(commodityCode, count); // 第二步:扣除账户余额 accountService.decrease(userId, count); // 第三步:确认(提交) // do nothing // 第四步:取消(回滚) // do nothing } } ``` 4. 在 Seata 控制台中配置相应的 TCC 事务分组。 以上就是在 Spring Boot + Spring Cloud Alibaba Seata 中配置 TCC 模式的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值