SpringCloud第二弹-euerka注册中心(代码注入)

创建eureka代码注入子模块

  • 创建一个springcloud-integration-eureka-code的子模块pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud-integration</artifactId>
        <groupId>com.edurt.si</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-integration-eureka-code</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

我们首先引入了spring-cloud-starter-netflix-eureka-server依赖,这样的话我们才能使用eureka服务.

此时的父pom.xml文件中的modules会变成以下内容

<modules>
    <module>springcloud-integration-eureka-code</module>
</modules>

如果没有变化的话,手动将子模块名称加入进去即可.

  • 创建一个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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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;

/**
 * <p> SpringCloudIntegrationEurekaLaunch </p>
 * <p> Description : SpringCloudIntegrationEurekaLaunch </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-03-27 17:17 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudIntegrationEurekaCodeLaunch {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudIntegrationEurekaCodeLaunch.class, args);
    }

}

此时该应用通过使用**@SpringBootApplication注解可以成为SpringBoot应用,使用@EnableEurekaServer**注解应用将成为Eureka Server.

  • 为了方便后期其他应用接入进来我们创建一个应用的配置文件

在resources资源目录下创建一个application.propertiesapplication-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的配置信息核心是依靠EurekaServerConfigBeanEurekaClientConfigBean这两个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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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;

/**
 * <p> EurekaConfig </p>
 * <p> Description : EurekaConfig </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-03-29 16:22 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@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的配置不是简简单单的我文中写的两个配置,具体的配置类可以参考进入EurekaServerConfigBeanEurekaClientConfigBean源码文件中查看.

转载于:https://my.oschina.net/shicheng2014/blog/3029798

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值