SpringCloud简单架构实现

搭建步骤

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
从三个方面来熟悉和搭建微服务框架:

注册中心;
服务提供方;
服务消费方

由于spring cloud已经帮我们集成了微服务框架,只需要从三个方面修改就可以快速搭建环境:

pom.xml jar包引用;
application配置文件;
注解以及少量代码

这里使用 idea搭建微服务环境
在Spring initialazr可以提前做好demoSpring initialazr

服务注册中心搭建

选择版本(这里选择自己适用的版本,可能随时间推移版本发生变化,大同小异)
在这里插入图片描述
添加依赖
在这里插入图片描述
下载

下载后解压,并在idea中导入
在这里插入图片描述
在这里插入图片描述
配置文件application.properties,增加配置

spring.application.name=demo-server
server.port=1001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动类增加注解

@EnableEurekaServer
@SpringBootApplication
public class DemoServerApplication {

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

}

启动启动类,如果提示报错

java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present
pom.xml引入

<dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.3.0</version>
</dependency>
<dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.3.0</version>
</dependency>
<dependency>
   <groupId>org.glassfish.jaxb</groupId>
   <artifactId>jaxb-runtime</artifactId>
   <version>2.3.0</version>
</dependency>
<dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1.1</version>
</dependency>

启动工程后,访问:http://localhost:1001/ ,可以看到下面的页面,其中还没有发现任何服务。
在这里插入图片描述

服务提供者client搭建

选择
在这里插入图片描述
依赖
在这里插入图片描述
同样解压添加至idea
配置文件application.properties,增加配置

#暴露的服务名
spring.application.name=demo-client 
#服务端口
server.port=2001 
 #注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

启动类增加注解

@EnableEurekaClient
@SpringBootApplication
public class DemoClientApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoClientApplication.class, args);
	}
}

创建一个接口用来消费demo-client提供的接口
这里注意DemoClientApplication在最上层

@RestController
public class DcController {
    @Autowired
    DiscoveryClient discoveryClient;
    @GetMapping("/dc")
    public  String dc(){
        String services = "Services:"+discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
}

启动client
地址http://localhost:2001/dc地址

服务消费者搭建(consumer)

在这里插入图片描述
这里暂时选择OpenFeign 原本用feign组件做消费服务 现在网站没有 待会再改
在这里插入图片描述
同样下载解压导入

pom.xml文件修改

//将feign-open改为这个
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
      </dependency>

配置文件application.properties

spring.application.name=demo-consumer
server.port=2101
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#注册中心地址

启动类增加注解

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoConsumerApplication {

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

}

创建一个Feign的客户端接口定义

@FeignClient("demo-client")
public interface DcClient {
    @GetMapping("/dc")
    String consumer();
}

通过定义的feign消费端来调用服务提供方client的接口

@RestController
public class DcController {
//这里装配可能因为idea的关系报错  但是没有关系
    @Autowired
    DiscoveryClient discoveryClient;
    @GetMapping("/dc")
    public  String dc(){
        String services = "Services:"+discoveryClient.getServices();
        System.out.println(services);
        return services;
    }

启动consumer。访问http://localhost:2101/consumer-feign 地址
可以看到结果和http://127.0.0.1:2001/dc地址 结果一样。

最后呈现结果

server:
在这里插入图片描述
client:
在这里插入图片描述
consumer:
在这里插入图片描述
易错架构:
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值