

然后解压,打开后一个exe文件,按住shift按右键,打开powerShell界面,输入下面命令启动。
./consul agent -dev
浏览器访问控制台 localhost:8500



consumer代码:
- @Configuration
- public class RestTemplateConfig {
- @Bean
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
- }
-
- @RestController
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- private RestTemplate restTemplate;
-
- @Autowired
- private DiscoveryClient discoveryClient;
-
- @GetMapping("/goods/{id}")
- public Goods findGoodsById(@PathVariable("id") int id){
- System.out.println("------------findGoodsById--------"+id);
-
- //远程调用Goods服务中的findOne接口
- /*
- 使用RestTemplate
- 1.定义Bean restTemplate
- 2.注入Bean
- 3.调用方法
- */
-
- /*
- 动态从Eureka Server中获取 provider的ip端口
- 1.注入 DiscoveryClient对象 激活
- 2.调用方法
- */
-
- //演示discoveryClient使用
- /**/
- List
instances =discoveryClient.getInstances("consul-PROVIDER"); - if (instances == null || instances.size() == 0){
- //集合没有数据
- System.out.println("没有获取到端口和IP");
- return null;
- }
- ServiceInstance instance = instances.get(0);
- String host =instance.getHost();//获取IP
- int port = instance.getPort();//获取端口
- System.out.println("----------host"+host);
- System.out.println("----------port"+port);
- //String url = "http://localhost:8000/goods/findOne/"+id;
- String url = "http://"+host+":"+port+"/goods/findOne/"+id;
- //3.调用方法
- Goods goods = restTemplate.getForObject(url,Goods.class);
- return goods;
- }
- }
- public class Goods {
- private int id;
- private String title;
- private double price;
- private int count;
- public Goods() {
- }
- public Goods(int id, String title, double price, int count) {
- this.id = id;
- this.title = title;
- this.price = price;
- this.count = count;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- @Override
- public String toString() {
- return "Goods{" +
- "id=" + id +
- ", title='" + title + '\'' +
- ", price=" + price +
- ", count=" + count +
- '}';
- }
- }
- @EnableDiscoveryClient //激活DiscoveryClient 可无
- //启动类
- @EnableEurekaClient //可无
- @SpringBootApplication
- public class ConsumerApp {
- public static void main(String[] args){
- SpringApplication.run(ConsumerApp.class,args);
- }
- }
consumer和provider依赖:
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-consul-discoveryartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
-
- dependencies>
consumer配置:
- server:
- port: 9000
-
- spring:
- cloud:
- consul:
- host: localhost
- port: 8500
- discovery:
- service-name: ${spring.application.name} #当前应用注册到consul的名称
- prefer-ip-address: true #注册ip
- application:
- name: consul-consumer
provider配置:
- server:
- port: 8000
-
- spring:
- cloud:
- consul:
- host: localhost
- port: 8500
- discovery:
- service-name: ${spring.application.name} #当前应用注册到consul的名称
- prefer-ip-address: true #注册ip
- application:
- name: consul-provider
provider代码:
- //服务提供方
- @RestController
- @RequestMapping("/goods")
- public class GoodsController {
- @Autowired
- private GoodsService goodsService;
-
- @GetMapping("/findOne/{id}")
- public Goods findOne(@PathVariable("id") int id){
- Goods goods = goodsService.findOne(id);
- return goods;
- }
- }
-
- @Repository
- public class GoodsDao {
- public Goods findOne(int id){
- return new Goods(1,"华为手机",3999,10000);
- }
- }
-
- public class Goods {
- private int id;
- private String title;
- private double price;
- private int count;
-
- public Goods() {
- }
-
- public Goods(int id, String title, double price, int count) {
- this.id = id;
- this.title = title;
- this.price = price;
- this.count = count;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public int getCount() {
- return count;
- }
-
- public void setCount(int count) {
- this.count = count;
- }
-
- @Override
- public String toString() {
- return "Goods{" +
- "id=" + id +
- ", title='" + title + '\'' +
- ", price=" + price +
- ", count=" + count +
- '}';
- }
- }
-
-
- @Service
- public class GoodsService {
- @Autowired
- private GoodsDao goodsDao;
-
- public Goods findOne(int id){
- return goodsDao.findOne(id);
- }
-
- }
-
- //启动类
- @EnableEurekaClient
- @SpringBootApplication
- public class ProviderApp {
- public static void main(String[] args){
- SpringApplication.run(ProviderApp.class,args);
- }
- }
运行consumer和provider两个服务互通

在consul上可以查到两个服务
