给我写一段springboot+双Elasticsearch数据源 配置文件以及实体类的代码
时间: 2023-06-03 20:07:15 浏览: 98
好的,下面是代码:
1. 配置文件
```
# datasource1
spring.datasource.ds1.url=jdbc:mysql://localhost:3306/db1?useSSL=false
spring.datasource.ds1.username=root
spring.datasource.ds1.password=123456
spring.datasource.ds1.driverClassName=com.mysql.cj.jdbc.Driver
# datasource2
spring.datasource.ds2.url=jdbc:mysql://localhost:3306/db2?useSSL=false
spring.datasource.ds2.username=root
spring.datasource.ds2.password=123456
spring.datasource.ds2.driverClassName=com.mysql.cj.jdbc.Driver
# Elasticsearch
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.username=username
spring.elasticsearch.rest.password=password
spring.elasticsearch.rest.connection-timeout=10000
spring.elasticsearch.rest.read-timeout=10000
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.properties.request.headers.Authorization=Bearer your_auth_token
# use Elasticsearch as the default data source
spring.data.jpa.repository.support.JpaRepositoryFactoryBean.DEFAULT_ENTITY_MANAGER_FACTORY_REF=esEntityManagerFactory
```
2. 实体类
```
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(indexes = {
@Index(name = "idx_name", columnList = "name"),
@Index(name = "idx_description", columnList = "description")
})
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@Column(name="create_time")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private LocalDateTime createTime;
}
```
注意,以上代码只是示例,具体代码需要根据实际情况进行调整。另外,要使用双数据源配置,还需要在代码中进行相关配置。