2019独角兽企业重金招聘Python工程师标准>>>
创建eureka代码注入子模块
- 创建一个springcloud-integration-eureka-code的子模块pom.xml文件如下
springcloud-integration
com.edurt.si
1.0.0
4.0.0
springcloud-integration-eureka-code
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
我们首先引入了spring-cloud-starter-netflix-eureka-server依赖,这样的话我们才能使用eureka服务.
此时的父pom.xml文件中的modules会变成以下内容
springcloud-integration-eureka-code
如果没有变化的话,手动将子模块名称加入进去即可.
- 创建一个java类用于做为程序的启动入口(SpringCloudIntegrationEurekaCodeLaunch.java)
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.edurt.si.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
SpringCloudIntegrationEurekaLaunch
* Description : SpringCloudIntegrationEurekaLaunch
* Author : qianmoQ
* Version : 1.0
* Create Time : 2019-03-27 17:17
* Author Email: qianmoQ
*/
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudIntegrationEurekaCodeLaunch {
public static void main(String[] args) {
SpringApplication.run(SpringCloudIntegrationEurekaCodeLaunch.class, args);
}
}
此时该应用通过使用**@SpringBootApplication注解可以成为SpringBoot应用,使用@EnableEurekaServer**注解应用将成为Eureka Server.
- 为了方便后期其他应用接入进来我们创建一个应用的配置文件
在resources资源目录下创建一个application.properties和application-eureka.properties的配置文件,内容如下
application.properties
# 服务端口
server.port=1001
application-eureka.properties
# eureka实例配置
custom.eureka.instance.hostname=localhost
custom.eureka.client.register-with-eureka=false
custom.eureka.client.fetch-registry=false
- 创建使用自定义配置文件的配置类
我们经过查看eureka的源码文件后发现eureka server和eureka client的配置信息核心是依靠EurekaServerConfigBean和EurekaClientConfigBean这两个bean注入进来的,那么我们需要使用自定义的一些配置信息/文件的话,我们只需要重新设置这两个bean即可实现定制化eureka使用自定义配置文件.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.edurt.si.eureka.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.PropertySource;
/**
*
EurekaConfig
* Description : EurekaConfig
* Author : qianmoQ
* Version : 1.0
* Create Time : 2019-03-29 16:22
* Author Email: qianmoQ
*/
@Configuration
@PropertySource(value = "application-eureka.properties")
public class EurekaServerConfig {
@Value(value = "${custom.eureka.instance.hostname}")
private String hostname;
@Value(value = "${custom.eureka.client.register-with-eureka}")
private Boolean registerWithEureka;
@Value(value = "${custom.eureka.client.fetch-registry}")
private Boolean fetchRegistry;
@Bean
@Description(value = "使用自定义配置进行配置eureka server服务")
public EurekaServerConfigBean eurekaConfig() {
EurekaServerConfigBean config = new EurekaServerConfigBean();
return config;
}
@Bean
@Description(value = "使用自定义配置进行配置eureka client服务")
public EurekaClientConfigBean eurekaClientConfigBean() {
EurekaClientConfigBean config = new EurekaClientConfigBean();
config.setRegisterWithEureka(registerWithEureka);
config.setFetchRegistry(fetchRegistry);
return config;
}
}
修改配置文件后重启服务打开浏览器就可以看到eureka的UI页面.
当然eureka server和eureka client的配置不是简简单单的我文中写的两个配置,具体的配置类可以参考进入EurekaServerConfigBean和EurekaClientConfigBean源码文件中查看.