如何properties文件中参数之间相互调用和通过properties文件来设置随机值?

跟杨春娟学SpringBoot笔记:如何properties文件中参数之间相互调用和通过properties文件来设置随机值?

完成:第一遍

1.如何properties文件中参数之间相互调用和通过properties文件来设置随机值?

步骤一:在config.properties中设置参数间的引用

config.properties

myboot.aws.key=23o48uworow3uro99w3ur3wurow3u
myboot.aws.region=cn-north-1
myboot.aws.bucketName=cherry
myboot.aws.show=${myboot.aws.region}--${myboot.aws.bucketName}

#random
myboot.random.secret=${random.value}
//分别设置int、long、uuid等类型的随机数
myboot.random.number=${random.int}
myboot.random.bigNumber=${random.long}
myboot.random.uuid=${random.uuid}
myboot.random.ten=${random.int(10)}
myboot.random.range=${random.int[1024,65536]}

步骤二:修改AWSConfigBean

AWSConfigBean

package com.springboot.demo.SpringBootDemoProject.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="myboot.aws")
public class AWSConfigBean {
	private String key;
	private String region;
	private String bucketName;
	private String show;
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getRegion() {
		return region;
	}
	public void setRegion(String region) {
		this.region = region;
	}
	public String getBucketName() {
		return bucketName;
	}
	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}
	public String getShow() {
		return show;
	}
	public void setShow(String show) {
		this.show = show;
	}
	
	
}

步骤三:新建RandomConfigBean.java

RandomConfigBean.java

package com.springboot.demo.SpringBootDemoProject.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="myboot.random")
public class RandomConfigBean {
	private String secret;
	private int number;
	private long bigNumber;
	private String uuid;
	private int ten;
	private int range;
	public String getSecret() {
		return secret;
	}
	public void setSecret(String secret) {
		this.secret = secret;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public long getBigNumber() {
		return bigNumber;
	}
	public void setBigNumber(long bigNumber) {
		this.bigNumber = bigNumber;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public int getTen() {
		return ten;
	}
	public void setTen(int ten) {
		this.ten = ten;
	}
	public int getRange() {
		return range;
	}
	public void setRange(int range) {
		this.range = range;
	}


	
	
}

步骤三:修改config.html

config.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>This is a Configuration Test page</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	This is Configuration Test page!
	<p th:text="${awsKey}"></p>
	<p th:text="${awsRegion}"></p>
	<p th:text="${awsBucketName}"></p>
	
	<p th:text="${configBean.key}"></p>
	<p th:text="${configBean.region}"></p>
	<p th:text="${configBean.bucketName}"></p>
	<p th:text="${'this is show'+configBean.show}"></p>
	
	<p>Following is random value</p>
	<p th:text="${'secret:'+randomConfigBean.secret}"></p>
	<p th:text="${'number:'+randomConfigBean.number}"></p>
	<p th:text="${'bigNumber:'+randomConfigBean.bigNumber}"></p>
	<p th:text="${'uuid:'+randomConfigBean.uuid}"></p>
	<p th:text="${'ten'+randomConfigBean.ten}"></p>
	<p th:text="${'range'+randomConfigBean.range}"></p>
</body>
</html>

步骤四:修改config.html
model.addAttribute(“randomConfigBean”, randomConfigBean);

package com.springboot.demo.SpringBootDemoProject.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("c")
public class ConfigurationPageController {

	@Value("${myboot.aws.key}")
	private String awsKey;
	
	@Value("${myboot.aws.region}")
	private String awsRegion;
	
	@Value("${myboot.aws.bucketName}")
	private String awsBucketName;
	
	@Autowired
	private AWSConfigBean aWSConfigBean;
	
	@Autowired
	private RandomConfigBean randomConfigBean;
	
	@RequestMapping("value")
	public String show(ModelMap model) {
		model.addAttribute("awsKey", awsKey);
		model.addAttribute("awsRegion", awsRegion);
		model.addAttribute("awsBucketName", awsBucketName);
		model.addAttribute("configBean", aWSConfigBean);
		model.addAttribute("randomConfigBean", randomConfigBean);
		return "config";
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenFeign是一个服务间通信框架,它简化了服务间的调用,同时集成了Ribbon,可以轻松实现客户端负载均衡。在OpenFeign配置Ribbon非常简单,在FeignClient的配置类添加如下代码即可: ```java @Configuration public class FeignConfig { @Bean public IRule ribbonRule() { return new RandomRule(); //这里配置策略,例如随机选择负载均衡策略 } } ``` 上述代码,使用@Bean注解生成了一个IRule对象实例,用于指定Ribbon的负载均衡策略。在示例,我们使用了随机选择策略。实际开发按需选择合适的负载均衡策略。当然,这里的配置也可以在application.yml(或者application.properties文件进行配置,如下: ```yaml my-service: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule ``` 在上述代码,我们配置了my-service服务使用随机选择策略。 需要注意的是,在使用OpenFeign框架时,我们也要引入相应的依赖和配置文件,以使得整个流程运行时能够顺畅地运转。在Maven项目,我们可以添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 在配置文件,我们需要配置服务的名称: ```yaml spring: application: name: your-service-name ``` 以上就是OpenFeign在使用Ribbon进行负载均衡时的配置方法。这里只给出了最基本最简单的配置方式,在实际的开发过程需要根据具体业务需求进行更加灵活的配置,以达到最优的服务通信效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值