A, Consul installation and configuration
https://www.cnblogs.com/yangk1996/p/10742911.html
Second, the service registration
- Maven relies introduced
<parent> <groupId>com.yk</groupId> <artifactId>cloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>springcloud-consul-client</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies>
- Service Discovery
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableDiscoveryClient //注册发现 public class ConsulApplication { public static void main(String[] args) { SpringApplication.run(ConsulApplication.class, args); } }
- yml Configuration
Spring-Cloud-= spring.application.name Consul-Client the server.port = 8080 # Consul connection configuration of the server # Consul host address spring.cloud.consul.host = http://192.168.100.129 # Consul service port spring.cloud 8500 = .consul.port # HealthCheck adjustment path it passed to the server Consul, help callback, if you do not configure the service will be registered on the unsuccessful spring.cloud.consul.discovery.healthCheckPath = / check host address # this is the default service machine spring.cloud.consul.discovery.hostname = 192.168.0.102
- Test Demo
@RestController public class DiscoveryClientController { @Autowired Private DiscoveryClient discoveryClient; @Value ( "spring.application.name $ {}") Private ApplicationName String; / ** * @author: yangk * @Description: Get all of the service names * @Param : [] * @return: java.util.List <java.lang.String> * @date: 2019 Nian 06 Yue 09 Ri 10 Shi Fen 38 * / @GetMapping ( "listServices") public List <String> listServices () { return discoveryClient.getServices (); } / ** * @author: yangk * @Description: acquiring information of all service instances * @Param: @Return *: * @date: 2019 dated 10 years 06 41 minutes when 09 * / @GetMapping ( "listInstance") public List <ServiceInstance> listInstance () { List <String> = listServices Servers (); the LinkedList <ServiceInstance> = the LinkedList new new serviceInstances <> (); servers.forEach (serviceName -> { serviceInstances.addAll (discoveryClient.getInstances (serviceName)); }); return serviceInstances; } / ** * @author: yangk * @Description: Get current examples of applications * @Param: * @return: * @date: 2019 Nian 06 Yue 09 Ri 10 Shi 44 Fen * / @GetMapping("getCurrentServiceInstance") public ServiceInstance getCurrentServiceInstance() { return discoveryClient.getInstances(ApplicationName).get(0); } /** * @Author: yangk * @Description: Consul服务器的回调 * @Param: * @return: * @Date: 2019年06月09日 12时51分 */ @GetMapping("/check") public String check() { return "OK"; } }
Third, start
- Start two clients
- By ip: 8500 query Consul ui page
- View examples