SpringCloud2.0实战之一

本文详细介绍了如何搭建基于SpringCloud2.0的微服务架构,包括注册服务中心的配置,会员服务与订单服务的注册流程,以及通过Eureka实现服务发现。通过实例展示了如何使用RestTemplate进行服务间调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建一个SpringCloud2.0,springcloud2.0和springcloud1.5的区别主要在于pom.xml文件中导包的版本,和application.properties中属性之间的细微差别(springcloud2.0比springcloud1.5会更加精细)

首先需要搭建一个注册服务中心:

server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      ######注册地址
      defaultZonde: http://${eureka.instance.hostname}:${server.port}/eureka/
    ######因为自己是注册中心,是否需要将自己注册到自己的注册中心(集群的时候需要true)
    register-with-eureka: false
    ######因为自己是注册中心,不需要去检索服务信息
    fetch-registry: false
package com.eurekaserver;

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

@SpringBootApplication
//此注解用于将服务加载到注册中心
@EnableEurekaServer
public class EurekaServerApplication {

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

启动项目

将会员服务注册到服务中心

server:
  port: 8801
spring:
  application:
    ####服务的别名,注册到注册中心的service—id
    name: eureka-member
eureka:
  client:
    serviceUrl:
    ######注册地址
      defaultZone: http://localhost:8888/eureka/
    ######需要将服务注册到注册中
    register-with-eureka: true
    ######需要去检索服务信息
    fetch-registry: true
package com.eurekamember;

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

@SpringBootApplication
//将当前服务注册到eureka上
@EnableEurekaClient
public class EurekaMemberApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaMemberApplication.class, args);
	}
}
package com.eurekamember.api.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

    @RequestMapping("/getMember")
    public String getMember(){
        return "springboot2.0 springcloud";
    }
}

订单服务

server:
  port: 8802
spring:
  application:
    name: eureka-order
eureka:
  client:
    serviceUrl:
    ######注册地址
      defaultZone: http://localhost:8888/eureka/
    ######需要将服务注册到注册中
    register-with-eureka: true
    ######需要去检索服务信息
    fetch-registry: true
package com.eureakorder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
//将服务注册到注册中心
@EnableEurekaClient
public class EureakOrderApplication {

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

	//将RestTemplate注册到容器中
	//将rest模板注入到@Bean容器中
	@Bean
	//@LoadBalanced注解表示支持负载均衡,加上注解,默认启动负载均衡,对应在pom文件中ribbin
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
}
package com.eureakorder.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
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
    //客户端调用工具,rest模板 RestTemplate是由SpringBoot web组件提供,默认整合ribbon负载均衡
    //rest方式底层是采用httpclien技术
    private RestTemplate restTemplate;
    @RequestMapping("/getOrder")
    public String getOrder(){
        return "order 订单springboot2.0";
    }

    @RequestMapping("/getMember")
    public String getMember(){
        //两种调用方式:一种采用服务别名。另一种直接调用,如果使用服务别名就要使用ribbon负载均衡
        String string=restTemplate.getForObject("http://eureka-member/getMember",String.class);
        return string;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值