美文网首页
eureka实现服务提供者

eureka实现服务提供者

作者: Angle_洛熙 | 来源:发表于2020-09-17 17:32 被阅读0次

服务提供

1. pom.xml添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka 客户端的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2. 添加配置

spring.application.name=provider
server.port=8000
provider.name=provider0
eureka.client.serviceUrl.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/,http://node1:8083/eureka/

3.启用注册和发现

@SpringBootApplication
//启用客户端的服务注册和发现功能
@EnableDiscoveryClient
public class EurekaDiscoveryClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaDiscoveryClientApplication.class, args);
    }

}

4. 实现服务提供者接口

@RestController
public class HelloController {
   @Value("${provider.name}")
   private String name;

   @Value("${server.port}")
   private String port;

   @RequestMapping("hello")
   public String hello() {
       return "provider: " + name + " port: " + port;
   }
}

5. 验证服务有效

  • 启动服务中心集群
  • 启动服务提供
  • 访问服务接口http://localhost:8000/hello
    可以查看到返回值,同时注册中心也可以看到该服务。

集群配置

配置多个配置文件, 如application-provider1.properties,application-provider2.properties,application-provider3.properties
然后打包启动
mvn clean package
java -jar EurekaDiscoveryClient-0.0.1-SNAPSHOT.jar --spring.profiles.active=provider1
java -jar EurekaDiscoveryClient-0.0.1-SNAPSHOT.jar --spring.profiles.active=provider2
java -jar EurekaDiscoveryClient-0.0.1-SNAPSHOT.jar --spring.profiles.active=provider3

相关文章

网友评论

      本文标题:eureka实现服务提供者

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