Spring boot 将Hazelcast缓存添加到配置为Eureka客户端的spring boot应用程序
我正在试用带有REST web服务的spring boot应用程序。我计划使用同一应用程序的多个实例,因此将它们注册为Eureka客户端。我用Zuul进行负载平衡。这个很好用。现在,当我尝试将Hezelcast添加到应用程序实例时,应用程序出现了,但hazelcast实例没有。我不明白为什么它不会。感谢您的帮助 这是我的配置类Spring boot 将Hazelcast缓存添加到配置为Eureka客户端的spring boot应用程序,spring-boot,hazelcast,netflix-eureka,Spring Boot,Hazelcast,Netflix Eureka,我正在试用带有REST web服务的spring boot应用程序。我计划使用同一应用程序的多个实例,因此将它们注册为Eureka客户端。我用Zuul进行负载平衡。这个很好用。现在,当我尝试将Hezelcast添加到应用程序实例时,应用程序出现了,但hazelcast实例没有。我不明白为什么它不会。感谢您的帮助 这是我的配置类 @Configuration public class CacheConfig { @Bean public Config config() {
@Configuration
public class CacheConfig {
@Bean
public Config config() {
return new Config();
}
}
这些是我的pom依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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-eureka</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
这是我的应用程序。为了方便起见,我把这三门课都放在同一块
@RestController
@RequestMapping("/sample-data")
public class POCApiController {
@Autowired
SampleService sampleService;
@RequestMapping(value="/function1/param1/{param1}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getData(@PathVariable("param1") String param1){
System.out.println("Controller called...");
String results = sampleService.getData(param1);
return new ResponseEntity<String>(results, HttpStatus.OK);
}
}
public interface SampleService {
public String getData(final String param);
}
@Service
@CacheConfig(cacheNames = "sampleCachedData")
public class SampleServiceImpl implements SampleService {
@Override
@Cacheable
public String getData(String param) {
System.out.println("Service called... ");
return "greetings"+param;
}
}
@RestController
@请求映射(“/sample data”)
公共类POCApiController{
@自动连线
样本服务样本服务;
@RequestMapping(value=“/function1/param1/{param1}”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值)
公共响应性getData(@PathVariable(“param1”)字符串param1){
System.out.println(“控制器调用…”);
字符串结果=sampleService.getData(参数1);
返回新的响应状态(结果,HttpStatus.OK);
}
}
公共接口示例服务{
公共字符串getData(最终字符串参数);
}
@服务
@CacheConfig(cacheNames=“sampleCachedData”)
公共类SampleServiceImpl实现SampleService{
@凌驾
@可缓存
公共字符串getData(字符串参数){
System.out.println(“服务调用…”);
返回“问候”+参数;
}
}
请您分享一些日志或示例应用程序,以便我们进一步调查。问题:您希望Hazelcast成员如何发现彼此?因为从上面的配置中,成员将使用多播来发现彼此。感谢您的回复。我已经更新了问题中的日志。我打算使用多播。但在此之前,我们应该看到hazelcast的一些活动开始了,对吗?我甚至没有看到,我尝试了同样的方法,在添加Config
bean之后,我可以看到Hazelcast实例启动了。请确保CacheConfig
与您的主类处于相同的包层次结构下。另外,为了更好地理解这个问题,可以添加一个hazelcastance
bean而不是Config
bean。然后我们需要看到一个实例被启动。如果没有,那么您的配置类不会被自动检测。请尝试HazelcastInstance bean。没有什么变化。是的,config和main类在同一个包(com.blah.poc_rest)上。我已经更新了关于主类外观的帖子,这意味着Spring根本没有创建HazelcastInstance
bean。您可以通过在Bean定义中添加一些日志来确认这一点吗?
@EnableEurekaClient
@SpringBootApplication(scanBasePackages= {"com.blah.poc_rest"})
@EnableConfigurationProperties
@EnableCaching
public class POCRESTApp
{
public static void main( String[] args )
{
SpringApplication.run(POCRESTApp.class, args);
}
}
@RestController
@RequestMapping("/sample-data")
public class POCApiController {
@Autowired
SampleService sampleService;
@RequestMapping(value="/function1/param1/{param1}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getData(@PathVariable("param1") String param1){
System.out.println("Controller called...");
String results = sampleService.getData(param1);
return new ResponseEntity<String>(results, HttpStatus.OK);
}
}
public interface SampleService {
public String getData(final String param);
}
@Service
@CacheConfig(cacheNames = "sampleCachedData")
public class SampleServiceImpl implements SampleService {
@Override
@Cacheable
public String getData(String param) {
System.out.println("Service called... ");
return "greetings"+param;
}
}