当前位置: 代码迷 >> 综合 >> SpringCloud 之 Consul 注册中心
  详细解决方案

SpringCloud 之 Consul 注册中心

热度:93   发布时间:2023-12-13 21:12:03.0

一.Consul 注册中心安装

1.consul怎么在windows下安装

  https://www.consul.io/downloads.html 

  根据自己系统选择版本

2.启动consul 注册中心

   consul agent -dev

3.注册中心界面

http://localhost:8500/

二.Consul 服务注册和服务消费

1.服务注册

yml 配置

spring:cloud:consul:host: localhostport: 8500discovery:service-name: consul-miyaheartbeat:enabled: trueapplication:name: consul-miya
server:port: 8507pom 文件:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
@SpringBootApplication
@EnableDiscoveryClient    //注册中心
@RestController
public class SpringCloudConsulApplication {public static void main(String[] args) {
;new SpringApplicationBuilder(SpringCloudConsulApplication.class).run(args);}@RequestMapping("/hi")public String home(){return "hi ,i'm miya";}}

注册服务信息如下

2.服务消费

server:port: 8093
spring:application:name: consul-cloud-consumercloud:consul:host: localhostport: 8500discovery:register: falsepom文件:<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>
</dependencies>
@FeignClient(value = "consul-miya")
public interface IHiService {@RequestMapping(value = "/hi",method = RequestMethod.GET)String call();
}@RestController
public class HiController {@Autowiredprivate LoadBalancerClient loadBalancer;@Autowiredprivate DiscoveryClient discoveryClient;@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate IHiService hiService;/*** 获取所有服务*/@RequestMapping("/services")public Object services() {return discoveryClient.getInstances("consul-miya");}/*** 从所有服务中选择一个服务(轮询)*/@RequestMapping("/discover")public Object discover() {return loadBalancer.choose("consul-miya").getUri().toString();}@RequestMapping("/call")public String call(){ServiceInstance serviceInstance = loadBalancer.choose("consul-miya");String call = new RestTemplate().getForObject(serviceInstance.getUri().toString()+"/hi",String.class);return call;}/**** fegin 调用服务* @return*/@RequestMapping("/callable")public String callable(){return hiService.call();}
}

调用成功

  相关解决方案