SpringCloud(一)
创建一个项目
1.导入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent>
<!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.创建application.yml文件
##声明端口号 server: port: 8888 ##配置eureka eureka: client: ##代表是否将自己注册到注册中心去 register-with-eureka: false ##表明自己是注册中心 fetch-registry: false ##配置地址 service-url: defaultZone: http://localhost:8888/eureka
3.启动类
@SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); } }
访问结果
生产者和消费者
生产者项目
1.导入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.创建application.yml文件
##配置应用名称 spring: application: name: eu_provider ##声明端口号 server: port: 8891 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
3.service接口
public interface IDoService { public String doSome(); }
4.service实现类
@RestController public class IDoServiceImpl implements IDoService { @RequestMapping("/doSome") @Override public String doSome() { System.out.println("服务提供者"); return "eureka"; } }
启动类
@SpringBootApplication @EnableEurekaClient public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); } }
访问结果
消费者项目
1.导入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.创建application.yml文件
##配置应用名称 spring: application: name: eu_consumer ##声明端口号 server: port: 8895 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
3,创建Controller
@RestController public class ConsumerController { @Resource private RestTemplate restTemplate; @RequestMapping("/doSome") public String doSome(){ System.out.println("ConsumerController"); return restTemplate.getForObject("http://localhost:8891/doSome",String.class); } }
启动类
@SpringBootApplication @EnableEurekaClient /*@EnableFeignClients*/ public class StaConsumer { public static void main(String[] args) { SpringApplication.run(StaConsumer.class, args); } @Bean /*@LoadBalanced //实现负载均衡*/ RestTemplate restTemplate(){ return new RestTemplate(); } //指定负载均衡策略 @Bean public IRule ribbonRule(){ return new RandomRule(); } }
访问结果
负载均衡
@RestController
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@RequestMapping("/doSome")
public String doSome(){
System.out.println("ConsumerController");
return restTemplate.getForObject("http://eu_provider/doSome",String.class);
}
}
Feign负载均衡策略
1.引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.创建service层
@FeignClient("eu_provider")
public interface IDoService {
@RequestMapping("/doSome")
public String doSome();
}
3.编写controller
@RestController public class ConsumerController { @RestController public class ConsumerController { /* @Resource private RestTemplate restTemplate;*/ @Resource private IDoSomeService iDoSomeService; @RequestMapping("/doSome") public String doSome(){ System.out.println("消费者"); return iDoSomeService.doSome(); } }