Eureka微服务实战-服务提供者

1.启动主java文件 DiscoveryApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //发现注册中心的服务
public class DiscoveryApplication {

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

复制代码

2.服务提供控制器 HelloController.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class HelloController {

    private final Logger log = LoggerFactory.getLogger(HelloController.class);


    @Autowired
    private DiscoveryClient client;//eureka服务发现接口

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){

        List instances = client.getInstances("hello-server");
        for (ServiceInstance instance:instances  ) {
            log.info("/hello"+instance.getHost() + "---server---"+instance.getServiceId());
        }
        return "DiscoveryApplication";
    }

}
复制代码

3.服务提供核心配置文件

spring:
    application:
        name: hello-server

server:
    port: 9000
eureka:
    instance:
        lease-expiration-duration-in-seconds: 30 #参数用于定于服务时效时间
        lease-renewal-interval-in-seconds: 10  #用户定义与注册中心续约时间
        hostname: localhost
    client:
        service-url:
            defaultZone: http://peer1:8761/eureka/ #注册中心地址
        enabled: true
        register-with-eureka: true #向注册中心注册此服务
复制代码

4.依赖pom.xml文件

"1.0" encoding="UTF-8"?>
"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">
    4.0.0

    com.hnair
    discovery
    0.0.1-SNAPSHOT
    jar

    discovery
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                <type>pomtype>
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




复制代码

访问地址:http://localhost:9000/hello

你可能感兴趣的:(Eureka微服务实战-服务提供者)