SpringCloud简单架构实现
搭建步骤
从三个方面来熟悉和搭建微服务框架:
注册中心;
服务提供方;
服务消费方
由于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:
易错架构: