springCloud
一、
<!--限定springBoot和springCloud的版本--> <properties> <project.build.sourceEncoding> UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> <springboot.version>2.0.5.RELEASE</springboot.version> </properties> <!--引入springBoot和springCloud的依赖--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> 包含了spring-cloud-dependencies和spring-boot-dependencies -->
<!--springboot支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--Eureka服务端支持--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
3.3.3 yum配置---resources中配置application.yml
#注册中心端口号 server: port: 7001 #注册中心的配置 eureka: instance: hostname: localhost client: #是否要注册到eureka,因为这里是先配置的服务器端,所以不注册 registerWithEureka: false #是否从eureka注册中心获取地址 fetchRegistry:false serviceUrl: #单机配置 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.3.4 配置主类(Application类)
@SpringBootApplication @EnableEurekaServer//标志是Euraka服务端,开启注册中心 public class EurekaApplication { public static void main(String[] args){ SpringApplication.run(EurekaApplication.class); } }
<dependencies>
<!--
公共代码依赖
<dependency>
<groupId>cn.itsource.springcloud</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
-->
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
-->
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.主配置类
@SpringBootApplication @EnableEurekaClient //开启注册中心客户端 public class DeptApplication { public static void main(String[] args){ SpringApplication.run(DeptApplication.class); } }
3.yml配置
server: port: 8001 spring: application: name: DEPT-PROVIDER #不要使用下划线 eureka: client: service-url: #告诉服务提供者要把服务注册到哪儿 defaultZone: http://localhost:7001/eureka
4. controller层
@RestController //使用@RestController是因为要传json值 public class DeptController { //springCloud是基于restful风格的,所以传参也要这个样子 @RequestMapping("/dept/{id}") public Dept getDept(@PathVariable("id") Long id){ /* dept作为服务的提供者,要向外暴露接口,返回的是一个dept对象, 服务的消费者调用接口获取,获取到的是dept对象,所以需要把dept对象单独提出去 再以jar包的方式依赖进行 */ //模拟数据库返回数据 return new Dept(id,"销售部"); } }
5.公共代码块 common模块
pom.xml中引入公共模块的依赖
<!--引入公共部分的jar包-->
<dependency>
<groupId>cn.itsource</groupId>
<artifactId>springCloud-common</artifactId>
</dependency>
4.1.1 启动结果
4.1.2 修改配置文件中的name
application.yml
server: port: 8001 spring: application: name: DEPT-PROVIDER #不要使用下划线 eureka: client: service-url: defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿 instance: prefer-ip-address: true #使用ip地址作为显示 instance-id: dept-provider #页面status部门显示的内容,没有任何影响
4.1.3 测试服务提供方法---成功
1.导包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--引入公共部分的jar包--> <dependency> <groupId>cn.itsource</groupId> <artifactId>springCloud-common</artifactId> </dependency> <!--eureka客户端支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.yml配置
application.yml
#注册中心消费者的客户端设置 server: port: 9001 spring: application: name: USER_CONSUMER eureka: client: service-url: defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿 instance: prefer-ip-address: true #使用ip地址作为显示 instance-id: uesr-consumer #页面status显示的内容,没有任何影响
3.主配置类Application类
@SpringBootApplication @EnableEurekaClient public class UserApplication { /** * 使用RestTemplate调用服务者提供的接口 * RestTemplate交个spring管理 * @param */ @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } public static void main(String[] args){ SpringApplication.run(UserApplication.class); } }
4.controller层
@RestController public class UserController { //注入RestTemplate @Autowired private RestTemplate restTemplate; @RequestMapping("/userDept/{id}") public Dept getDeptById(@PathVariable("id") Long id){ System.out.println("1111111111"); /* 调用注册中心的提供者的接口 url:http://localhost+服务提供者的配置文件的端口号+调用的方法路径 */ Dept dept = restTemplate.getForObject("http://localhost:8001/dept/" + id, Dept.class); System.out.println(dept); return dept; } }
测试-------成功