服务注册与发现

版权声明:原创博文,转载请注明出处~ https://blog.csdn.net/She_lock/article/details/82455115

今年 Dubbo 活了,并且被 Apache 收了。同时很不幸,Spring Cloud 下的 Netflix Eureka 组件项目居然宣布闭源了。。。但是之前的版本还是可以使用的。

服务注册中心

1、pom.xml 中引入需要的依赖内容:

    <!--继承 1.5.4.RELEASE版本spring boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--服务注册中心-->
    <dependencies>  
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <!--继承 Dalston.SR1版本spring cloud-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

本人亲试,springboot 2.04springcloud Dalston.SR1 版本配合会抛错,springboot 1.5.4.RELEASEspringcloud Finchley.SR1 版本配合会抛错,出错信息为java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V。这是不是说明springboot2.04 及以上 和 springcloud Finchley.SR1 及以上版本,已经不适合使用Eureka作为服务配置中心了???

2、application.properties 添加如下配置:

spring.application.name=eureka-server
server.port=8001

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

ureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。
  • eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。
  • eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。

3、启动:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DeomSpringCloudApplication {

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

添加@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。运行main方法,访问 http://localhost:8001/ 可以看到下面的页面,没有可用实例注册到服务注册中心:

服务提供方

添加依赖:

    <!--继承 1.5.4.RELEASE版本spring boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--服务发现组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

    <!--继承 Dalston.SR1版本spring cloud-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application.properties 配置一把:

spring.application.name=eureka-client
server.port=8002
# 将服务注册到注册中心地址去
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

写一个web接口服务:

@RestController
public class HelloController {


    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/hello")
    public Map hello() {
        String services = "Services: " + discoveryClient.getServices();
        String uri = "Uri: " + discoveryClient.getLocalServiceInstance().getUri();
        String port = "Port: " + discoveryClient.getLocalServiceInstance().getPort();
        System.out.println(services);
        Map msg = new HashMap();
        msg.put("uri", uri);
        msg.put("port", port);
        msg.put("services", services);
        return msg;
    }
}

启动:

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

可以看到注册中心 http://localhost:8001/ 可以发现多了一个客户端程序:

访问http://localhost:8002/hello 可以看到以下内容:

猜你喜欢

转载自blog.csdn.net/She_lock/article/details/82455115