springcloud学习入门

Springcloud入门学习笔记 1. 项目初始化配置 1. 1. 新建maven工程

使用idea创建maven项目

1. 2. 在parent项目pom中导入以下依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <properties> <spring.cloud-version>Hoxton.SR8</spring.cloud-version> </properties> <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> </dependencies> </dependencyManagement> 2. Eureka使用 2. 1. 创建子module,命名为eureka-server 2. 2. 在eureka-server中添加以下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-security</artifactId> </dependency> 2. 3. 在application.yml中添加以下配置 server: port: 8900 #应用的端口号 eureka: client: service-url: defaultZone: :123@localhost:8900/eureka #eureka服务的的注册地址 fetch-registry: false #是否去注册中心拉取其他服务地址 register-with-eureka: false #是否注册到eureka spring: application: name: eureka-server #应用名称 还可以用eureka.instance.hostname = eureka-server security: #配置自定义Auth账号密码 user: name: user password: 123 2. 4. 在启动类上架注解 @SpringBootApplication @EnableEurekaServer

在启动类中加入以下方法,防止spring的Auth拦截eureka请求

@EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } } 2. 5. 创建module名为provider-user为服务提供者 2. 5. 1. 在pom中添加以下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 2. 5. 2. application.yml配置 server: port: 7900 #程序启动入口 spring: application: name: provider-user #应用名称 eureka: client: service-url: defaultZone: :123@${eureka.instance.hostname}:${server.port}/eureka/ 2. 5. 3. 启动类加注解 @SpringBootApplication @EnableEurekaClient

Controller相关代码如下:

@RestController public class UserController { @GetMapping (value = "/user/{id}") public User getUser(@PathVariable Long id){ User user = new User(); user.setId(id); user.setDate(new Date()); System.out.println("7900"); return user; } @PostMapping (value = "/user") public User getPostUser(@RequestBody User user){ return user; } } 2. 6. 创建module名为consumer-order为服务提供者 2. 6. 1. pom依赖同服务提供者 2. 6. 2. application.yml配置 server: port: 8010 spring: application: name: consumer-order eureka: client: serviceUrl: defaultZone: :123@${eureka.instance.hostname}:${server.port}/eureka/ 2. 6. 3. 启动类 @SpringBootApplication @EnableEurekaClient public class ConsumerApp { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main( String[] args ) { SpringApplication.run(ConsumerApp.class,args); } } 2. 6. 4. Controller层代码 @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping (value = "/order/{id}") public User getOrder(@PathVariable Long id){ //获取数据 User user = new User(); user.setId(id); user.setDate(new Date()); user = restTemplate.getForObject("http://provider-user:7900/user/"+id,User.class); return user; } } 2. 7. 启动应用

分别启动Eureka-server、provider-user、consumer-order三个服务

2. 8. 访问地址

:8900就可以看到两个服务已经注册到eureka注册中心上了

2. 9. eureka高可用配置

两个节点

#高可用配置,两个节点 spring: application: name: eureka-server-ha profiles: active: peer1 eureka: client: serviceUrl: defaultZone: https://peer1/eureka/, --- server: port: 8901 spring: profiles: peer1 eureka: instance: hostname: peer1 --- server: port: 8902 spring: profiles: peer2 eureka: instance: hostname: peer2

三个节点

#高可用配置,三个 spring: application: name: eureka-server-ha profiles: active: peer3 eureka: client: serviceUrl: defaultZone: :8901/eureka/,:8902/eureka/,:8903/eureka/ --- spring: profiles: peer1 eureka: instance: hostname: peer1 server: port: 8901 --- spring: profiles: peer2 eureka: instance: hostname: peer2 server: port: 8902 --- spring: profiles: peer3 eureka: instance: hostname: peer3 server: port: 8903 3. Ribbon的使用入门 3. 1. 方式一(默认)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwydw.html