SpringCloud学习之手把手教你用IDEA搭建入门项目(三)

      本篇博客是承接上一篇《手把手教你用IDEA搭建SpringCloud入门项目(二)》,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目,本篇博客主要是贴出项目的代码部分,方便读者更好的实战操作搭建一个属于自己的SpringCloud项目

1)项目框架大体如下

2)编写eureka-server模块的启动类

package com.xu.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

3)把properties文件改成yml类型文件,并且编辑配置文件如下

application.yml文件编辑如下:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost    #表示这个注册中心在本地
  client:
    registerWithEureka: false   #注册中心不注册自己,默认是注册自己
    fetchRegistry: false
    serviceUrl:
      #拿到上面的主机地址和端口地址,配置一个注册中心
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4)启动注册中心服务,看到控制台输出这些信息,则表示注册中心服务启动成功

5)注册中心服务启动后,在浏览器输入地址:http://localhost:8761/,应该可以看到这个界面,这里可以看到在注册中心注册的一些服务

6)服务提供者模块service-provider其他代码如下

ServiceProviderApplication.java
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }

}

修改application.properties文件为application.yml,并编辑如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/    #这里是告诉他注册中心地址
server:
  port: 8763
spring:
  application:
    name: service-hello    #这里给服务取一个帅气的名字

这里只提供了一个RestApi接口服务,后面服务消费者会调用这个测试API服务:

HelloWorldController.java

package com.xu.serviceprovider.controller;

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

@RestController
public class HelloWorldController {

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ", i am from port: " + port;
    }

}

启动这个模块

然后刷新注册中心地址http://localhost:8761/,就可以看到他已经被注册到注册中心了

7)服务消费者模块service-consumer其他代码如下:

ServiceConsumerApplication.java

package com.xu.serviceconsumer;

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 ServiceConsumerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {

        return new RestTemplate();
    }
}

service-consumer模块的application.yml如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

其他代码如下:

HelloService.java

package com.xu.serviceconsumer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name)
    {
        return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name, String.class);
    }
}

HelloControler.java

package com.xu.serviceconsumer.controller;

import com.xu.serviceconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService(name);
    }
}

启动这个模块,成功后如下:

刷新注册中心地址:http://localhost:8761/,可以看到一个新的服务又被注册到注册中心了

打开浏览器,在一个新的窗口中输入地址:http://localhost:8764/hi?name=admin,如果成功则返回如下信息则表示成功调用了服务提供者的服务接口(根据端口可以知道,8763端口是服务提供者端口)

本篇博客到此为止,如有问题请留言大家一起讨论学习,诸君共勉!

猜你喜欢

转载自www.cnblogs.com/xulijun137/p/12209721.html