美文网首页
Spring Cloud Eureka

Spring Cloud Eureka

作者: LeonardoEzio | 来源:发表于2018-06-07 18:45 被阅读0次

一、新建Spring Boot项目作为 Eureka的服务注册中心

Idea.jpg

1.pom文件

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

2.application.propertiest配置

 //服务端口号
 server.port=8083
 //应用名称
 spring.application.name=erueka_consumer
  //实例名称
 eureka.instance.appname=erueka_consumer
 //实例主机名
 eureka.instance.hostname=localhost
 //是否向服务注册中心注册自己,注册中心不用注册自己
 eureka.client.registerWithEureka=false
//是否检索服务
 eureka.client.fetchRegistry=false
//服务注册中心位置
 eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.porty}/eureka/

3启动类

@SpringBootApplication
@EnableEurekaServer
public class CenterApplication {

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

二、在项目中新建Module 作为服务提供方

1.pom文件

  继承服务注册中心配置即可
  <parent>
    <groupId>cn.tempus.erueka</groupId>
    <artifactId>center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

2.application.propertiest配置

    server.port=8082
    spring.application.name=erueka-provider

    eureka.instance.appname=erueka-provider
    eureka.instance.hostname=localhost
    eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/

3.启动类

    @SpringBootApplication
    @EnableEurekaClient
    public class ProviderApplication {

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

4.提供服务

  @RestController
  public class TestServiceController {

      @RequestMapping(value = "hello/{name}")
      public String index(@PathVariable("name") String name) {
          return "Hello "+name + "  how are you?";
      }
  }

三、在项目中新建Module 作为服务调用方

1.pom文件

  1)继承服务注册中心配置
  2)<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

2.application.propertiest配置

  server.port=8083
  spring.application.name=erueka_consumer

  eureka.instance.appname=erueka_consumer
  eureka.instance.hostname=localhost
  eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/

3.启动类

  @SpringBootApplication
  @EnableDiscoveryClient
  @EnableFeignClients
  public class ConsumerApplication {

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

4.新建Interface获取远程服务实例

  @Component
  @FeignClient("erueka-provider")//指向Provider里面配置的spring.application.name
  public interface SayHelloService {

      //注意请求路径,和方法名及参数要与对应的提供者一致
      @RequestMapping(value = "hello/{name}")
      public String index(@PathVariable("name") String name);

  }

5.在需要调用服务的地方注入实例

  @RestController
  public class TestController {

      @Autowired
      SayHelloService helloService;

      @RequestMapping("/test/{name}")
      public String hi(@PathVariable("name") String name){
          return helloService.index(name);
      }
  }

四、测试

1.启动服务注册中心

服务注册中心.png

2.启动服务提供方|调用方

服务提供方启动后.png

服务调用方启动后界面跟这一样

3.调用服务

调用服务.png

五、备注

1.项目结构图

Spring Cloud Eureka项目结构.png

2.注意事项

spring.application.name 的值中不能用中划线
报 java.lang.IllegalStateException: Service id not legal hostname 被坑了好久

相关文章

网友评论

      本文标题:Spring Cloud Eureka

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