springcloud-服务提供与注册Eureka

1. eureka_server

引入pom的依赖文件


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>

dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

配置application.properties文件

server.port=8761

配置安全认证
security.basic.enabled=true
security.user.name=chuk
security.user.password=123456

配置eureka
不注册自己
eureka.client.register-with-eureka=false
不检索服务
eureka.client.fetch-registry=false
注册地址
eureka.client.service-url.defaultZone=http://chuk:123456@localhost:8761/eureka

服务端主程序

//注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

2. eureka_client

加入eureka客户端的依赖


        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>

配置properties文件

server.port=8887

spring.application.name=spring_cloud_movie

logging.level.root=info
logging.level.org.org.hibernate=info
logging.level.com.com.chukun=debug

注册中心的地址
eureka.client.service-url.defaultZone=http://chuk:123456@localhost:8761/eureka/
显示ip
eureka.instance.prefer-ip-address=true

显示服务的实例id
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${server.port}}

客户端主程序

@SpringBootApplication
//提供服务
@EnableEurekaClient
public class SpringCloudMovieApplication {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

启动注册中心,与服务的提供者,就会向注册中心注册服务

运行结果如下
springcloud-服务提供与注册Eureka_第1张图片

你可能感兴趣的:(springcloud)