springboot 2.1.0.RELEASE 遇坑小结

1.版本问题

       目前不支持1.*版本与2.1.0版本一起使用。

       例:项目A是1.5.7 版本,搭建的cloud服务B是用的2.1.0,那么A无法直接调用B的client服务,如需调用,切换版本或直接调用http接口。

2.配置改动

       context-path前面追加servlet,healthCheckPath相应调整并在health钱加入actuator

server:
  port: 14040
  servlet:
    context-path: /test
spring:
  application:
    name: test-service
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    type: com.alibaba.druid.pool.DruidDataSource
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        enabled: true
        instanceId: ${spring.application.name}:${spring.cloud.client.ipAddress:${random.value}}
        serviceName: ${spring.application.name}
        preferIpAddress: true
        port: ${server.port}
        healthCheckPath: ${server.servlet.context-path}/actuator/health
        tags: 

3.@FeignClient(value=TestConstant.SERVICE_NAME,path=TestConstant.SERVICE_PATH)

不支持多个client,存放多个client,调用方依赖之后,启动异常。

异常信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'test-service.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

4.由于cloud服务的注册机制,为防止cloud服务因为多次启动,出现过多service列表,可以增加下面代码:

@Component
@Primary
public class CustomizedConsulDiscoveryProperties extends ConsulDiscoveryProperties {

	private Logger logger = LoggerFactory.getLogger(CustomizedConsulDiscoveryProperties.class);
	public CustomizedConsulDiscoveryProperties(InetUtils inetUtils) {
		super(inetUtils);
	}

	@Override
	public String getInstanceId() {
		logger.info("CustomizedConsulDiscoveryProperties>>>>>>>>>>"+this.getIpAddress());
		return this.getServiceName()+"-"+this.getIpAddress();
	}

}

通过当前serviceName与服务器ip生成固定的服务,随意生成服务,同一台服务器,多次启动,会出现多个。

5.通过Tomcat启动不会注册consul服务问题:

spring cloud 2.0 consul 注册是通过监听WebServerInitializedEvent事件,通过tomcat容器启动服务时监听不到WebServerInitializedEvent事件,Consul无法注册服务,可以加入下面代码:

@Component
public class CustomizedEventLinsiners implements ApplicationListener<ContextRefreshedEvent>{
	@Autowired
	ConsulAutoServiceRegistration consulAutoServiceRegistration;
	@Autowired
	ConsulDiscoveryProperties properties;
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		consulAutoServiceRegistration.start();
	}

}

6.自动注入prefix不要加入下划线、不要使用static  进行set(1.5.7支持,但是规范问题,还是不要使用)。

猜你喜欢

转载自blog.csdn.net/qq_24842293/article/details/84578860