上一篇讲述了consul的安装及简单配置,这一次来说一下springcloud中如何使用consul对服务进行管理
生产者:boot版本是2.0.3
pom文件引入consul依赖
<!-- consul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
在application文件引入consul支持
spring.cloud.consul.host=localhost 本机,推荐每个机器都部署一个consul监测发现服务
spring.cloud.consul.port=8500 consul端口
spring.cloud.consul.enabled=true 是否启用consul服务
spring.cloud.consul.discovery.enabled=true 是否可以被发现
spring.cloud.consul.discovery.serviceName=KafkaService 注册的服务名
spring.cloud.consul.discovery.prefer-ip-address=true 是否允许IP显示
spring.cloud.consul.discovery.heartbeat.enabled=true 是否允许心跳检测
spring.cloud.consul.discovery.instance-id=KafkaService-${random.value} 给应用附上唯一标识,consul集群时推荐
在主程序中加入服务发现支持
//cloud支持
@EnableDiscoveryClient
//事务支持
@EnableTransactionManagement
@ComponentScan("com.kafka.KafkaBase")
@SpringBootApplication
public class KafkaBaseApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(KafkaBaseApplication.class, args);
}
@Bean
public FilterRegistrationBean httpFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new HttpFilter());
return filterRegistrationBean;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/**");
}
}
在Controller写入测试接口
@RequestMapping(value = "/param",method = RequestMethod.GET)
public String param(HttpServletRequest request, HttpServletResponse response){
String param = request.getParameter("param");
return "服务端-测试参数"+param;
}
消费者:
消费者这里我们引入了feign组件,他可以进行负载均衡,帮助用户把请求平均的分发到各个生产者那里。
pom文件引入consul依赖及feign组件
<!-- consul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在application文件引入consul支持
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.serviceName=ConsulConsumer
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.heartbeat.enabled=true
spring.cloud.consul.discovery.register=false
在主程序中加入服务发现支持
//cloud支持
@EnableDiscoveryClient
//事务支持
@EnableTransactionManagement
@SpringBootApplication
@EnableFeignClients
public class KafkaBaseApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(KafkaBaseApplication.class, args);
}
@Bean
public FilterRegistrationBean httpFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new HttpFilter());;
return filterRegistrationBean;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/**");
}
}
在Controller写入测试接口
@ResponseBody
@RequestMapping(value = "/clientParam", method = RequestMethod.GET)
public String paramname(@RequestParam(value = "param") Integer param) {
return loadBalancer.choose("KafkaService").getUri().toString()+feinClient.paramname(param);
}
至此,一个简单的基于consul的服务发现及调用的例子就写完了。