美文网首页
SpringCloud微服务架构——服务注册与发现

SpringCloud微服务架构——服务注册与发现

作者: 晏子小七 | 来源:发表于2019-10-08 10:14 被阅读0次

    Eureka用于实现服务治理。
    1.创建『服务注册中心』
    首先创建springboot工程,命名eureka-server,并在pom.xml中引入依赖内容:
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
    </parent>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    </dependencies>
    <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>

    引入pom内容之后通过在启动类中添加注解@EnableEurekaServer启动一个服务中心提供给其他应用进行对话:

    @EnableEurekaServer

    @SpringBootApplication

    public class Application{

    public static void main(String[] args) {
    

    new SpringApplicationBuilder(Application.class)

    .web(true).run(args);

    }
    }

    application.properties配置如下:

    spring.application.name=eureka-server

    server.port=1001

    eureka.instance.hostname=localhost

    eureka.client.register-with-eureka=false

    eureka.client.fetch-registry=false

    启动工程后,访问:http://localhost:1001/

    2.创建服务提供方:
    下面我们创建服务得客户端,并向服务注册中心注册自己;
    首先创建一个springboot应用,命名为eureka-client,引入pom文件
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
    </parent>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
    <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>

    其次,实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。

    @RestController

    public class DcController {

    @Autowired

    DiscoveryClient discoveryClient;

    @GetMapping("/dc")

    public String dc() {

    String services = "Services: " + discoveryClient.getServices();

    System.out.println(services);

    return services;

    }
    }

    最后在应用主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

    @EnableDiscoveryClient

    @SpringBootApplication

    public class Application {

    public static void main(String[] args) {

    new SpringApplicationBuilder(

    ComputeServiceApplication.class)

    .web(true).run(args);

    }

    }

    我们在完成了服务内容的实现之后,再继续对application.properties做一些配置工作,具体如下:

    spring.application.name=eureka-client

    server.port=2001

    eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

    通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。

    启动该工程后,再次访问:http://localhost:1001/。可以如下图内容,我们定义的服务被成功注册了。

    image

    当然,我们也可以通过直接访问eureka-client服务提供的/dc接口来获取当前的服务清单,只需要访问:http://localhost:2001/dc,我们可以得到如下输出返回:Services: [eureka-client]

    其中,方括号中的eureka-client就是通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中获取到的所有服务清单。由于Spring Cloud在服务发现这一层做了非常好的抽象,所以,对于上面的程序,我们可以无缝的从eureka的服务治理体系切换到consul的服务治理体系中区。

    相关文章

      网友评论

          本文标题:SpringCloud微服务架构——服务注册与发现

          本文链接:https://www.haomeiwen.com/subject/hqnvpctx.html