基于Spring Cloud搭建微服务--简单实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Linjingke32/article/details/82284205

        1. 环境说明

            - JDK: Java1.7以上,这里使用Java1.8

            - Spring Framework4.2.7以上

            - Maven构建工具

            - Spring Boot使用1.3.8版本

            - Spring Cloud使用Brixton.SR7版本

            - IDE使用IntelliJ IDEA

            - 搭建好的每个服务及端口如下所示:

              

        2. 服务注册中心

            为了提高开发速度,这里使用网页工具产生工程项目框架再进行修改以进行进一步开发,下面的工程项目都这样配置,其主页如下:

            

            选择Spring Boot版本为1.3.8,也可另外版本,输入项目Group ID信息和Artiface信息,依赖包搜索`Eureka`确定后,点击绿色按钮即可下载项目模板:

             

              导入项目:

              

              导入后,等待下载依赖包,完成后如下:

              

              修改IHouseServerCenterApplication.Java代码为如下:

              

              第7行的注解`@EnableEurekaServer`给当前应用开启服务注册中心,提供服务提供者接入。因为当前应用是服务注册中心,因此它没有必要注册自己本身。因此,修改配置文件如下:

               

               上面的配置说明了端口和服务注册地址,现在来启动服务注册中心:

               

               启动后,打开浏览器地址: `http://localhost:1100`,页面如下,因为还没有注册服务,因此这里没什么内容:

              

        3. 服务提供实例x:(生产者)

            以上面同样方式,新建项目模板:

            

            导入IDEA后,项目pom如下:

             

            修改IHouseProApplication.java代码如下:

            

package cn.linjk.iHousePro;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class IHouseProApplication {

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        ServiceInstance instance = client.getLocalServiceInstance();

        return "Access /Index from " + instance.getHost();
    }

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

             修改配置文件,配置服务注册中心地址:

             

             因为这里多个服务提供实例是一样的,注意到这里我没有配置"serveri.port",这里使用生成jar包的方式,在启动时配置端口参数。生成Jar包(在pom.xml已声明打包方式是jar包):

             

             启动实例1,端口1200:

            

            开启新的终端,开启实例2,端口1201:

           

           这时在服务注册中可以看到如下界面,现在,两个一样的实例已注册完成:

           

        4. 服务消费者x:(消费者)

            以上面同样方式,新建项目模板:

            

             导入项目后,pom如下:

             

             修改IHouseClientApplication.java代码如下,注意这里在获取服务时,url地址使用的是服务名称:

             

package cn.linjk.iHouseClient;

import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class IHouseClientApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/client", method = RequestMethod.GET)
    public String client() {
        return restTemplate.getForEntity("http://IHOUSE-PRO/index", String.class)
                .getBody();
    }

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

             修改配置文件如下:

             

            启动应用:

            

            这时发现服务注册中心多了一个服务,因为客户端需要与服务端同步服务列表,因此,也需要依赖于eureka,客户端的访问负载均衡基于Ribbon来进行处理:

             

             访问`http://localhost:1300/client`,如下:

            

            可以修改服务提供程序,打印日志,看看多次访问,会由哪个服务提供者返回,这里就不再修改了。
刷新服务注册中心,可以发现如下一行红色的字:

            

            这是Eureka Server的自我保护机制,服务注册Eureka后,会有一个心跳来监测存活状态,由于本地调试很容易触发,可以暂时屏蔽,可以在配置文件增加配置:
`eureka.server.enable-self-preservation = false`来关闭保护机制,以确保注册中心可以不将不可用的实例正确剔除。

        5. 基于Spring Cloud的简单微服务搭建完,这里只是一个很简单地流程,实际生产环境还有很多完善机制需要添加的。

猜你喜欢

转载自blog.csdn.net/Linjingke32/article/details/82284205
今日推荐