springboot生产者项目集群利用RestTemplate的注解进行负载均衡的配置
在真实的项目中不仅要将eureka等进行集群配置,有时也需要对项目进行集群配置,这样能够避免访问都集中到一个服务器上,避免服务器宕机后服务瘫痪。
新建生产者项目
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--引入自己的公共api-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--eureka-client-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-zipkin</artifactId>-->
<!-- </dependency>-->
</dependencies>
</project>
yml文件
#端口号
server:
port: 8001
#数据库链接设置
spring:
application:
# 应用名称
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: zhangxian11
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#集群版
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#单机版
#defaultZone: http://localhost:7001/eureka/
instance:
instance-id: payment8001
prefer-ip-address: true #访问路径可以显示ip
#Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒)
lease-renewal-interval-in-seconds: 1
#Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务
lease-expiration-duration-in-seconds: 2
DAO层
@Mapper
@Component
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
public List<Payment> getAll();
}
service层
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById( Long id);
public List<Payment> getAll();
}
serviceImpl层
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
System.out.println("sssssss");
return paymentDao.getPaymentById(id);
}
@Override
public List<Payment> getAll() {
return paymentDao.getAll();
}
}
controller层
@RestController
@Slf4j
public class PaymentController {
@Autowired
PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Autowired
private DiscoveryClient discoveryClient;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result=paymentService.create(payment);
CommonResult commonResult=new CommonResult();
if(result>0) {
commonResult.setCode(200);
commonResult.setMessage("插入数据成功,serverPort"+serverPort);
commonResult.setData(result);
}else {
commonResult.setCode(444);
commonResult.setMessage("插入失败");
commonResult.setData(null);
}
return commonResult;
}
@GetMapping(value ="/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
CommonResult commonResult=new CommonResult();
Payment paymentById = paymentService.getPaymentById(id);
if(paymentById!=null){
commonResult.setCode(200);
commonResult.setMessage("查询成功serverPort"+serverPort);
commonResult.setData(paymentById);
}else {
commonResult.setCode(444);
commonResult.setMessage("查询失败");
commonResult.setData(null);
}
return commonResult;
}
@GetMapping("/payment/getAll")
public CommonResult<Payment> getAll(){
CommonResult commonResult=new CommonResult();
List<Payment> list1=paymentService.getAll();
if(list1.isEmpty()){
commonResult.setCode(444);
commonResult.setMessage("查询失败");
commonResult.setData(list1);
}else {
commonResult.setCode(200);
commonResult.setMessage("查询成功serverPort"+serverPort);
commonResult.setData(list1);
}
return commonResult;
}
//服务发现Discovery对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
@GetMapping(value = "/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element:services ) {
log.info("***"+element);
}
final List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance:instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri()+"\t");
}
return this.discoveryClient;
}
}
}
启动类
服务发现需要添加@EnableDiscoveryClient才能够实现
@SpringBootApplication
@EnableEurekaClient
//服务发现
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
<!-- 如果插入成功返回主键 keyProperty 为正数,否则失败返回非正数 -->
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values (#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"></id>
<result column="serial" property="serial" jdbcType="VARCHAR"></result>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id = #{id};
</select>
<select id="getAll" parameterType="com.atguigu.springcloud.entities.Payment" resultMap="BaseResultMap">
select * from payment
</select>
</mapper>
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
返回值类型接收实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T>{
private Integer code;
private String message;
private T data;
public CommonResult (Integer code,String message){
this(code,message,null);
}
}
再新建一个与上述项目一样的项目,只需要将端口号改为8002即可。
消费者项目建设
controller类不再是写死的ip应该使用生产者在eureka中的名称作为查询路径
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
private static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
@RequestMapping("/consumer/payment/create")
public CommonResult<Payment> create(@RequestBody Payment payment){
//System.out.println(restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class));
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@RequestMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
@GetMapping("/consumer/payment/getAll")
public CommonResult<Payment> getAll(){
return restTemplate.getForObject(PAYMENT_URL+"/payment/getAll",CommonResult.class);
}
}
为了能够实现负载均衡需要在ApplicationContextConfig类中添加注解@LoadBalanced
@Configuration
public class ApplicationContextConfig {
@Bean
//使用轮询的负载均衡 @LoadBalanced
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
启动项目即可看到消费者采用轮询的方式访问项目。