关于springcloud:Spring-Cloud基础之Discovery-Service服务发现简单搭建

120次阅读

共计 3389 个字符,预计需要花费 9 分钟才能阅读完成。

1. 创立 Discovery Server 服务

// 导入包,理论是通过 Spring Initializr 导入的,Eureka Server,DevTools,Actuactor
spring-boot-devtools
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-actuator
// 配置启动类注解 EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServerApplication {public static void main(String[] args) {SpringApplication.run(DiscoveryServerApplication.class, args);
    }
}
// 此时启动会报错,须要配置
spring.application.name=discovery-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761

2. 创立 Service 服务

// 导入包,理论是通过 Spring Initializr 导入的,Eureka Discovery,DevTools,Actuactor
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
spring-boot-devtools
// 配置启动类注解 EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ServiceApplication {@Value("${service.instance.name}")
    private String instance;
    public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);
    }
    @RequestMapping("/")
    public String message(){return "Hello from" + instance;}
}
// 配置文件
spring.application.name=service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

3. 别离配置两个启动服务,每个配置端口以及节点名称,第一个为 instance1 8081,第二个为 instance2 8082

// 第一个节点配置
service.instance.name= instance 1
server.port=8081
// 第二个节点配置
service.instance.name= instance 2
server.port=8082

4. 启动后,两个 servic 日志中会显示 204,示意注册胜利

2020-04-11 21:51:03.000  INFO 964 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICE/localhost:service:8081: registering service...
2020-04-11 21:51:03.072  INFO 964 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICE/localhost:service:8081 - registration status: 204

5.server 端会显示两个节点注册胜利

2020-04-11 21:51:03.070  INFO 899 --- [nio-8761-exec-5] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SERVICE/localhost:service:8081 with status UP (replication=false)
2020-04-11 21:51:03.595  INFO 899 --- [nio-8761-exec-6] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SERVICE/localhost:service:8081 with status UP (replication=true)
2020-04-11 21:52:11.887  INFO 899 --- [nio-8761-exec-3] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SERVICE/192.168.31.76:service:8082 with status UP (replication=false)
2020-04-11 21:52:12.408  INFO 899 --- [nio-8761-exec-5] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SERVICE/192.168.31.76:service:8082 with status UP (replication=true)

6. 创立 Client 服务

// 导入包,理论是通过 Spring Initializr 导入的,Eureka Discovery,DevTools,Actuactor
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
spring-boot-devtools
// 配置启动类注解 EnableDiscoveryClient,以及后去 eureka 服务中注册的 servic 地址
@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ClientApplication {
    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);
    }
    @RequestMapping("/")
    public String callService(){RestTemplate restTemplate = restTemplateBuilder.build();
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("service",false);
        String baseUrl = instanceInfo.getHomePageUrl();
        ResponseEntity<String> reponse =
                restTemplate.exchange(baseUrl,HttpMethod.GET,null,String.class);
        return reponse.getBody();}
}
// 配置文件,其中设置不被发现所以设置为 false
spring.application.name=client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=false
// 启动服务调用 http://localhost:8080/ 能够每次拜访到不同的两个 service 节点 

正文完
 0