美文网首页
spring cloud 之初步认识

spring cloud 之初步认识

作者: 心灵的震撼 | 来源:发表于2018-10-19 10:05 被阅读0次

1.实现一个微服务,有订单服务,有会员服务。订单服务通过rest方式去调用会员服务,注册中心使用eureka。

第一步:实现一个eureka服务注册中心。

POM的相关配置:增加依赖如下:

image

application.yml的配置如下:

设置euraka-server访问时带有权限,不能直接访问

security:

basic:

enabled:true

user:

name: zzh

password: password123

server:

port:8086

设置注册显示的实例名称为eureka-server1,如果不配置显示为UNKNOWN

spring:

application:

name: eureka-server1

eureka:

关闭Eureka的自我保护功能

server:

enable-self-preservation:false

  #配置Eureka Server清理无效节点的时间间隔

  eviction-interval-timer-in-ms:4000

instance:

hostname: localhost

配置鼠标点击到某个服务上显示对应的IP地址

  prefer-ip-address:true

client:

register-with-eureka:false

  fetch-registry:false

  healthcheck:

enabled: true

serviceUrl:

defaultZone: http://zzh:password123@{eureka.instance.hostname}:{server.port}/eureka/

新建一个类:

EurakaServerApplication

@SpringBootApplication

@EnableEurekaServer

public class EurakaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurakaServerApplication.class, args);

}

}

启动服务。

通过浏览器访问后就可以查看到基本eureka 的整个界面情况。

第二步:把服务注册到eureka中。

新建一个会员服务工程

增加依赖:

image

编写application.yml

server:

port:8088

spring:

application:

name: service-member

eureka:

instance:

prefer-ip-address:true

client:

register-with-eureka:true

healthcheck:

enabled: true

serviceUrl:

defaultZone: http://zzh:password123@127.0.0.1:8086/eureka/

编写controller

@RestController

@RequestMapping("/member")

public class MemberController {

@RequestMapping("/getAllMemberInfo")

@ResponseBody

public ListgetAllMemberInfo(){

List list=new ArrayList<>();

    list.add("zhangsan");

    list.add("lisi");

    list.add("wangwu");

    return list;

}

}

编写启动服务类:

@SpringBootApplication

@EnableEurekaClient

@ComponentScan(basePackages ="com.itshirui")

public class ServiceMemberApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceMemberApplication.class, args);

}

}

启动成功后,可以去注册中心的页面中查看该服务。

第三部,编写订单服务,订单服务中调用会员服务,目前通过rest方式进行通讯的发送。

配置和会员服务一样,只是controller中的调用采用rest方式进行调用:

@RestController

@RequestMapping("/order")

public class OrderController {

@Autowired

private RestTemplaterestTemplate;

@RequestMapping("/getAllOrderInfo")

@ResponseBody

public List  getAllOrderInfo(){

List result =restTemplate.getForObject("http://service-member/member/getAllMemberInfo", List.class);

    return result;

}

}

经过测试通过订单服务调用会员服务,能成功。

相关文章

网友评论

      本文标题:spring cloud 之初步认识

      本文链接:https://www.haomeiwen.com/subject/lofszftx.html