SpringCloud Consul注册中心实现服务之间的调用

一、下载consul:https://www.consul.io/downloads.html
解压–>修改环境变量path:consul.exe目录
打开CMD:consul agent -dev命令启动consulSpringCloud Consul注册中心实现服务之间的调用_第1张图片
在浏览器中输入:http://localhost:8500/ui 看到consul界面,ui可以不加,默认有

二、代码部分
1.项目结构如下图:
SpringCloud Consul注册中心实现服务之间的调用_第2张图片
2.pom.xml 两个项目使用相同依赖

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
	
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.M7
				pom
				import
			
		
	
	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-consul-discovery
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		

	
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/libs-milestone
			
				false
			
		
	

SpringCloud2.0-Consul-Member

1.application.yml
###服务启动端口号
server:
  port: 8510
###服务名称(服务注册到consul名称)  
spring:
    application:
        name: consul-member
###consul注册中心地址
    cloud:
      consul:
        host: localhost
        port: 8500
2.MemberApp类:
package com.youke.api.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

//@EnableDiscoveryClient将当前服务注册到consul上
@SpringBootApplication
@EnableDiscoveryClient
public class MemberApp {
	public static void main(String[] args) {
		SpringApplication.run(MemberApp.class, args);
	}
}
3.MemberApiController类
package com.youke.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberApiController {
	@Value("${server.port}")
	private String serverPort;
	@RequestMapping("getMember")
	public String getMember() {
		return "我是会员服务,port:" + serverPort;
	}
}

SpringCloud2.0-Consul-Order

1.application.yml
###服务启动端口号
server:
  port: 8501
###服务名称(服务注册到consul名称)  
spring:
    application:
        name: consul-order
###consul注册中心地址
    cloud:
      consul:
        host: localhost
        port: 8500
2.OrderApp 类
package com.youke.api.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class OrderApp {
	public static void main(String[] args) {
		SpringApplication.run(OrderApp.class, args);
	}

	// 解决RestTemplate 找不到原因 应该把restTemplate注册到SpringBoot容器中@bean
	// 如果使用rest方式以别名方式进行调用必须依赖ribbon负载均衡
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}
3.OrderController 类
package com.youke.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private DiscoveryClient discoveryClient;
	@Value("${server.port}")
	private String serverPort;

	@RequestMapping("getOrder")
	public String getOrder() {
		
		List list = discoveryClient.getInstances("consul-member");
		if (list != null && !list.isEmpty()) {
			String serviceId = list.get(0).getServiceId();
			String url = "http://" + serviceId + "/getMember";
			/*
			 * getForObject Eureka中使用
			 * consul中使用getForEntity
			 * rest底层采用httpclient技术实现rpc远程调用
			 */
			//String result = restTemplate.getForObject(url, String.class);
			String result = restTemplate.getForEntity(url, String.class).getBody();
			return result;
		} 
		return null;
	}
}

完结!希望能给你带来帮助

你可能感兴趣的:(微服务)